2

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();
        }
    }
}
alexrnov
  • 2,346
  • 3
  • 18
  • 34
zsp
  • 31
  • 6
  • Could you please clarify what you want (or expect) to happen, as in, what do you want to happend when one clicks outside a shape? – Leon Aug 23 '18 at 13:53
  • @Leon ohh, sorry! I just want to color with the chosen color previously. To clarify, an example would be: i draw a purple rectangle > so i click inside it to change its color to blue > then i click outside and change its color to purple again – zsp Aug 23 '18 at 14:00
  • So it is the `else if` condition which doesn't work ? – Arnaud Aug 23 '18 at 14:16
  • I think here's what happening: You set the color to blue on a click. You click again, now the color is Blue and you set it to, BLUE – Amir Afghani Aug 23 '18 at 14:17
  • @Arnaud the `else if`, because does not change back to the previous color. – zsp Aug 23 '18 at 14:20

2 Answers2

2

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).

Leon
  • 2,926
  • 1
  • 25
  • 34
  • Thanks for answering! I've edited the post with some bad news. – zsp Aug 23 '18 at 14:40
  • Thanks, Leon! Now it's working! But there's another problem. :( Have edited the post again.. – zsp Aug 23 '18 at 14:52
  • Currently, the code only has a `if` case to handle rectangles. To handle circles you need to add the `else if` clause I suggested in a comment in the code snipped provided by me (in case you did mot already do that). And currently the code will change the color of all shapes in which you have clicked (and change all the others back). Meaning that if you click in an area which lies within multiple shapes (e.g. the shapes intersect) all of them will change color. The `if` condition you are using looks correct to me and the loop as well. This *might* be a problem with the `setColor` method. – Leon Aug 23 '18 at 14:57
  • Nevermind, Leon! It's working perfectly! Thank you very much for the help! When i was deleting one shape, my code was not removing its color too. Now it's working! – zsp Aug 23 '18 at 15:01
  • As my answer solved your problem, please consider accepting it. Check [this help article](https://stackoverflow.com/help/someone-answers) if you want to know more about how to handle answers to your questions on StackOverflow. – Leon Aug 23 '18 at 15:03
1

Here's what I think is happening :

  • User clicks the mouse inside the shape - and the shape's color is set to blue.
  • User clicks the mouse outside of the shape, and we set the color to colorAux, which was set by the images color, which is blue!

You need to save the original color somewhere.

Amir Afghani
  • 37,814
  • 16
  • 84
  • 124