简体   繁体   English

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

[英]Java Swing & AWT - Mouse Position Not Accurate

I have a basic program that renders the position of the mouse on a java.awt.Canvas. 我有一个基本程序,可在java.awt.Canvas上呈现鼠标的位置。 However, the further away the mouse gets from the coordinates 0, 0 (top left of the window) the more inaccurate it becomes. 但是,鼠标离坐标0、0(窗口左上角)越远,它变得越不准确。

The box drawn on the screen (that represents the mouse position) seems to increasingly fall further and further behind where the actual mouse position is. 屏幕上显示的框(代表鼠标位置)似乎越来越落在实际鼠标位置的后面。 Is there something I haven't done right or is this a problem with AWT or Swing. 是否有我做不正确的事情,或者这是AWT或Swing的问题。

Here's my code: 这是我的代码:

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();
    }

}

Mouse Handler class: 鼠标处理程序类:

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();
    }

}

Thanks for any help in advance. 感谢您的任何帮助。

The problem here is that you're setting size of the whole frame, ie 480 - it is the height of window with title bar, this is why actual size of content pane is smaller (~455), then during painting you're down scaling image. 这里的问题是您要设置整个框架的大小,即480-这是带有标题栏的窗口的高度,这就是为什么内容窗格的实际大小较小(〜455),然后在绘制时向下缩放图像。

To fix this - set content pane size to desired one: 要解决此问题-将内容窗格的大小设置为所需的大小:

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

You also have small bug in drawMousePos: 您在drawMousePos中也有一个小错误:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM