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