1 | package gamegui;
|
---|
2 |
|
---|
3 | import java.awt.FontMetrics;
|
---|
4 | import java.awt.Graphics;
|
---|
5 | import java.awt.Font;
|
---|
6 | import java.awt.Color;
|
---|
7 |
|
---|
8 | public class Label extends Member
|
---|
9 | {
|
---|
10 | private String text;
|
---|
11 | public Color color;
|
---|
12 | private Font font;
|
---|
13 | private Align alignment;
|
---|
14 |
|
---|
15 | public Label(final String newName, final int newX, final int newY, final int newWidth, final int newHeight, final String newText, final Font newFont) {
|
---|
16 | super(newName, newX, newY, newWidth, newHeight);
|
---|
17 | this.text = newText;
|
---|
18 | this.color = Color.green;
|
---|
19 | this.font = newFont;
|
---|
20 | this.alignment = Align.Center;
|
---|
21 | }
|
---|
22 |
|
---|
23 | 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) {
|
---|
24 | super(newName, newX, newY, newWidth, newHeight);
|
---|
25 | this.text = newText;
|
---|
26 | this.color = color;
|
---|
27 | this.font = newFont;
|
---|
28 | this.alignment = Align.Center;
|
---|
29 | }
|
---|
30 |
|
---|
31 | 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) {
|
---|
32 | super(newName, newX, newY, newWidth, newHeight);
|
---|
33 | this.text = newText;
|
---|
34 | this.color = Color.green;
|
---|
35 | this.font = newFont;
|
---|
36 | this.alignment = alignment;
|
---|
37 | }
|
---|
38 |
|
---|
39 | public void setText(final String s) {
|
---|
40 | this.text = s;
|
---|
41 | }
|
---|
42 |
|
---|
43 | @Override
|
---|
44 | public void draw(final Graphics g) {
|
---|
45 | final FontMetrics metrics = g.getFontMetrics(this.font);
|
---|
46 | g.setColor(this.color);
|
---|
47 | g.setFont(this.font);
|
---|
48 | switch (this.alignment) {
|
---|
49 | case Center: {
|
---|
50 | g.drawString(this.text, this.getX() + (this.getWidth() - metrics.stringWidth(this.text)) / 2, this.getY() + (this.getHeight() + metrics.getHeight()) / 2 - 2);
|
---|
51 | break;
|
---|
52 | }
|
---|
53 | case Right: {
|
---|
54 | g.drawString(this.text, this.getX() + this.getWidth() - metrics.stringWidth(this.text), this.getY() + (this.getHeight() + metrics.getHeight()) / 2 - 2);
|
---|
55 | break;
|
---|
56 | }
|
---|
57 | case Left: {
|
---|
58 | g.drawString(this.text, this.getX(), this.getY() + (this.getHeight() + metrics.getHeight()) / 2 - 2);
|
---|
59 | break;
|
---|
60 | }
|
---|
61 | }
|
---|
62 | }
|
---|
63 | }
|
---|