get Mouse Position - Java Swing

Java examples for Swing:Mouse Event

Description

get Mouse Position

Demo Code


//package com.java2s;

import java.awt.GraphicsConfiguration;
import java.awt.GraphicsDevice;

import java.awt.MouseInfo;
import java.awt.Point;

public class Main {
    public static Point getMousePos(GraphicsDevice dev) {
        Point pos = MouseInfo.getPointerInfo().getLocation();
        GraphicsConfiguration cfg = dev.getDefaultConfiguration();
        int newX = pos.x - cfg.getBounds().x;
        int newY = pos.y - cfg.getBounds().y;
        return new Point(newX, newY);
    }//from  ww  w  .  ja  va2  s . com
}

Related Tutorials