I'm stuck on this problem:
When I click inside a shape (there's a list of rectangles and circles) it changes its color. But when I click outside, it doesn't change back.
public void mouseClicked(MouseEvent me) {
Color colorAux;
for (int i = 0; i < images.size(); i++) {
colorAux = images.get(i).getColor();
if (images.get(i).getShape() == "Rectangle") {
if ((images.get(i).getLocation().getX() < me.getX() && images.get(i).getLocation().getY() < me.getY() && images.get(i).getX() + images.get(i).getWidth() > me.getX() && images.get(i).getLocation().getY() + images.get(i).getHeight() > me.getY())) {
images.get(i).setColor(Color.BLUE);
repaint();
JOptionPane.showMessageDialog(null, colorAux); //Debug
} else if (!(images.get(i).getLocation().getX() < me.getX() && images.get(i).getLocation().getY() < me.getY() && images.get(i).getX() + images.get(i).getWidth() > me.getX() && images.get(i).getLocation().getY() + images.get(i).getHeight() > me.getY()) && (images.get(i).getColor() == Color.BLUE)) {
images.get(i).setColor(colorAux);
repaint();
}
}
}
Should I be using an array of colors? Don't know how can I solve this. To clarify what I am trying to archive, here is an example:
If the list contains a purple rectangle, I want it to change to blue when clicking inside it (which works). Then, when I click outside the rectangle, I want it to change back to purple (which does not work).
I've tried the Leon's advice, but it did not work. Where am i doing wrong?
Being more specific, when i draw only 1 shape it works! But for example, i draw a blue rectangle, a purple circle and a red rectangle, and click inside some of the shapes, like the red rectangle, every shape changes its color to BLUE. And when i click outside again, it changes every shape's color to the default color (black).
public void mouseClicked(MouseEvent me) {
List<Color> colors = new ArrayList<Color>();
for (int j = 0; j < images.size(); j++) {
colors.add(images.get(j).getColor());
}
for (int i = 0; i < images.size(); i++) {
if ((images.get(i).getLocation().getX() < me.getX() && images.get(i).getLocation().getY() < me.getY() && images.get(i).getX() + images.get(i).getWidth() > me.getX() && images.get(i).getLocation().getY() + images.get(i).getHeight() > me.getY())) {
images.get(i).setColor(Color.BLUE);
repaint();
} else {
images.get(i).setColor(colors.get(i));
repaint();
}
}
}