1 | ackage gamegui;
|
---|
2 |
|
---|
3 | import java.awt.FontMetrics;
|
---|
4 | import java.awt.Color;
|
---|
5 | import java.awt.Graphics;
|
---|
6 | import java.awt.event.MouseEvent;
|
---|
7 | import java.awt.Font;
|
---|
8 | import java.util.ArrayList;
|
---|
9 |
|
---|
10 | public class RadioGroup extends Member {
|
---|
11 | private ArrayList<RadioButton> buttons;
|
---|
12 | private RadioButton selected;
|
---|
13 | private String text;
|
---|
14 | private Font font;
|
---|
15 |
|
---|
16 | public RadioGroup(final String newName, final int newX, final int newY, final int newWidth, final int newHeight, final String newText, final Font newFont) {
|
---|
17 | super(newName, newX, newY, newWidth, newHeight);
|
---|
18 | this.buttons = new ArrayList<RadioButton>();
|
---|
19 | this.selected = null;
|
---|
20 | this.text = newText;
|
---|
21 | this.font = newFont;
|
---|
22 | }
|
---|
23 |
|
---|
24 | @Override
|
---|
25 | public boolean handleEvent(final MouseEvent e) {
|
---|
26 | if (this.selected != null) {
|
---|
27 | this.selected.clear();
|
---|
28 | }
|
---|
29 | for (int x = 0; x < this.buttons.size(); ++x) {
|
---|
30 | if (this.buttons.get(x).isClicked(e.getX(), e.getY())) {
|
---|
31 | (this.selected = this.buttons.get(x)).setSelected(true);
|
---|
32 | return true;
|
---|
33 | }
|
---|
34 | }
|
---|
35 | return false;
|
---|
36 | }
|
---|
37 |
|
---|
38 | @Override
|
---|
39 | public void draw(final Graphics g) {
|
---|
40 | final FontMetrics metrics = g.getFontMetrics(this.font);
|
---|
41 | g.setColor(Color.green);
|
---|
42 | g.setFont(this.font);
|
---|
43 | g.drawString(this.text, this.getX() - metrics.stringWidth(this.text) - 10, this.getY() + (this.getHeight() + metrics.getHeight()) / 2 - 2);
|
---|
44 | for (int x = 0; x < this.buttons.size(); ++x) {
|
---|
45 | this.buttons.get(x).draw(g);
|
---|
46 | }
|
---|
47 | }
|
---|
48 |
|
---|
49 | @Override
|
---|
50 | public void clear() {
|
---|
51 | for (int x = 0; x < this.buttons.size(); ++x) {
|
---|
52 | this.buttons.get(x).clear();
|
---|
53 | }
|
---|
54 | }
|
---|
55 |
|
---|
56 | public void add(final RadioButton aButton) {
|
---|
57 | this.buttons.add(aButton);
|
---|
58 | }
|
---|
59 |
|
---|
60 | public RadioButton getButton(final String aName) {
|
---|
61 | for (int x = 0; x < this.buttons.size(); ++x) {
|
---|
62 | if (this.buttons.get(x).getName().equals(aName)) {
|
---|
63 | return this.buttons.get(x);
|
---|
64 | }
|
---|
65 | }
|
---|
66 | return null;
|
---|
67 | }
|
---|
68 |
|
---|
69 | public String getSelected() {
|
---|
70 | if (this.selected != null) {
|
---|
71 | return this.selected.getName();
|
---|
72 | }
|
---|
73 | return "None";
|
---|
74 | }
|
---|
75 |
|
---|
76 | public void setSelected(final String button) {
|
---|
77 | this.clear();
|
---|
78 | for (int x = 0; x < this.buttons.size(); ++x) {
|
---|
79 | if (this.buttons.get(x).getName().equals(button)) {
|
---|
80 | this.buttons.get(x).setSelected(true);
|
---|
81 | }
|
---|
82 | }
|
---|
83 | }
|
---|
84 | }
|
---|