Here is the code for my method that is supposed to detect whether or not a mouse click is within a rectangle that I defined.
private boolean mouseOver(int mx,int my,int x, int y, int width, int height){
if((x < mx && mx < x + width)&&(y < my && my < y + height)){
System.out.print(mx+"\n"+my+"\n"+x+"\n"+y+"\n"+width+"\n"+height+"\n");
return true;
}else
System.out.print(mx+"\n"+my+"\n"+x+"\n"+y+"\n"+width+"\n"+height+"\n");
return false;
}
This is where I am defining the rectangles (with text).
public void render(Graphics g){
Font fnt = new Font("arial", 1, 50);
Font fnt1 = new Font("arial", 1, 30);
g.setColor(Color.white);
g.setFont(fnt);
g.drawString("Menu", 240,70);
g.setFont(fnt1);
g.drawString("Play", 270,190);
g.drawRect(210, 150, 200, 64);
g.drawString("Scores", 270,290);
g.drawRect(210, 250, 200, 64);
g.drawString("Quit", 270,390);
g.drawRect(210, 350, 200, 64);
}
Here is where the method is used.
public void mousePressed(MouseEvent e){
int mx = e.getY();
int my = e.getX();
//play button
if (mouseOver(mx ,my ,210, 150, 200, 64)){
game.gameState = STATE.Game;
}
//quit button
if (mouseOver(mx ,my ,210, 350, 200, 64)){
System.exit(1);
}
}
When the method is called I commanded it to spit out the fields it takes, and here is an example.
mx = 185 my = 344 x = 210 y = 350 width = 200 height = 64 That data was from clicking the mouse inside of the rectangle, so it should have returned true if it was working properly. Maybe it's returning the wrong coordinates of the mouse press?
Any ideas? I'm using a mac if that makes any difference.
I solved it. I was sending the x coordinate data to the y value in the equation and visa versa.