I'm trying to figure out how to access my different images, drawn with paintComponent, (using JLabels is not allowed in the assignment) in my events.
When dragging, I only want one image to move with my mouse drag and I cant seem to access "current image" with e.getSource().
My paintComponent
will move all (3) images at the same time at the moment.
My question is: how to get hold of a single ImageIcon with my mouseDragged?
public class PhotoPanel extends JPanel implements MouseListener, MouseMotionListener {
private java.util.List<ImageIcon> myList = new ArrayList<>();
private int mx, my;
private ImageIcon image1 = new ImageIcon("src/resources/gira.gif");
private ImageIcon image2 = new ImageIcon("src/resources/stru.gif");
private ImageIcon image3 = new ImageIcon("src/resources/back.gif");
public PhotoPanel()
{
setBackground(Color.GREEN);
myList.add(image1);
myList.add(image2);
myList.add(image3);
//Is this a problematic way of doin it?
addMouseMotionListener(this);
}
public void paintComponent (Graphics g) {
super.paintComponent(g);
for (ImageIcon i : myList)
{
g.drawImage(i.getImage(), mx, my, this);
}
}
@Override
public void mouseDragged(MouseEvent e) {
//if(e.getSource == image1)
//{
// Manipulate single picture, but not working this way
//}
mx = e.getX();
my = e.getY();
repaint();
}
}