Changeset 8edd04e in lost-haven for gamegui/RadioGroup.java
- Timestamp:
- Jun 7, 2020, 3:04:32 PM (5 years ago)
- Branches:
- master
- Children:
- a49176d
- Parents:
- 155577b
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
gamegui/RadioGroup.java
r155577b r8edd04e 1 1 package gamegui; 2 2 3 import java.awt.*; 4 import java.awt.event.*; 5 import java.util.*; 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; 6 9 10 public class RadioGroup extends Member { 7 11 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 } 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 } 83 80 }
Note:
See TracChangeset
for help on using the changeset viewer.