Having trouble with custom button

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;
	}

}

Shouldn’t this:

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;
      }
   }

be this:

public void update() {
      if (handler.getMouse().getX() >= bounds.x
            && handler.getMouse().getY() >= bounds.y
            && handler.getMouse().getX() <= bounds.x + bounds.width
            && handler.getMouse().getY() <= bounds.height + bounds.y) {
         hover = true;
      } else {
         hover = false;
      }
   }

Okay, changed that and it’s still not working properly.

I have a different issue to address.

Your class is completely inconsistent in how you handle positioning. IMHO, the class should look like this:

public class GuiButton implements IButton {

   private int 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.id = id;

      bounds = new Rectangle(x, y, 300, 80);
   }

   public void update() {
      if (handler.getMouse().getX() >= bounds.x
            && handler.getMouse().getY() >= bounds.y
            && handler.getMouse().getX() <= bounds.width + bounds.x
            && handler.getMouse().getY() <= bounds.height + bounds.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 bounds.x;
   }

   public int getY() {
      return bounds.y;
   }

   public int getID() {
      return id;
   }

   public Rectangle getBounds() {
      return bounds;
   }

}

Ah i see, thanks so much for the help!

Tip: If you’re already using Rectangle, just use bounds.contains() instead of doing the intersection test yourself (don’t give yourself opportunities to be wrong):


public void update() {
    hover = bounds.contains(handler.getMouse().getX(), handler.getMouse().getY());
}

Dang didn’t think of that. thanks for the tip!