comparison game/IDMButton.java @ 75:b586ce4f6d97

Large GUI code cleanup.
author Matti Hamalainen <ccr@tnsp.org>
date Thu, 03 Mar 2011 04:04:57 +0200
parents bca0112851d6
children 881deac2daf8
comparison
equal deleted inserted replaced
74:b7adeae80363 75:b586ce4f6d97
22 String text; 22 String text;
23 boolean active; 23 boolean active;
24 24
25 public IDMButton(IDMPoint pos, int keyCode, Font font, String text) 25 public IDMButton(IDMPoint pos, int keyCode, Font font, String text)
26 { 26 {
27 super(pos);
27 loadImages(); 28 loadImages();
28 this.pos = pos; 29 setSize(imgUp.getWidth(), imgUp.getHeight());
30
29 this.font = font; 31 this.font = font;
30 this.keyCode = keyCode; 32 this.keyCode = keyCode;
31 this.text = text; 33 this.text = text;
34
32 state = State.NORMAL; 35 state = State.NORMAL;
33 active = false; 36 active = false;
34 } 37 }
35 38
36 public IDMButton(float x, float y, int keyCode, Font font, String text) 39 public IDMButton(float x, float y, int keyCode, Font font, String text)
80 g.drawImage(img, getScaledX() + xoffs, getScaledY() + yoffs, null); 83 g.drawImage(img, getScaledX() + xoffs, getScaledY() + yoffs, null);
81 84
82 g.setFont(font); 85 g.setFont(font);
83 g.setPaint(Color.black); 86 g.setPaint(Color.black);
84 g.drawString(text, 87 g.drawString(text,
85 getScaledX() + xoffs * 2 + (img.getWidth() - textWidth) / 2, 88 getScaledX() + xoffs + (getScaledWidth() - textWidth) / 2,
86 getScaledY() + yoffs * 2 + img.getHeight() / 2); 89 getScaledY() + yoffs + getScaledHeight() / 2);
87 } 90 }
88 91
89 public boolean contains(Point where)
90 {
91 return (where.x >= getScaledX() && where.y >= getScaledY() &&
92 where.x < getScaledX() + imgUp.getWidth() &&
93 where.y < getScaledY() + imgUp.getHeight());
94 }
95 92
96 public void mousePressed(MouseEvent e) 93 public boolean mousePressed(MouseEvent e)
97 { 94 {
98 state = State.PRESSED; 95 state = State.PRESSED;
99 active = true; 96 active = true;
97 return true;
100 } 98 }
101 99
102 public void mouseReleased(MouseEvent e) 100 public boolean mouseReleased(MouseEvent e)
103 { 101 {
102 boolean oldActive = active;
104 super.mouseReleased(e); 103 super.mouseReleased(e);
105 state = State.NORMAL; 104 state = State.NORMAL;
106 active = false; 105 active = false;
106 return oldActive;
107 } 107 }
108 108
109 public void mouseEntered(MouseEvent e) 109 public boolean mouseEntered(MouseEvent e)
110 { 110 {
111 if (active) 111 if (active)
112 {
112 state = State.PRESSED; 113 state = State.PRESSED;
114 return true;
115 }
116 else
117 return false;
113 } 118 }
114 119
115 public void mouseExited(MouseEvent e) 120 public boolean mouseExited(MouseEvent e)
116 { 121 {
117 if (active) 122 if (active)
123 {
118 state = State.NORMAL; 124 state = State.NORMAL;
125 return true;
126 }
127 else
128 return false;
119 } 129 }
120 } 130 }