package gamegui; import java.awt.FontMetrics; import java.awt.Graphics; import java.awt.Font; import java.awt.Color; public class Label extends Member { private String text; public Color color; private Font font; private Align alignment; public Label(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.alignment = Align.Center; } public Label(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.alignment = Align.Center; } public Label(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 = new Color(0, 160, 0); this.font = newFont; this.alignment = alignment; } public String getText() { return this.text; } public void setText(final String s) { this.text = s; } @Override public void draw(final Graphics g) { final FontMetrics metrics = g.getFontMetrics(this.font); 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; } } } }