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