1 | package gamegui;
|
---|
2 |
|
---|
3 | import java.awt.FontMetrics;
|
---|
4 | import java.awt.Color;
|
---|
5 | import java.awt.Graphics;
|
---|
6 | import java.awt.Font;
|
---|
7 |
|
---|
8 | public class RadioButton extends Member {
|
---|
9 | private String text;
|
---|
10 | private Font font;
|
---|
11 | private boolean textAfter;
|
---|
12 | private boolean selected;
|
---|
13 |
|
---|
14 | public RadioButton(final String newName, final int newX, final int newY, final int newWidth, final int newHeight, final String newText, final Font newFont, final boolean isTextAfter) {
|
---|
15 | super(newName, newX, newY, newWidth, newHeight);
|
---|
16 | this.text = newText;
|
---|
17 | this.font = newFont;
|
---|
18 | this.textAfter = isTextAfter;
|
---|
19 | }
|
---|
20 |
|
---|
21 | @Override
|
---|
22 | public void draw(final Graphics g) {
|
---|
23 | final FontMetrics metrics = g.getFontMetrics(this.font);
|
---|
24 | g.setColor(Color.red);
|
---|
25 | g.drawOval(this.getX(), this.getY(), this.getWidth(), this.getHeight());
|
---|
26 | if (this.selected) {
|
---|
27 | g.setColor(Color.green);
|
---|
28 | g.fillOval(this.getX() + 5, this.getY() + 5, this.getWidth() - 10, this.getHeight() - 10);
|
---|
29 | }
|
---|
30 | g.setColor(Color.green);
|
---|
31 | g.setFont(this.font);
|
---|
32 | if (this.textAfter) {
|
---|
33 | g.drawString(this.text, this.getX() + this.getWidth() + 7, this.getY() + (this.getHeight() + metrics.getHeight()) / 2 - 2);
|
---|
34 | }
|
---|
35 | else {
|
---|
36 | g.drawString(this.text, this.getX() - metrics.stringWidth(this.text) - 7, this.getY() + (this.getHeight() + metrics.getHeight()) / 2 - 2);
|
---|
37 | }
|
---|
38 | }
|
---|
39 |
|
---|
40 | @Override
|
---|
41 | public void clear() {
|
---|
42 | this.selected = false;
|
---|
43 | }
|
---|
44 |
|
---|
45 | public String getLabel() {
|
---|
46 | return this.text;
|
---|
47 | }
|
---|
48 |
|
---|
49 | @Override
|
---|
50 | public boolean isClicked(final int xCoord, final int yCoord) {
|
---|
51 | final double distance = Math.sqrt(Math.pow(this.getX() + this.getWidth() / 2 - xCoord, 2.0) + Math.pow(this.getY() + this.getHeight() / 2 - yCoord, 2.0));
|
---|
52 | return distance <= this.getWidth() / 2;
|
---|
53 | }
|
---|
54 |
|
---|
55 | public boolean isSelected() {
|
---|
56 | return this.selected;
|
---|
57 | }
|
---|
58 |
|
---|
59 | public void setSelected(final boolean isSelected) {
|
---|
60 | this.selected = isSelected;
|
---|
61 | }
|
---|
62 | }
|
---|