MouseListener没有提供准确的鼠标位置

时间:2019-05-11 05:36:38

标签: java mouselistener mousemotionlistener

我正在尝试在屏幕上出现一个圆圈并跟随鼠标四处移动。 (最终,我将其转换为带有光线投射的游戏)我正在使用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);
    }
}

1 个答案:

答案 0 :(得分:1)

您的代码比需要的复杂得多。 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);
            }
        });
    }
}