V
V
Vladimir Minenkov2014-12-24 15:17:06
Java
Vladimir Minenkov, 2014-12-24 15:17:06

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();
            }
        }
    }
 
}

I want to make sure that by clicking on the GO button, different images are loaded for me, depending on the selected item from the drop-down list.
As I understand it, you can write in the "paint" method:
String n = combo.getSelectedItem();
Image image = new ImageIcon(n + ".jpg").getImage();
ourGraphics.drawImage(image, 100, 100, this);

But the eclipse accordingly gives the error "combo cannot be resolved"...
As I understand it, because the listener is in another method (main).
Tell me, good people, how can I solve this problem or do it differently?
Thank you in advance.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
N
Nikolai Stupak, 2014-12-25
@nikolaas

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();
}

In TransformingCanvas I would have an ImageProvider field and a setter for it. When you click on the button, you set the corresponding implementation of the ImageProvider through the setter.
When rendering, the TransformingCanvas must take the image from the ImageProvider. One question remains: how do we notify the TransformingCanvas that the image has changed and needs to be redrawn? There are many ways to solve. 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.
ps And more. Of course, I don't know the specifics of your TransformingCanvas, but in general it is better to override not the JComponent.paint(Graphics g) method, but JComponent.paintComponent(Graphics g) The paint method defines the entire component drawing algorithm, including its border and its child components, while while paintComponent only paints the component itself. By redefining paint, you will have to bother with drawing child components yourself, if necessary. Read the documentation for these methods, everything is described there.

N
Nikolai, 2014-12-24
@j_wayne

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.

V
Vladimir Minenkov, 2014-12-25
@sqwartl

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.

All the same, I didn’t understand (((
I don’t care how I need to turn to the button listener and pull it out combo.getSelectedItem();
... I’m confused (

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question