Java Swing和AWT-鼠标位置不准确

时间:2018-08-19 16:21:29

标签: java swing awt

我有一个基本程序,可将鼠标的位置呈现在java.awt.Canvas上。但是,鼠标离坐标0、0(窗口左上角)越远,它变得越不准确。

在屏幕上绘制的框(代表鼠标位置)似乎越来越落后于实际鼠标位置。有没有我做不正确的事情,还是AWT或Swing有问题。

这是我的代码:

import java.awt.Canvas;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.image.BufferStrategy;
import java.awt.image.BufferedImage;
import java.awt.image.DataBufferInt;

import javax.swing.JFrame;

public class Main extends Canvas implements Runnable {
    private static final long serialVersionUID = 1L;

    private BufferedImage image;
    private int[] pixels;

    private static final MouseHandler handler = new MouseHandler();

    public Main() {
        image = new BufferedImage(640, 480, BufferedImage.TYPE_INT_RGB);
        pixels = ((DataBufferInt)image.getRaster().getDataBuffer()).getData();
        initWindow();

        addMouseMotionListener(handler);

        start();
    }

    private void initWindow() {
        Dimension d = new Dimension(image.getWidth(), image.getHeight());
        JFrame f = new JFrame("Mouse Pointer");
        f.setPreferredSize(d);
        f.setMinimumSize(d);
        f.setMaximumSize(d);

        f.setVisible(true);
        f.setResizable(false);
        f.add(this);
        f.pack();
        f.setLocationRelativeTo(null);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    private void start() {
        new Thread(this).start();
    }

    private void render() {
        BufferStrategy bs = getBufferStrategy();
        if(bs == null) {
            createBufferStrategy(3);
            return;
        }

        clearScreen();
        drawMousePos();

        Graphics g = bs.getDrawGraphics();
        g.drawImage(image, 0, 0, getWidth(), getHeight(), null);
        g.dispose();
        bs.show();
    }

    public void run() {
        while(true) {
            render();
        }
    }

    private void clearScreen() {
        for(int i = 0; i < pixels.length; i++) pixels[i] = 0;
    }

    //draw 3 by 3 box around mouse position
    private void drawMousePos() {
        for(int y = -1; y <= 1; y++) {
            for(int x = -1; x <= 1; x ++) {
                if((handler.x + x) >= 0 && 
                        (handler.x + x) <= image.getWidth() && 
                        (handler.y + y) >= 0 && 
                        (handler.y + y) <= image.getHeight())
                    pixels[(handler.x + x) + (handler.y + y) * image.getWidth()] = 0xff00ff00;
            }
        }
    }

    public static void main(String[] args) {
        new Main();
    }

}

鼠标处理程序类:

import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;

public class MouseHandler implements MouseMotionListener {

    public int x, y;

    public void mouseDragged(MouseEvent e) {
        x = e.getX();
        y = e.getY();
    }

    public void mouseMoved(MouseEvent e) {
        x = e.getX();
        y = e.getY();
    }

}

谢谢您的帮助。

1 个答案:

答案 0 :(得分:2)

这里的问题是您要设置整个框架的大小,即480-这是带有标题栏的窗口的高度,这就是内容窗格的实际大小较小的原因(〜 455),然后在绘画过程中缩小图像的比例。

要解决此问题-将内容窗格的大小设置为所需的大小:

f.getContentPane().setPreferredSize(d);
f.getContentPane().setMinimumSize(d);
f.getContentPane().setMaximumSize(d);

您在drawMousePos中也有一个小错误:

(handler.x + x) <= image.getWidth()应该使用<而不是<=