package gamegui; import java.awt.*; import java.awt.image.*; public class Button extends Member { private String text; public Color color; private Font font; private BufferedImage img; private Align alignment; boolean border; public Button(String newName, int newX, int newY, int newWidth, int newHeight, String newText, Font newFont) { super(newName, newX, newY, newWidth, newHeight); text = newText; color = Color.green; font = newFont; img = null; alignment = Align.Left; border = true; } public Button(String newName, int newX, int newY, int newWidth, int newHeight, String newText, Font newFont, Color color) { super(newName, newX, newY, newWidth, newHeight); text = newText; this.color = color; font = newFont; img = null; alignment = Align.Left; border = true; } public Button(String newName, int newX, int newY, int newWidth, int newHeight, String newText, Font newFont, Color color, boolean border) { super(newName, newX, newY, newWidth, newHeight); text = newText; this.color = color; font = newFont; img = null; alignment = Align.Left; this.border = border; } public Button(String newName, int newX, int newY, int newWidth, int newHeight, String newText, Font newFont, Align alignment) { super(newName, newX, newY, newWidth, newHeight); text = newText; color = Color.green; font = newFont; img = null; this.alignment = alignment; border = true; } public Button(String newName, int newX, int newY, int newWidth, int newHeight, BufferedImage img) { super(newName, newX, newY, newWidth, newHeight); text = ""; font = null; this.img = img; alignment = Align.Left; border = false; } public void setImage(BufferedImage img) { this.img = img; } public void draw(Graphics g) { if(img == null) { FontMetrics metrics = g.getFontMetrics(font); if(border) { g.setColor(Color.red); g.drawRect(getX(), getY(), getWidth(), getHeight()); } g.setColor(color); g.setFont(font); switch(alignment) { case Center: g.drawString(text, getX() + (getWidth() - metrics.stringWidth(text))/2, getY() + (getHeight() + metrics.getHeight())/2 - 2); break; case Right: g.drawString(text, getX() + getWidth() - metrics.stringWidth(text), getY() + (getHeight() + metrics.getHeight())/2 - 2); break; case Left: g.drawString(text, getX(), getY() + (getHeight() + metrics.getHeight())/2 - 2); break; } }else g.drawImage(img, getX() + (getWidth() - img.getWidth(null))/2, getY() + (getHeight() - img.getHeight(null))/2 - 2, null); } }