Mercurial > hg > ristipolku
comparison game/IDMButton.java @ 162:e8eeac403e5f
Backed out changeset fb33d3796942
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Thu, 01 Dec 2016 14:33:25 +0200 |
parents | src/IDMButton.java@fb33d3796942 |
children | dda7152d2402 |
comparison
equal
deleted
inserted
replaced
161:fb33d3796942 | 162:e8eeac403e5f |
---|---|
1 /* | |
2 * Ristipolku IDM button widget | |
3 * (C) Copyright 2011 Matti 'ccr' Hämäläinen <ccr@tnsp.org> | |
4 */ | |
5 package game; | |
6 | |
7 import java.awt.*; | |
8 import java.awt.event.*; | |
9 import java.awt.image.*; | |
10 import java.awt.geom.*; | |
11 import javax.imageio.*; | |
12 import java.io.*; | |
13 | |
14 | |
15 public class IDMButton extends IDMWidget | |
16 { | |
17 enum State { FOCUSED, PRESSED, NORMAL } | |
18 State state; | |
19 static BufferedImage imgUp, imgPressed; | |
20 Font font; | |
21 FontMetrics metrics; | |
22 String text; | |
23 boolean active; | |
24 | |
25 public IDMButton(IDMPoint pos, int keyCode, Font font, String text) | |
26 { | |
27 super(pos); | |
28 loadImages(); | |
29 setSize(imgUp.getWidth(), imgUp.getHeight()); | |
30 | |
31 this.font = font; | |
32 this.keyCode = keyCode; | |
33 this.text = text; | |
34 | |
35 state = State.NORMAL; | |
36 active = false; | |
37 } | |
38 | |
39 public IDMButton(float x, float y, int keyCode, Font font, String text) | |
40 { | |
41 this(new IDMPoint(x, y), keyCode, font, text); | |
42 } | |
43 | |
44 private static void loadImages() | |
45 { | |
46 if (imgUp != null && imgPressed != null) | |
47 return; | |
48 | |
49 try | |
50 { | |
51 ResourceLoader res = new ResourceLoader("graphics/button1_up.png"); | |
52 imgUp = ImageIO.read(res.getStream()); | |
53 | |
54 res = new ResourceLoader("graphics/button1_down.png"); | |
55 imgPressed = ImageIO.read(res.getStream()); | |
56 } | |
57 catch (IOException e) | |
58 { | |
59 System.out.print(e.getMessage()); | |
60 } | |
61 } | |
62 | |
63 public void scale() | |
64 { | |
65 } | |
66 | |
67 public void paint(Graphics2D g) | |
68 { | |
69 BufferedImage img; | |
70 int xoffs, yoffs; | |
71 | |
72 if (state == State.PRESSED) | |
73 { | |
74 img = imgPressed; | |
75 xoffs = yoffs = 5; | |
76 } | |
77 else | |
78 { | |
79 xoffs = yoffs = 0; | |
80 img = imgUp; | |
81 } | |
82 | |
83 if (metrics == null) | |
84 metrics = g.getFontMetrics(font); | |
85 | |
86 int textWidth = metrics.stringWidth(text); | |
87 g.drawImage(img, getScaledX() + xoffs, getScaledY() + yoffs, null); | |
88 | |
89 g.setFont(font); | |
90 g.setPaint(Color.black); | |
91 g.drawString(text, | |
92 getScaledX() + xoffs + (getScaledWidth() - textWidth) / 2, | |
93 getScaledY() + yoffs + getScaledHeight() / 2); | |
94 } | |
95 | |
96 | |
97 public boolean mousePressed(MouseEvent e) | |
98 { | |
99 state = State.PRESSED; | |
100 active = true; | |
101 return true; | |
102 } | |
103 | |
104 public boolean mouseReleased(MouseEvent e) | |
105 { | |
106 boolean oldActive = active; | |
107 super.mouseReleased(e); | |
108 state = State.NORMAL; | |
109 active = false; | |
110 return oldActive; | |
111 } | |
112 | |
113 public boolean mouseEntered(MouseEvent e) | |
114 { | |
115 if (active) | |
116 { | |
117 state = State.PRESSED; | |
118 return true; | |
119 } | |
120 else | |
121 return false; | |
122 } | |
123 | |
124 public boolean mouseExited(MouseEvent e) | |
125 { | |
126 if (active) | |
127 { | |
128 state = State.NORMAL; | |
129 return true; | |
130 } | |
131 else | |
132 return false; | |
133 } | |
134 } |