package gamegui; import java.awt.FontMetrics; import java.awt.Image; import java.awt.image.ImageObserver; import java.awt.Graphics; import java.awt.image.BufferedImage; import java.awt.Font; import java.awt.Color; public class Button extends Member { private String text; public Color color; private Font font; private BufferedImage img; private Align alignment; boolean border; 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 = newText; this.color = Color.green; this.font = newFont; this.img = null; this.alignment = Align.Left; this.border = true; } public Button(final String newName, final int newX, final int newY, final int newWidth, final int newHeight, final String newText, final Font newFont, final Color color) { super(newName, newX, newY, newWidth, newHeight); this.text = newText; this.color = color; this.font = newFont; this.img = null; this.alignment = Align.Left; this.border = true; } public Button(final String newName, final int newX, final int newY, final int newWidth, final int newHeight, final String newText, final Font newFont, final Color color, final boolean border) { super(newName, newX, newY, newWidth, newHeight); this.text = newText; this.color = color; this.font = newFont; this.img = null; this.alignment = Align.Left; this.border = border; } public Button(final String newName, final int newX, final int newY, final int newWidth, final int newHeight, final String newText, final Font newFont, final Align alignment) { super(newName, newX, newY, newWidth, newHeight); this.text = newText; this.color = Color.green; this.font = newFont; this.img = null; this.alignment = alignment; this.border = true; } public Button(final String newName, final int newX, final int newY, final int newWidth, final int newHeight, final BufferedImage img) { super(newName, newX, newY, newWidth, newHeight); this.text = ""; this.font = null; this.img = img; this.alignment = Align.Left; this.border = false; } @Override public void draw(final Graphics g) { if (this.img == null) { final FontMetrics metrics = g.getFontMetrics(this.font); if (this.border) { g.setColor(Color.red); g.drawRect(this.getX(), this.getY(), this.getWidth(), this.getHeight()); } g.setColor(this.color); g.setFont(this.font); switch (this.alignment) { case Center: { g.drawString(this.text, this.getX() + (this.getWidth() - metrics.stringWidth(this.text)) / 2, this.getY() + (this.getHeight() + metrics.getHeight()) / 2 - 2); break; } case Right: { g.drawString(this.text, this.getX() + this.getWidth() - metrics.stringWidth(this.text), this.getY() + (this.getHeight() + metrics.getHeight()) / 2 - 2); break; } case Left: { g.drawString(this.text, this.getX(), this.getY() + (this.getHeight() + metrics.getHeight()) / 2 - 2); break; } } } else { g.drawImage(this.img, this.getX() + (this.getWidth() - this.img.getWidth(null)) / 2, this.getY() + (this.getHeight() - this.img.getHeight(null)) / 2 - 2, null); } } }