【问题标题】:Cannot find the coordinates of the mouse找不到鼠标坐标
【发布时间】:2017-04-28 06:58:28
【问题描述】:

我对代码非常陌生,目前正在制作我的第一个项目(只是一个简单的游戏),这里是一些代码的快照。上。向下、向左和向右按钮工作得很好,但是 'mouse = new Point(e.getX(), e.getY(), -25);' 行尽管放入了“public Point mouse;”,但似乎没有找到鼠标的坐标。有什么建议吗?

public Rectangle character;

public int charW = 24;
public int charH = 36;

public boolean right = false;
public boolean left = false;
public boolean up = false;
public boolean down = false;
public boolean mouseActive = false;

/**
 *
 */
public Point mouse;

public Keying(Display f, Images i){
    character = new Rectangle(180, 180, charW, charH); 

    f.addKeyListener(new KeyAdapter(){
        public void keyPressed(KeyEvent e){
            if(e.getKeyCode() == KeyEvent.VK_D){
                mouseActive = false;
                right = true;
                character.x += 1;
            }
            if(e.getKeyCode() == KeyEvent.VK_A){
                mouseActive = false;
                left = true;
                character.x -= 1;
            }
            if(e.getKeyCode() == KeyEvent.VK_W){
                mouseActive = false;
                up = true;
                character.y -= 1;
            }
            if(e.getKeyCode() == KeyEvent.VK_S){
                mouseActive = false;
                down = true;
                character.y += 1;
            }
            if(e.getKeyCode() == KeyEvent.VK_M){
                mouseActive = true;
            }
        }

        public void keyReleased(KeyEvent e){
            if(e.getKeyCode() == KeyEvent.VK_D){
                right = false;
            }
            if(e.getKeyCode() == KeyEvent.VK_A){
                left = false;
            }
            if(e.getKeyCode() == KeyEvent.VK_W){
                up = false;
            }
            if(e.getKeyCode() == KeyEvent.VK_S){
                down = false;
            }
        }
    });

    f.addMouseMotionListener(new MouseMotionAdapter() {

        public void mouseMoved(MouseEvent e){
            mouse = new Point(e.getX(), e.getY(), -25);
            if(mouseActive){
                character.x = mouse.x;
                character.y = mouse.y;
            }
            repaint();
        }           
    });
}    

public void paintComponent(Graphics g){
    super.paintComponent(g);
    this.setBackground(Color.BLACK);
    g.setColor(Color.RED);
    g.fillRect(character.x, character.y, character.width, character.height);

    if(right){
        character.x += 1;
    }
    if(left){
        character.x -= 1;
    }
    if(up){
        character.y -= 1;
    }
    if(down){
        character.y += 1;
    }
    repaint();
}

【问题讨论】:

    标签: java


    【解决方案1】:

    MouseEvent 对象有一个名为getPoint() 的方法。这将允许您获得准确的Point,而不必使用 X 和 Y 值创建一个新的。

    所以,换行试试:

    mouse = new Point(e.getX(), e.getY(), -25);

    到以下:

    mouse = e.getPoint();

    MouseEventhere 的 JavaDoc 中提供了所有这些信息

    【讨论】:

    • 反正我自己还是自己做的,和你做的有点不同,但效果一样好,谢谢!
    • 别担心!祝你的项目好运:)
    • 谢谢!到目前为止一切顺利
    【解决方案2】:

    我自己设法做到了,但如果有人想知道我做了什么,这是我使用的代码行 -

        public void mouseMoved(MouseEvent e){
                int mouseX = e.getX();
                int mouseY = e.getY();
                mouse = new Point(mouseX, mouseY);
    

    【讨论】:

      猜你喜欢
      • 2014-06-18
      • 2013-04-30
      • 1970-01-01
      • 1970-01-01
      • 2016-08-11
      • 1970-01-01
      • 1970-01-01
      • 2017-02-08
      • 2016-09-16
      相关资源
      最近更新 更多