package gamegui; import java.awt.FontMetrics; import java.awt.Color; import java.awt.Graphics; import java.awt.Font; public class RadioButton extends Member { private String text; private Font font; private boolean textAfter; private boolean selected; 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) { super(newName, newX, newY, newWidth, newHeight); this.text = newText; this.font = newFont; this.textAfter = isTextAfter; } @Override public void draw(final Graphics g) { final FontMetrics metrics = g.getFontMetrics(this.font); g.setColor(Color.red); g.drawOval(this.getX(), this.getY(), this.getWidth(), this.getHeight()); if (this.selected) { g.setColor(Color.green); g.fillOval(this.getX() + 5, this.getY() + 5, this.getWidth() - 10, this.getHeight() - 10); } g.setColor(Color.green); g.setFont(this.font); if (this.textAfter) { g.drawString(this.text, this.getX() + this.getWidth() + 7, this.getY() + (this.getHeight() + metrics.getHeight()) / 2 - 2); } else { g.drawString(this.text, this.getX() - metrics.stringWidth(this.text) - 7, this.getY() + (this.getHeight() + metrics.getHeight()) / 2 - 2); } } @Override public void clear() { this.selected = false; } public String getLabel() { return this.text; } @Override public boolean isClicked(final int xCoord, final int yCoord) { 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)); return distance <= this.getWidth() / 2; } public boolean isSelected() { return this.selected; } public void setSelected(final boolean isSelected) { this.selected = isSelected; } }