同时选择两个或多个形状
Selecting two or more shapes at the same time
我被这个问题困住了:
当我在一个形状内部单击时(有一个矩形和圆形的数组列表)我 select 它(只是为了调试,将其颜色更改为蓝色)。因此,如果我在外面单击,在空白 space 中,我将取消 select 它(只是为了调试,将其颜色更改为以前的颜色)。
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.
我怎样才能 select 同时有 2 个或更多形状?
你的"if"子句设置了最近选中的项目的颜色,并将其设置为选中状态; "else" 子句将所有其他项目重置为未选中状态并重置颜色。
这不是正确的方法。
您应该有一个 Shape class 来保存图像及其所有属性。其中一个属性是当前是否选择了该形状。然后,当您重新绘制时,将图形传递给 Shape class 中的方法,该方法将图像重新绘制为选中或未选中。
您应该在一个单独的循环中将所有项目设置为未选中,并且只有在第一个循环没有确定点击是在一个对象中时才会进入该循环。
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();
我被这个问题困住了:
当我在一个形状内部单击时(有一个矩形和圆形的数组列表)我 select 它(只是为了调试,将其颜色更改为蓝色)。因此,如果我在外面单击,在空白 space 中,我将取消 select 它(只是为了调试,将其颜色更改为以前的颜色)。
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.
我怎样才能 select 同时有 2 个或更多形状?
你的"if"子句设置了最近选中的项目的颜色,并将其设置为选中状态; "else" 子句将所有其他项目重置为未选中状态并重置颜色。
这不是正确的方法。
您应该有一个 Shape class 来保存图像及其所有属性。其中一个属性是当前是否选择了该形状。然后,当您重新绘制时,将图形传递给 Shape class 中的方法,该方法将图像重新绘制为选中或未选中。
您应该在一个单独的循环中将所有项目设置为未选中,并且只有在第一个循环没有确定点击是在一个对象中时才会进入该循环。
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();