【问题标题】:Getting coordinates of mouse click on grid easy java StdDraw获取鼠标单击网格的坐标简单java StdDraw
【发布时间】:2014-06-21 05:49:12
【问题描述】:

好的,所以我试图获取单击鼠标的正方形的网格上的坐标(为什么我将其转换为 int),这给了我鼠标的当前位置,但是,我想要之后的位置点击一下,当它悬停时什么也没有发生。

我需要做什么?

while (gameOver==false){
    mouseX= (int) StdDraw.mouseX();
    mouseY=(int) StdDraw.mouseY();
    game.update(mouseX, mouseY);
}

现在我有

    public void mouseReleased(MouseEvent e){
       int mouseX = e.getX();
       int mouseY = e.getY();
               synchronized (mouseLock) {
    mousePressed = false;
}
}
public void run(){
    print();
    boolean gameOver=false;
    int mouseX,mouseY;
    StdDraw.setCanvasSize(500, 500);
    StdDraw.setXscale(0, game.gettheWidth());
    StdDraw.setYscale(0, game.gettheHeight());
    game.update(-1,-1);
    while (gameOver==false){

            mouseReleased(????)
            game.update(mouseX, mouseY);
        }       

}

还是不行

这些都没有意义,

谁能给我一个例子,让我得到 x 和 y 坐标然后打印它们? 我希望 mouseX 和 mouseY 成为 mouseclick 的坐标。上网查了一下,其他的问题都看不懂,估计是和mouseevent有关吧?

【问题讨论】:

    标签: java mouseevent stdin stddraw


    【解决方案1】:

    StdDraw 实现了一个 MouseListener。重载 mouseReleased 方法来设置 mouseX 和 mouseY 变量。

    要重载,请按照需要运行的方式重写方法:

    int mouseX = 0;
    int mouseY = 0;
    public static void main(String[] args) {
        //do stuff
        //...
        while (gameOver == false) {
            //because mouseX and mouseY only change when the mouse button is released
            //they will remain the same until the user clicks and releases the mouse button
            game.update(mouseX, mouseY);
        }
    }
    
    //mouseReleased happens whenever the user lets go of the mouse button
    @Override
    public void mouseReleased(MouseEvent e) {
        //when the user lets go of the button, send the mouse coordinates to the variables.
        mouseX = e.getX();
        mouseY = e.getY();
        synchronized (mouseLock) {
            mousePressed = false;
        }
    }
    

    例如,mouseX 和 mouseY 都是 0。我在 5, 6 处单击鼠标,拖动它,然后在 120, 50 处释放鼠标。调用 mouseReleased 并将 mouseX 更改为 120,将 mouseY 更改为 50。同时,game.update(0,0) 一直在发生。现在变成了 game.update(120, 50),并且在我再次释放鼠标按钮之前将保持这种状态。

    打印鼠标坐标:

    @Override
    public void mouseReleased(MouseEvent e) {
        //when the user lets go of the button, send the mouse coordinates to the variables.
        System.out.println("mouse x coord = " + e.getX());
        System.out.println("mouse y coord = " + e.getY());
        synchronized (mouseLock) {
            mousePressed = false;
        }
    }
    

    【讨论】:

    • “重载mousereleased方法”是什么意思?
    • 你写一个新版本吧。我已经编辑了我的答案以包含一个示例。
    • 我看不到,假设你还在打字?非常感谢
    • while (gameOver==false){ @Override public void mouseReleased(MouseEvent e){ mouseX = e.getX(); mouseY = e.getY(); } game.update(mouseX, mouseY); }
    • 不,你不需要把方法放到你的游戏循环中;只要用户松开鼠标按钮,就会调用mouseReleased 方法。您的游戏循环将与您的问题完全相同,减去设置 mouseXmouseY 的行
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-02-14
    • 2010-10-24
    • 1970-01-01
    • 2015-06-29
    • 2017-12-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多