Changeset 8edd04e in lost-haven for gamegui/RadioButton.java
- Timestamp:
- Jun 7, 2020, 3:04:32 PM (4 years ago)
- Branches:
- master
- Children:
- a49176d
- Parents:
- 155577b
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
gamegui/RadioButton.java
r155577b r8edd04e 1 1 package gamegui; 2 2 3 import java.awt.*; 3 import java.awt.Color; 4 import java.awt.Font; 5 import java.awt.FontMetrics; 6 import java.awt.Graphics; 4 7 5 public class RadioButton extends Member 6 { 7 private String text; 8 private Font font; 9 private boolean textAfter; 10 private boolean selected; 11 12 public RadioButton(String newName, int newX, int newY, int newWidth, int newHeight, String newText, Font newFont, boolean isTextAfter) 13 { 14 super(newName, newX, newY, newWidth, newHeight); 15 16 text = newText; 17 font = newFont; 18 textAfter = isTextAfter; 19 } 20 21 public void draw(Graphics g) 22 { 23 FontMetrics metrics = g.getFontMetrics(font); 24 25 g.setColor(Color.red); 26 g.drawOval(getX(), getY(), getWidth(), getHeight()); 27 28 if(selected) 29 { 30 g.setColor(Color.green); 31 g.fillOval(getX() + 5, getY() + 5, getWidth() - 10, getHeight() - 10); 32 } 33 34 g.setColor(Color.green); 35 g.setFont(font); 36 37 if(textAfter) 38 g.drawString(text, getX() + getWidth() + 7, getY() + (getHeight() + metrics.getHeight())/2 - 2); 39 else 40 g.drawString(text, getX() - metrics.stringWidth(text) - 7, getY() + (getHeight() + metrics.getHeight())/2 - 2); 41 } 42 43 public void clear() 44 { 45 selected = false; 46 } 47 48 public String getLabel() { 49 return text; 50 } 51 52 public boolean isClicked(int xCoord, int yCoord) 53 { 54 double distance = Math.sqrt(Math.pow(getX() + getWidth()/2 - xCoord, 2) + Math.pow(getY() +getHeight()/2 - yCoord, 2)); 55 56 return !(distance > getWidth() / 2); 57 } 58 59 public boolean isSelected() 60 { 61 return selected; 62 } 63 64 public void setSelected(boolean isSelected) 65 { 66 selected = isSelected; 67 } 8 public class RadioButton extends Member { 9 10 private String text; 11 private Font font; 12 private boolean textAfter; 13 private boolean selected; 14 15 public RadioButton(String newName, int newX, int newY, int newWidth, int newHeight, String newText, Font newFont, boolean isTextAfter) { 16 super(newName, newX, newY, newWidth, newHeight); 17 this.text = newText; 18 this.font = newFont; 19 this.textAfter = isTextAfter; 20 } 21 22 public void draw(Graphics g) { 23 FontMetrics metrics = g.getFontMetrics(this.font); 24 g.setColor(Color.red); 25 g.drawOval(getX(), getY(), getWidth(), getHeight()); 26 if (this.selected) { 27 g.setColor(Color.green); 28 g.fillOval(getX() + 5, getY() + 5, getWidth() - 10, getHeight() - 10); 29 } 30 g.setColor(Color.green); 31 g.setFont(this.font); 32 if (this.textAfter) { 33 g.drawString(this.text, getX() + getWidth() + 7, getY() + (getHeight() + metrics.getHeight()) / 2 - 2); 34 } else { 35 g.drawString(this.text, getX() - metrics.stringWidth(this.text) - 7, getY() + (getHeight() + metrics.getHeight()) / 2 - 2); 36 } 37 } 38 39 public void clear() { 40 this.selected = false; 41 } 42 43 public String getLabel() { 44 return this.text; 45 } 46 47 public boolean isClicked(int xCoord, int yCoord) { 48 double distance = Math.sqrt(Math.pow((getX() + getWidth() / 2 - xCoord), 2.0D) + Math.pow((getY() + getHeight() / 2 - yCoord), 2.0D)); 49 return !(distance > (getWidth() / 2)); 50 } 51 52 public boolean isSelected() { 53 return this.selected; 54 } 55 56 public void setSelected(boolean isSelected) { 57 this.selected = isSelected; 58 } 68 59 }
Note:
See TracChangeset
for help on using the changeset viewer.