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