source: winedb/src/gamegui/Button.java@ 9b6a069

Last change on this file since 9b6a069 was 9b6a069, checked in by dportnoy <devnull@…>, 13 years ago

Initial commit

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