comparison game/IDMButton.java @ 50:496e616ff09d

More work on IDMgui.
author Matti Hamalainen <ccr@tnsp.org>
date Tue, 22 Feb 2011 11:45:40 +0200
parents e6da5c71be28
children f81f76458b92
comparison
equal deleted inserted replaced
49:e6da5c71be28 50:496e616ff09d
19 static BufferedImage imgUp, imgPressed; 19 static BufferedImage imgUp, imgPressed;
20 Point pos; 20 Point pos;
21 Font font; 21 Font font;
22 FontMetrics metrics; 22 FontMetrics metrics;
23 String text; 23 String text;
24 boolean active;
24 25
25 public IDMButton(Point pos, int keyCode, Font font, String text) 26 public IDMButton(Point pos, int keyCode, Font font, String text)
26 { 27 {
27 loadImages(); 28 loadImages();
28 this.pos = pos; 29 this.pos = pos;
29 this.font = font; 30 this.font = font;
30 this.keyCode = keyCode; 31 this.keyCode = keyCode;
31 this.text = text; 32 this.text = text;
32 state = State.NORMAL; 33 state = State.NORMAL;
34 active = false;
33 } 35 }
34 36
35 public IDMButton(int x, int y, int keyCode, Font font, String text) 37 public IDMButton(int x, int y, int keyCode, Font font, String text)
36 { 38 {
37 this(new Point(x, y), keyCode, font, text); 39 this(new Point(x, y), keyCode, font, text);
69 int xoffs, yoffs; 71 int xoffs, yoffs;
70 72
71 if (state == State.PRESSED) 73 if (state == State.PRESSED)
72 { 74 {
73 img = imgPressed; 75 img = imgPressed;
74 xoffs = yoffs = 15; 76 xoffs = yoffs = 5;
75 } 77 }
76 else 78 else
77 { 79 {
78 xoffs = yoffs = 0; 80 xoffs = yoffs = 0;
79 img = imgUp; 81 img = imgUp;
86 g.drawImage(img, pos.x + xoffs, pos.y + yoffs, null); 88 g.drawImage(img, pos.x + xoffs, pos.y + yoffs, null);
87 89
88 g.setFont(font); 90 g.setFont(font);
89 g.setPaint(Color.black); 91 g.setPaint(Color.black);
90 g.drawString(text, 92 g.drawString(text,
91 pos.x + xoffs + (img.getWidth() - textWidth) / 2, 93 pos.x + xoffs * 2 + (img.getWidth() - textWidth) / 2,
92 pos.y + yoffs + (img.getHeight() - metrics.getHeight() / 2) / 2); 94 pos.y + yoffs * 2 + (img.getHeight() / 2));
93 } 95 }
94 96
95 public boolean contains(Point where) 97 public boolean contains(Point where)
96 { 98 {
97 return (where.x >= pos.x && where.y >= pos.y && 99 return (where.x >= pos.x && where.y >= pos.y &&
100 } 102 }
101 103
102 public void mousePressed(MouseEvent e) 104 public void mousePressed(MouseEvent e)
103 { 105 {
104 state = State.PRESSED; 106 state = State.PRESSED;
107 active = true;
105 } 108 }
106 109
107 public void mouseReleased(MouseEvent e) 110 public void mouseReleased(MouseEvent e)
108 { 111 {
109 super.mouseReleased(e); 112 super.mouseReleased(e);
110 state = State.NORMAL; 113 state = State.NORMAL;
114 active = false;
111 } 115 }
112 116
113 public void clicked() 117 public void mouseEntered(MouseEvent e)
114 { 118 {
119 if (active)
120 {
121 state = State.PRESSED;
122 }
123 }
124
125 public void mouseExited(MouseEvent e)
126 {
127 state = State.NORMAL;
115 } 128 }
116 } 129 }