zsp
zsp

Reputation: 31

Selecting two or more shapes at the same time

I'm stuck on this problem:

When I click inside a shape (there's an arraylist of rectangles and circles) I select it (just for debugging, changes its color to BLUE). So if I click outside, in a blank space, I unselect it (just for debugging, changes its color to the previous color).

        for(int i=0; i<images.size(); i++){
        //checking if the click is inside a shape
            if((images.get(i).getLocation().getX() < e.getX() && images.get(i).getLocation().getY() < e.getY() && images.get(i).getX() + images.get(i).getWidth() > e.getX() && images.get(i).getLocation().getY() + images.get(i).getHeight() > e.getY())){ 
                images.get(i).setColor(Color.BLUE);
                images.get(i).setIsSelected(true);
        //debugging
                JOptionPane.showMessageDialog(null, images.get(i).getIsSelected());
                repaint();
                //JOptionPane.showMessageDialog(null, colors.get(i));
            }
            else{
                images.get(i).setColor(colors.get(i));
        //debugging
                JOptionPane.showMessageDialog(null, images.get(i).getIsSelected());
                images.get(i).setIsSelected(false);
                repaint();
                }

For example, imagine 2 circles and 1 rectangle, all in black. My code has the following workflow:

  • Click inside the Rectangle
  • Change its color to BLUE
  • Just for debugging, it prints "selected == true" (for the rectangle), "selected = false" (for the 1st circle), "selected = false", (for the 2nd circle)
  • Click in a blank space
  • Change the rectangle's color to the previous color (black)
  • Just for debugging, it prints "selected == false" (for the rectangle), "selected = false" (for the 1st circle), "selected = false", (for the 2nd circle)
  • Click inside the Rectangle again
  • Change its color to BLUE
  • Just for debugging, it prints "selected == true" (for the rectangle), "selected = false" (for the 1st circle), "selected = false", (for the 2nd circle)
  • Click inside a Circle
  • Change its color to BLUE
  • Just for debugging, it prints "selected == true" (for the rectangle), "selected = true" (for the 1st circle), "selected = false", (for the 2nd circle)
  • The problem is: the rectangle's color turns back to BLACK. It should be still BLUE.

How can i select 2 or more shapes at the same time?

Upvotes: 0

Views: 32

Answers (1)

FredK
FredK

Reputation: 4084

Your "if" clause sets the color of the most recently selected item and sets it as selected; the "else" clause resets all other items as unselected and resets the color.

This is not the correct approach.

You should have a Shape class that holds the image and all of its attributes. One of those attributes would be whether or not that shape is currently selected. Then when you repaint, pass the Graphics to a method in the Shape class that repaints the image as either selected or not selected.

You should set all items as unselected in a separate loop, and that loop would be entered only if the first loop did not determine that the click was in an object.

boolean found = false;
for ( Shape s : images ) {
  if ( click is in s ) {
     s.setSelected(true);
     found = true;
     break;
   }
} 
if ( !found ) {
   // set all images to unselected here
}
repaint();

Upvotes: 2

Related Questions