java - MouseListener 没有给出准确的鼠标位置
问题描述
我试图在屏幕上出现一个圆圈并跟随鼠标。(最终我将把它变成一个带有光线投射的游戏)我正在使用 MouseMotionListener 并尝试使用 mouseMoved 方法在我的 JPanel 中获取准确的鼠标位置。问题是我将鼠标在屏幕上移动得越远,它变得越不准确。当我的鼠标到达底部时,它正在上方约 20 像素处绘制圆圈。这不是一件落后的事情,因为它永远不会赶上,它总是比它应该在的位置高几个像素。
我尝试使用从 MouseEvents 调用的不同方法,并尝试使用 MousePointerInfo,但没有一个能正常工作。当我将 JFrame 设置为未装饰时,它似乎确实有效,但显然这对于程序看起来并不好,因此我想避免这种情况。
public class Driver {
public static void main(String[] args) {
JFrame frame = new JFrame("Moonlight");
frame.setSize(700, 700);
frame.setLocation(350, 50);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setContentPane(new MoonlightPanel());
frame.setVisible(true);
}
}
public class Panel extends JPanel {
private BufferedImage myImage;
private Graphics myBuffer;
private Timer t;
public Panel () {
myImage = new BufferedImage(700, 700, BufferedImage.TYPE_INT_RGB);
myBuffer = myImage.getGraphics();
t = new Timer(0, new Listener());
t.start();
addMouseMotionListener(new Mouse());
}
private class Listener implements ActionListener {
public void actionPerformed(ActionEvent e) {
drawBackground();
/*try {
Point pos = getMousePosition();
myBuffer.setColor(Color.WHITE);
myBuffer.fillOval(pos.x - 10, pos.y - 10, 20, 20);
}
catch(NullPointerException en) {}*/
repaint();
}
}
private class Mouse implements MouseMotionListener {
public void mouseMoved(MouseEvent e) {
Point pos = new Point(e.getX(), e.getY());
System.out.println(pos);
myBuffer.setColor(Color.BLUE);
myBuffer.fillOval(pos.x - 10, pos.y - 10, 20, 20);
}
public void mouseDragged(MouseEvent e) {}
}
public void drawBackground() {
setBackground(Color.BLACK);
}
public void paintComponent(Graphics g) {
g.drawImage(myImage, 0, 0, getWidth(), getHeight(), null);
}
}
解决方案
您的代码比它需要的复杂得多。Panel
类成员是不必要的。您需要做的就是将鼠标位置保存在mouseMoved()
方法中 - 在类成员变量中 - 并在paintComponent()
方法中引用它以绘制蓝色圆圈。下面的代码是一个精简的版本,它在屏幕上显示一个跟随鼠标指针的蓝色圆圈。
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class MoonLite extends JPanel implements MouseMotionListener {
private Point pt;
public MoonLite() {
setBackground(Color.BLACK);
setForeground(Color.BLUE);
addMouseMotionListener(this);
}
public void mouseMoved(MouseEvent e) {
pt = e.getPoint();
repaint();
}
public void mouseDragged(MouseEvent e) {
// Do nothing.
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
if (pt != null) {
g.fillOval(pt.x - 10, pt.y - 10, 20, 20);
}
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
JFrame frame = new JFrame("Moonlight");
frame.setSize(700, 700);
frame.setLocation(350, 50);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setContentPane(new MoonLite());
frame.setVisible(true);
}
});
}
}
推荐阅读
- javascript - 如何在JS中显示星期几?
- git - Github & Release 分支显示不应该存在的差异
- amazon-web-services - Does cost of EC2 on AWS increase at the same rate as user count?
- python - 使用 PIPE 打开的子进程仍在从终端读取输入
- python - 使用 Python 访问 Azure 上的 SQL Server SQL 数据库出现错误
- sql - 如何在 Json Auto 中获取空数组 - SQL Server
- ios - 当我单击我使用 Swift 和 xcode 构建的应用程序中的按钮时,它们不起作用
- python - 使用 print r"\" (Python) 仅打印一个斜杠
- go - golang 编译器何时可以重新排序命令以及同步原语如何影响它?
- apache-spark - 在 spark-shell 中运行带有换行符的 scala 脚本