[英]Trouble with comparisons to make a dot change color when clicked
I'm having trouble with making my comparisons. 我在进行比较时遇到麻烦。 The project is supposed to draw a square from user input, and then wherever the user clicks would draw a dot of varying colors there.
该项目应该从用户输入中绘制一个正方形,然后用户单击的任何位置都将在其中绘制一个不同颜色的点。 For instance, if they click inside the square it should make a red circle, if it's on the edge of the square it makes a green circle, and if outside it makes a blue circle.
例如,如果他们在正方形内单击,则应形成一个红色圆圈,如果在正方形边缘上,则应形成一个绿色圆圈,在外部,则应形成一个蓝色圆圈。 At the moment my program draws red and blue circles, but no greens.
目前,我的程序绘制了红色和蓝色的圆圈,但没有绿色。 In fact it draws red circles when it's above a certain point as well.
实际上,当它高于某个点时,它也会绘制红色圆圈。
public class Square extends GraphicsProgram {
// Instance variables
private int side; // the length of a side
private int anchorX; // the X value at the upper left corner
private int anchorY; // the Y value at the upper left corner
public Square(int x, int y, int side) {
anchorX = x;
anchorY = y;
this.side = side;
}
// mouseClicked method
public void mouseClicked(MouseEvent e) {
// Find the location where the mouse was clicked
int x = e.getX();
int y = e.getY();
// boolean variables to indicate location
boolean isInside = false;
boolean isOutside = false;
boolean isOnEdge = false;
if (x > anchorX + 1 && anchorY + 1 < y && anchorY + side + 1 > y) {
isInside = true;
}
if (x > anchorX + side + 1 && anchorY + side + 1 < y && x > anchorX + side - 1 & y > anchorY + side - 1) {
isOutside = true;
}
/*** NOTE: There a hard, and an easy way to do this! ***/
if (anchorX - 1 <= x && x <= anchorX - 3 && anchorY - 1 <= y && anchorY + side - 3 >= y) {
isOnEdge = true;
}
if (isOnEdge == true) {
System.out.println("(" + x + ", " + y + ") is on the square");
GOval circle = new GOval(x - 2, y - 2, 4, 4);
circle.setFillColor(Color.GREEN);
circle.setFilled(true);
add(circle);
}
else if (isInside == true) {
System.out.println("(" + x + ", " + y + ") is inside the square");
GOval circle = new GOval(x - 2, y - 2, 4, 4);
circle.setFillColor(Color.RED);
circle.setFilled(true);
add(circle);
}
else if (isOutside == true) {
System.out.println("(" + x + ", " + y + ") is outside the square");
GOval circle = new GOval(x - 2, y - 2, 4, 4);
circle.setFillColor(Color.BLUE);
circle.setFilled(true);
add(circle);
}
}
}
We were given a hint on how to do the (x,y) locations of the square as 我们得到了关于如何做正方形的(x,y)位置的提示
"For example, the left edge of the square has: “例如,正方形的左边缘有:
x values in the range: anchorX-1 ≤ x ≤ anchorX+1, and y values in the range: anchorY-1 ≤ y ≤ anchorY+side+1. x值范围:anchorX-1≤x≤anchorX + 1,y值范围:anchorY-1≤y≤anchorY + side + 1。
Which would mean that if we had a square with anchorX 50, anchorY 100 and side 60, coordinates like (49-51, 99-161) would be considered on the edge of the left side. 这意味着如果我们有一个带有anchorX 50,anchorY 100和侧面60的正方形,则将在左侧边缘考虑像(49-51,99-161)之类的坐标。
I think the problem is a little confusion with the bounds that make up the square - understandable as it's difficult to visualize. 我认为问题是与构成正方形的边界有些混乱-可以理解,因为很难可视化。
if (x > anchorX+1 && anchorY+1 < y && anchorY+side+1 > y) {
isInside = true;
}
Right now this: 现在这个:
x > anchorX+1
x > anchorX+1
的右侧 anchorY+1 < y
anchorY+1 < y
的下方anchorY+1 < y
anchorY+side+1 > y
anchorY+side+1 > y
上方anchorY+side+1 > y
The problems with that are: 问题在于:
anchorY+side+1 > y
will include the edge - the +1 will go over anchorY+side+1 > y
将包含边线-+1将结束 A working solution would be: 一个可行的解决方案是:
if (x > anchorX+1 && y > anchorY+1 && x < anchorX+side-1 && y < anchorY+side-1) {
isInside = true;
}
if (x > anchorX+side+1 && anchorY+side+1 < y && x > anchorX+side-1 & y > anchorY+side-1) {
isOutside = true;
}
Right now this: 现在这个:
x > anchorX+side+1
x > anchorX+side+1
的右侧 anchorY+side+1 < y
anchorY+side+1 < y
下方anchorY+side+1 < y
x > anchorX+side-1
x > anchorX+side-1
的右侧 y > anchorY+side-1
y > anchorY+side-1
之上 The problems with that are: 问题在于:
x > anchorX+side-1
and y > anchorY+side-1
will include the edge in the area considered outside. x > anchorX+side-1
和y > anchorY+side-1
将在考虑为外部的区域中包含边。 x > anchorX+side+1
and x > anchorX+side-1
do practically the same. x > anchorX+side+1
和x > anchorX+side-1
几乎相同。 &
symbol I'm not sure was intentional or not. &
符号,但不确定是否故意。 The difference is &&
only runs if the previous are all true, while &
always runs. &&
仅在前面的条件都为true时运行,而&
始终运行。 &&
is therefore faster and usually preferred. &&
更快,通常是首选。 A working solution would be: 一个可行的解决方案是:
if (x < anchorX-1 || y < anchorY-1 || x > anchorX+side+1 || y > anchorY+side+1) {
isOutside = true;
}
This is the more tricky one 这是比较棘手的一个
if (anchorX-1 <= x && x <= anchorX-3 && anchorY-1 <= y && anchorY+side-3 >= y) {
isOnEdge = true;
}
Right now this: 现在这个:
anchorX-1 <= x
anchorX-1 <= x
左侧的右侧anchorX-1 <= x
x <= anchorX-3
x <= anchorX-3
anchorY-1 <= y
anchorY-1 <= y
的顶部下方anchorY-1 <= y
anchorY+side-3 >= y
anchorY+side-3 >= y
上方的一个点上方anchorY+side-3 >= y
The problems with that are: 问题在于:
anchorX-1 <= x
and x <= anchorX-3
are contridictary, so there is no x such that both are true, so the whole thing must be false. anchorX-1 <= x
和x <= anchorX-3
是矛盾的,因此不存在x都为真的x,因此整个事物必须为假。 &&
instead of ||
&&
而不是||
(OR), you it will only be true when the whole thing is true - it must be within both edges at the same time - meaning only the corner would be counted. This could be fixed to create a working solution for this one, but defining all the conditions manually would take a lot of work. 可以修复此问题以为此创建一个可行的解决方案,但是手动定义所有条件将需要大量工作。 This is what it refers to in the easy and hard comment.
这就是在简单而艰苦的评论中所指的含义。
As a hint, you can use the fact you already know if it's in the square to simplify it. 提示一下,您可以使用已经知道它是否在方格中的事实来简化它。 I suggest thinking on it.
我建议考虑一下。
If you're struggling to comprehend all this, I suggest drawing it out manually and writing down all the values onto it - it's much easier than trying to do it in your head. 如果您难以理解所有这些,我建议手动将其绘制出来并将所有值写下来-比尝试在脑海中做起来容易得多。 It gets easier the more you do it.
您做得越多,就越容易。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.