Question

I have a game where you click on a panel, and yes the panel is getting the action command (tested by printing words when i click the panel). How can you check if Point MosPos = MouseInfo.getPointerInfo().getLocation(); if between two points.

@Override
public void mouseReleased(MouseEvent event) {
    if (event.getSource().equals(panel)) {
        Point MosPos = MouseInfo.getPointerInfo().getLocation();
        if (MosPos >= new Point(0, 0) && MosPos <= new Point(100, 100)) {
            System.out.println("working.");
        }
    }
}

Any suggestions?

Was it helpful?

Solution

Just do

int x = event.getX();
int y = event.getY();

if you want a point:

Point p = event.getPoint();

then to compare:

int start = 0;
int end = 100;

if(x >= start && x <= end &&
    y >= start && y <= end)
    System.out.println("working");
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top