source: last-defense/gamegui/Button.java

Last change on this file was fb4fc67, checked in by Dmitry Portnoy <dmitry.portnoy@…>, 6 years ago

Initial commit, code decompiled from a jar

  • Property mode set to 100644
File size: 3.7 KB
Line 
1package gamegui;
2
3import java.awt.FontMetrics;
4import java.awt.Image;
5import java.awt.image.ImageObserver;
6import java.awt.Graphics;
7import java.awt.image.BufferedImage;
8import java.awt.Font;
9import java.awt.Color;
10
11public class Button extends Member {
12 private String text;
13 public Color color;
14 private Font font;
15 private BufferedImage img;
16 private Align alignment;
17 boolean border;
18
19 public Button(final String newName, final int newX, final int newY, final int newWidth, final int newHeight, final String newText, final Font newFont) {
20 super(newName, newX, newY, newWidth, newHeight);
21 this.text = newText;
22 this.color = Color.green;
23 this.font = newFont;
24 this.img = null;
25 this.alignment = Align.Left;
26 this.border = true;
27 }
28
29 public Button(final String newName, final int newX, final int newY, final int newWidth, final int newHeight, final String newText, final Font newFont, final Color color) {
30 super(newName, newX, newY, newWidth, newHeight);
31 this.text = newText;
32 this.color = color;
33 this.font = newFont;
34 this.img = null;
35 this.alignment = Align.Left;
36 this.border = true;
37 }
38
39 public Button(final String newName, final int newX, final int newY, final int newWidth, final int newHeight, final String newText, final Font newFont, final Color color, final boolean border) {
40 super(newName, newX, newY, newWidth, newHeight);
41 this.text = newText;
42 this.color = color;
43 this.font = newFont;
44 this.img = null;
45 this.alignment = Align.Left;
46 this.border = border;
47 }
48
49 public Button(final String newName, final int newX, final int newY, final int newWidth, final int newHeight, final String newText, final Font newFont, final Align alignment) {
50 super(newName, newX, newY, newWidth, newHeight);
51 this.text = newText;
52 this.color = Color.green;
53 this.font = newFont;
54 this.img = null;
55 this.alignment = alignment;
56 this.border = true;
57 }
58
59 public Button(final String newName, final int newX, final int newY, final int newWidth, final int newHeight, final BufferedImage img) {
60 super(newName, newX, newY, newWidth, newHeight);
61 this.text = "";
62 this.font = null;
63 this.img = img;
64 this.alignment = Align.Left;
65 this.border = false;
66 }
67
68 @Override
69 public void draw(final Graphics g) {
70 if (this.img == null) {
71 final FontMetrics metrics = g.getFontMetrics(this.font);
72 if (this.border) {
73 g.setColor(Color.red);
74 g.drawRect(this.getX(), this.getY(), this.getWidth(), this.getHeight());
75 }
76 g.setColor(this.color);
77 g.setFont(this.font);
78 switch (this.alignment) {
79 case Center: {
80 g.drawString(this.text, this.getX() + (this.getWidth() - metrics.stringWidth(this.text)) / 2, this.getY() + (this.getHeight() + metrics.getHeight()) / 2 - 2);
81 break;
82 }
83 case Right: {
84 g.drawString(this.text, this.getX() + this.getWidth() - metrics.stringWidth(this.text), this.getY() + (this.getHeight() + metrics.getHeight()) / 2 - 2);
85 break;
86 }
87 case Left: {
88 g.drawString(this.text, this.getX(), this.getY() + (this.getHeight() + metrics.getHeight()) / 2 - 2);
89 break;
90 }
91 }
92 } else {
93 g.drawImage(this.img, this.getX() + (this.getWidth() - this.img.getWidth(null)) / 2, this.getY() + (this.getHeight() - this.img.getHeight(null)) / 2 - 2, null);
94 }
95 }
96}
Note: See TracBrowser for help on using the repository browser.