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