Java Swing & AWT – Mouse Position Not Accurate Code Answer

Hello Developer, Hope you guys are doing great. Today at Tutorial Guruji Official website, we are sharing the answer of Java Swing & AWT – Mouse Position Not Accurate without wasting too much if your time.

The question is published on by Tutorial Guruji team.

I have a basic program that renders the position of the mouse on a 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.

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.

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.

Answer

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.

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:

(handler.x + x) <= image.getWidth() should use < instead of <=

We are here to answer your question about Java Swing & AWT – Mouse Position Not Accurate - If you find the proper solution, please don't forgot to share this with your team members.

Related Posts

Tutorial Guruji