package gamegui; import java.awt.FontMetrics; import java.awt.Color; import java.awt.Graphics; import java.awt.Font; public class Button extends Member { private String text; private Font font; public Button(final String newName, final int newX, final int newY, final int newWidth, final int newHeight, final String newText, final Font newFont) { super(newName, newX, newY, newWidth, newHeight); this.text = new String(newText); this.font = newFont; } public void draw(final Graphics g) { final FontMetrics metrics = g.getFontMetrics(this.font); g.setColor(Color.red); g.drawRect(this.getX(), this.getY(), this.getWidth(), this.getHeight()); g.setColor(Color.green); g.setFont(this.font); g.drawString(this.text, this.getX() + (this.getWidth() - metrics.stringWidth(this.text)) / 2, this.getY() + (this.getHeight() + metrics.getHeight()) / 2 - 2); } public boolean isClicked(final int xCoord, final int yCoord) { return xCoord >= this.getX() && this.getX() + this.getWidth() >= xCoord && yCoord >= this.getY() && this.getY() + this.getHeight() >= yCoord; } }