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();
}
}
}
Your idea of using a list of colors in the right idea (unless all objects have the same color at the start). In this list you store the initial color of all objects and the you can do
// initialColors is the list holding the initial colors
for (int i=0; i<images.size(); i++) {
if (images.get(i).getShape() == "Rectangle") {
if (/*code to check if we are inside the rectangle which you already have*/) {
images.get(i).setColor(Color.BLUE);
repaint();
} else {
images.get(i).setColor(initialColors.get(i));
repaint();
}
} /* maybe add a case for getShape() == "Circle" */
}
You could create and populate the initialColors
List at the same time as the images
List (because in that moment, you know which color each shape has).
About why your approach is not working: Let's say we have clicked inside a rectangle and its color is changed to blue. When we now use colorAux = images.get(i).getColor()
to get the color, we get blue, because we changed the rectangles color. When we then reach images.get(i).setColor(colorAux)
, we set the color of the blue rectangle to blue, meaning nothing happens.
Also, you do not need the else if
and can use else
instead, as the first if
check whether the click happend inside the rectangle. Meaning we execute the else branch when the click was not inside the rectangle and we can simply reset the color in there.
Edit: Now the change you added to the question does not work, as we still have the same problem: We get the color in the mouseClicked
event, not when the shapes are initially colored. This means we fill the list of colors with the current colors (which might have been changed to blue), not the initial ones. You should move the loop you added wherever you initially color the shapes (probably where you fill the images
list).