Welcome to the CSC Q&A, where you can get help (and share your knowledge) about computer science!

How do I make my mouse click only work on a certain spot, such as a rectangle?

+4 votes
asked Feb 11, 2015 in (Winter 14) by Kevin Barbian (100 points)

1 Answer

+4 votes
you use this boolean method from lab6, you need to assign 2 interger clickX and clickY using panel.getMouseClickX() and panel.getMouseClickY() and then pass the x,y, width, height from your rectangle

public static boolean checkMouse(int clickX, int clickY,int x, int y, int width, int height) {
    if (clickX >= x && clickX <= (x + width)
            && clickY >= y && clickY <= (y +height) ) {
            return true;
            } else {
            return false;
            }
}
answered Feb 11, 2015 by Hoang Nguyen (100 points)
...