Changeset 8edd04e in lost-haven for gamegui/Label.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/Label.java
r155577b r8edd04e 3 3 import java.awt.*; 4 4 5 public class Label extends Member 6 { 7 private String text; 8 private Font font; 9 private boolean centered; 10 private boolean fixed; 11 12 public Label(String newName, int newX, int newY, int newWidth, int newHeight, String newText, Font newFont, boolean centered) { 13 super(newName, newX, newY, newWidth, newHeight); 14 15 text = newText; 16 font = newFont; 17 this.centered = centered; 18 fixed = false; 19 } 20 21 public Label(String newName, int newX, int newY, int newWidth, int newHeight, String newText, Font newFont, boolean centered, boolean fixed) { 22 super(newName, newX, newY, newWidth, newHeight); 23 24 text = newText; 25 font = newFont; 26 this.centered = centered; 27 this.fixed = fixed; 28 } 29 30 public void setText(String s) { 31 text = s; 32 } 33 34 public void draw(Graphics g) { 35 FontMetrics metrics = g.getFontMetrics(font); 36 37 g.setColor(Color.green); 38 g.setFont(font); 39 40 if(centered) 41 g.drawString(text, getX() + (getWidth() - metrics.stringWidth(text))/2, getY() + (getHeight() + metrics.getHeight())/2 - 2); 42 else if(fixed) 43 g.drawString(text, getX(), getY()); 44 else 45 g.drawString(text, getX(), getY() + (getHeight() + metrics.getHeight())/2 - 2); 46 } 5 public class Label extends Member { 6 7 private String text; 8 private Font font; 9 private Align alignment; 10 11 public Label(String newName, int newX, int newY, int newWidth, int newHeight, String newText, Font newFont) { 12 super(newName, newX, newY, newWidth, newHeight); 13 this.text = newText; 14 this.font = newFont; 15 this.alignment = Align.Center; 16 } 17 18 public Label(String newName, int newX, int newY, int newWidth, int newHeight, String newText, Font newFont, Align alignment) { 19 super(newName, newX, newY, newWidth, newHeight); 20 this.text = newText; 21 this.font = newFont; 22 this.alignment = alignment; 23 } 24 25 public void setText(String s) { 26 this.text = s; 27 } 28 29 public void draw(Graphics g) { 30 FontMetrics metrics = g.getFontMetrics(this.font); 31 g.setColor(Color.green); 32 g.setFont(this.font); 33 switch (this.alignment) { 34 case Center: 35 g.drawString(this.text, getX() + (getWidth() - metrics.stringWidth(this.text)) / 2, getY() + (getHeight() + metrics.getHeight()) / 2 - 2); 36 break; 37 case Right: 38 g.drawString(this.text, getX() + getWidth() - metrics.stringWidth(this.text), getY() + (getHeight() + metrics.getHeight()) / 2 - 2); 39 break; 40 case Left: 41 g.drawString(this.text, getX(), getY() + (getHeight() + metrics.getHeight()) / 2 - 2); 42 break; 43 } 44 } 47 45 }
Note:
See TracChangeset
for help on using the changeset viewer.