Mercurial > hg > ristipolku
comparison game/IDMButton.java @ 49:e6da5c71be28
Add more code to IDM widgets, do preliminary work for integrating them.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Tue, 22 Feb 2011 10:52:08 +0200 |
parents | |
children | 496e616ff09d |
comparison
equal
deleted
inserted
replaced
48:f13bab4cccd3 | 49:e6da5c71be28 |
---|---|
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 Point pos; | |
21 Font font; | |
22 FontMetrics metrics; | |
23 String text; | |
24 | |
25 public IDMButton(Point pos, int keyCode, Font font, String text) | |
26 { | |
27 loadImages(); | |
28 this.pos = pos; | |
29 this.font = font; | |
30 this.keyCode = keyCode; | |
31 this.text = text; | |
32 state = State.NORMAL; | |
33 } | |
34 | |
35 public IDMButton(int x, int y, int keyCode, Font font, String text) | |
36 { | |
37 this(new Point(x, y), keyCode, font, text); | |
38 } | |
39 | |
40 public void move(Point pos) | |
41 { | |
42 this.pos = pos; | |
43 } | |
44 | |
45 public void move(int x, int y) | |
46 { | |
47 this.pos = new Point(x, y); | |
48 } | |
49 | |
50 private static void loadImages() | |
51 { | |
52 try | |
53 { | |
54 ResourceLoader res = new ResourceLoader("graphics/button1_up.png"); | |
55 imgUp = ImageIO.read(res.getStream()); | |
56 | |
57 res = new ResourceLoader("graphics/button1_down.png"); | |
58 imgPressed = ImageIO.read(res.getStream()); | |
59 } | |
60 catch (IOException e) | |
61 { | |
62 System.out.print(e.getMessage()); | |
63 } | |
64 } | |
65 | |
66 public void paint(Graphics2D g) | |
67 { | |
68 BufferedImage img; | |
69 int xoffs, yoffs; | |
70 | |
71 if (state == State.PRESSED) | |
72 { | |
73 img = imgPressed; | |
74 xoffs = yoffs = 15; | |
75 } | |
76 else | |
77 { | |
78 xoffs = yoffs = 0; | |
79 img = imgUp; | |
80 } | |
81 | |
82 if (metrics == null) | |
83 metrics = g.getFontMetrics(font); | |
84 | |
85 int textWidth = metrics.stringWidth(text); | |
86 g.drawImage(img, pos.x + xoffs, pos.y + yoffs, null); | |
87 | |
88 g.setFont(font); | |
89 g.setPaint(Color.black); | |
90 g.drawString(text, | |
91 pos.x + xoffs + (img.getWidth() - textWidth) / 2, | |
92 pos.y + yoffs + (img.getHeight() - metrics.getHeight() / 2) / 2); | |
93 } | |
94 | |
95 public boolean contains(Point where) | |
96 { | |
97 return (where.x >= pos.x && where.y >= pos.y && | |
98 where.x < pos.x + imgUp.getWidth() && | |
99 where.y < pos.y + imgUp.getHeight()); | |
100 } | |
101 | |
102 public void mousePressed(MouseEvent e) | |
103 { | |
104 state = State.PRESSED; | |
105 } | |
106 | |
107 public void mouseReleased(MouseEvent e) | |
108 { | |
109 super.mouseReleased(e); | |
110 state = State.NORMAL; | |
111 } | |
112 | |
113 public void clicked() | |
114 { | |
115 } | |
116 } |