这种简单的if-comparison不起作用,我不确定原因。
代码:
public abstract class Button extends Drawable_object {
...
@Override
public void update()
{
super.update();
mouseOver_last = mouseOver;
double mx = Game_logic.get_mouse_x();
double my = Game_logic.get_mouse_y();
//the not working statement check:
if ((mx <= x + ((double)width / 2))&&
(mx >= x - ((double)width / 2))&&
(my >= y - ((double)height / 2))&&
(my <= y + ((double)height / 2)))
{ mouseOver = true;}
else mouseOver = false;
....
}
虽然Game_logic.get_mouse_x()和y是静态方法:
public class Game_logic {
...
public static double get_mouse_x() { return mouse_x; }
public static double get_mouse_y() { return mouse_y; }
}
这些是由我的主要运行类中的鼠标适配器设置的:
public Board() throws IOException, URISyntaxException {
...
private class MAdapter2 extends MouseAdapter {
@Override
public void mouseMoved(MouseEvent e) {
Game_logic.set_mouse_x(e.getX());
Game_logic.set_mouse_y(e.getY());
}
}
问题是,当我在屏幕上绘制x - width / 2,mx和x + width / 2(与y相同)时,将鼠标放在我想要的位置看起来声明必须为true,但是mouseOver
仍然是假的。我该如何解决这个问题?
答案 0 :(得分:2)
这不会直接回答您的问题,我只是表明您的 if 声明可能会出现。我相信你应该打印 mx,我的并将它们与你期望它们的价值进行比较。这可能是您的问题所在。显然你也应该检查你选择的 x,y,width,height 。
public static void main(String[] args) {
boolean mouseOver;
int width = 50;
int height = 50;
double x = 40;
double y = 40;
double mx = 40;
double my = 40;
if ((mx <= x + ((double) width / 2)) && (mx >= x - ((double) width / 2))
&& (my >= y - ((double) height / 2))
&& (my <= y + ((double) height / 2))) {
mouseOver = true;
} else {
mouseOver = false;
}
System.out.println(mouseOver);
}
请打印出 mx,my,x,y,width,heigh 的所有值,并将其发布并与您的想法进行比较。