1 | package gamegui;
|
---|
2 |
|
---|
3 | import java.awt.FontMetrics;
|
---|
4 | import java.awt.Image;
|
---|
5 | import java.awt.image.ImageObserver;
|
---|
6 | import java.awt.Graphics;
|
---|
7 | import java.awt.image.BufferedImage;
|
---|
8 | import java.awt.Font;
|
---|
9 | import java.awt.Color;
|
---|
10 |
|
---|
11 | public class Button extends Member
|
---|
12 | {
|
---|
13 | private String text;
|
---|
14 | public Color color;
|
---|
15 | private Font font;
|
---|
16 | private BufferedImage img;
|
---|
17 | private Align alignment;
|
---|
18 | boolean border;
|
---|
19 |
|
---|
20 | public Button(final String newName, final int newX, final int newY, final int newWidth, final int newHeight, final String newText, final Font newFont) {
|
---|
21 | super(newName, newX, newY, newWidth, newHeight);
|
---|
22 | this.text = newText;
|
---|
23 | this.color = Color.green;
|
---|
24 | this.font = newFont;
|
---|
25 | this.img = null;
|
---|
26 | this.alignment = Align.Left;
|
---|
27 | this.border = true;
|
---|
28 | }
|
---|
29 |
|
---|
30 | 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) {
|
---|
31 | super(newName, newX, newY, newWidth, newHeight);
|
---|
32 | this.text = newText;
|
---|
33 | this.color = color;
|
---|
34 | this.font = newFont;
|
---|
35 | this.img = null;
|
---|
36 | this.alignment = Align.Left;
|
---|
37 | this.border = true;
|
---|
38 | }
|
---|
39 |
|
---|
40 | 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) {
|
---|
41 | super(newName, newX, newY, newWidth, newHeight);
|
---|
42 | this.text = newText;
|
---|
43 | this.color = color;
|
---|
44 | this.font = newFont;
|
---|
45 | this.img = null;
|
---|
46 | this.alignment = Align.Left;
|
---|
47 | this.border = border;
|
---|
48 | }
|
---|
49 |
|
---|
50 | 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) {
|
---|
51 | super(newName, newX, newY, newWidth, newHeight);
|
---|
52 | this.text = newText;
|
---|
53 | this.color = Color.green;
|
---|
54 | this.font = newFont;
|
---|
55 | this.img = null;
|
---|
56 | this.alignment = alignment;
|
---|
57 | this.border = true;
|
---|
58 | }
|
---|
59 |
|
---|
60 | public Button(final String newName, final int newX, final int newY, final int newWidth, final int newHeight, final BufferedImage img) {
|
---|
61 | super(newName, newX, newY, newWidth, newHeight);
|
---|
62 | this.text = "";
|
---|
63 | this.font = null;
|
---|
64 | this.img = img;
|
---|
65 | this.alignment = Align.Left;
|
---|
66 | this.border = false;
|
---|
67 | }
|
---|
68 |
|
---|
69 | @Override
|
---|
70 | public void draw(final Graphics g) {
|
---|
71 | if (this.img == null) {
|
---|
72 | final FontMetrics metrics = g.getFontMetrics(this.font);
|
---|
73 | if (this.border) {
|
---|
74 | g.setColor(Color.red);
|
---|
75 | g.drawRect(this.getX(), this.getY(), this.getWidth(), this.getHeight());
|
---|
76 | }
|
---|
77 | g.setColor(this.color);
|
---|
78 | g.setFont(this.font);
|
---|
79 | switch (this.alignment) {
|
---|
80 | case Center: {
|
---|
81 | g.drawString(this.text, this.getX() + (this.getWidth() - metrics.stringWidth(this.text)) / 2, this.getY() + (this.getHeight() + metrics.getHeight()) / 2 - 2);
|
---|
82 | break;
|
---|
83 | }
|
---|
84 | case Right: {
|
---|
85 | g.drawString(this.text, this.getX() + this.getWidth() - metrics.stringWidth(this.text), this.getY() + (this.getHeight() + metrics.getHeight()) / 2 - 2);
|
---|
86 | break;
|
---|
87 | }
|
---|
88 | case Left: {
|
---|
89 | g.drawString(this.text, this.getX(), this.getY() + (this.getHeight() + metrics.getHeight()) / 2 - 2);
|
---|
90 | break;
|
---|
91 | }
|
---|
92 | }
|
---|
93 | }
|
---|
94 | else {
|
---|
95 | g.drawImage(this.img, this.getX() + (this.getWidth() - this.img.getWidth(null)) / 2, this.getY() + (this.getHeight() - this.img.getHeight(null)) / 2 - 2, null);
|
---|
96 | }
|
---|
97 | }
|
---|
98 | }
|
---|