Answer the question
In order to leave comments, you need to log in
How to upload images to a list?
Good evening.
All with the upcoming!
There is a code:
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.AffineTransform;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class tests {
private static TransformingCanvas canvas;
public static void main(String[] args) {
JFrame frame = new JFrame();
JPanel toolbar = new JPanel();
final String[] number = new String[] {"1044", "1045", "1046", "1054"};
final JComboBox combo = new JComboBox(number);
combo.setSelectedIndex(0);
combo.setEditable(true);
final JButton butGo = new JButton("GO");
butGo.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
combo.getSelectedItem();
}
});
toolbar.add(combo);
toolbar.add(butGo);
canvas = new TransformingCanvas();
TranslateHandler translater = new TranslateHandler();
canvas.addMouseListener(translater);
canvas.addMouseMotionListener(translater);
canvas.addMouseWheelListener(new ScaleHandler());
frame.setLayout(new BorderLayout());
frame.getContentPane().add(toolbar, BorderLayout.PAGE_START);
frame.getContentPane().add(canvas, BorderLayout.CENTER);
frame.setSize(500, 500);
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.setVisible(true);
}
private static class TransformingCanvas extends JComponent {
private static final long serialVersionUID = 1L;
private double translateX;
private double translateY;
private double scale;
TransformingCanvas() {
translateX = 0;
translateY = 0;
scale = 0.5;
setOpaque(true);
setDoubleBuffered(true);
}
@Override
public void paint(Graphics g) {
AffineTransform tx = new AffineTransform();
tx.translate(translateX, translateY);
tx.scale(scale, scale);
Graphics2D ourGraphics = (Graphics2D) g;
ourGraphics.setColor(Color.WHITE);
ourGraphics.fillRect(0, 0, getWidth(), getHeight());
ourGraphics.setTransform(tx);
ourGraphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
ourGraphics.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
Image image = new ImageIcon("1.jpg").getImage();
ourGraphics.drawImage(image, 100, 100, this);
}
}
private static class TranslateHandler implements MouseListener,
MouseMotionListener {
private int lastOffsetX;
private int lastOffsetY;
public void mousePressed(MouseEvent e) {
// Захват стартовой точки
lastOffsetX = e.getX();
lastOffsetY = e.getY();
}
public void mouseDragged(MouseEvent e) {
// new x and y are defined by current mouse location subtracted
// by previously processed mouse location
int newX = e.getX() - lastOffsetX;
int newY = e.getY() - lastOffsetY;
// increment last offset to last processed by drag event.
lastOffsetX += newX;
lastOffsetY += newY;
// update the canvas locations
canvas.translateX += newX;
canvas.translateY += newY;
// schedule a repaint.
canvas.repaint();
}
public void mouseClicked(MouseEvent e) {}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
public void mouseMoved(MouseEvent e) {}
public void mouseReleased(MouseEvent e) {}
}
private static class ScaleHandler implements MouseWheelListener {
public void mouseWheelMoved(MouseWheelEvent e) {
if(e.getScrollType() == MouseWheelEvent.WHEEL_UNIT_SCROLL) {
// make it a reasonable amount of zoom
// .1 gives a nice slow transition
canvas.scale += (.02 * e.getWheelRotation());
// don't cross negative threshold.
// also, setting scale to 0 has bad effects
canvas.scale = Math.max(0.00001, canvas.scale);
canvas.repaint();
}
}
}
}
String n = combo.getSelectedItem();
Image image = new ImageIcon(n + ".jpg").getImage();
ourGraphics.drawImage(image, 100, 100, this);
Answer the question
In order to leave comments, you need to log in
On the good side, from an architectural point of view, you don't need to pass the combobox to the TransformingCanvas, because its job is to draw images. But where the images come from is the second question. I would introduce some kind of interface, for example:
public interface ImageProvider {
Image getImage();
}
Almost no experience with Swing, but can you still pass a combo to the TransformingCanvas constructor, store it in a field, and call it what you want?
Perhaps there is a more regular way.
The simplest one is to explicitly call the redraw of the TransformingCanvas in the ImageProvider setter. It is also possible through a listener registered with the ImageProvider.
combo.getSelectedItem();
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question