Every time i move the position of the button it messes up the drawing and messes up the bounds used to detect if the mouse cursor is on it.
Button Class:
public class GuiButton implements IButton {
private int x, y, id;
private Handler handler;
private boolean hover;
private Rectangle bounds;
private String text;
public GuiButton(Handler handler, String text, int x, int y, int id) {
this.handler = handler;
this.text = text;
this.x = x;
this.y = y;
this.id = id;
bounds = new Rectangle();
this.bounds.x = x;
this.bounds.y = x;
this.bounds.width = 300;
this.bounds.height = 80;
}
public void update() {
if (handler.getMouse().getX() >= bounds.x
&& handler.getMouse().getY() <= y + bounds.y
&& handler.getMouse().getX() <= bounds.width
&& handler.getMouse().getY() <= bounds.height + y) {
hover = true;
} else {
hover = false;
}
}
public void render(Graphics graphics) {
graphics.setColor(Color.BLUE);
graphics.drawRect(bounds.x, bounds.y, bounds.width,
bounds.height);
if (hover) {
graphics.setColor(Color.GRAY);
} else {
graphics.setColor(Color.WHITE);
}
graphics.fillRect(bounds.x, bounds.y, bounds.width, bounds.height);
graphics.setColor(Color.BLACK);
graphics.drawString(text, bounds.x + 100, bounds.y + 60);
if (hover) {
graphics.setColor(Color.WHITE);
graphics.drawString("HOVERING", handler.getMouse().getX() - 4,
handler.getMouse().getY());
}
}
public int getX() {
return x;
}
public int getY() {
return y;
}
public int getID() {
return id;
}
public Rectangle getBounds() {
return bounds;
}
}