annotate 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
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
49
e6da5c71be28 Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1 /*
e6da5c71be28 Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2 * Ristipolku IDM button widget
e6da5c71be28 Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3 * (C) Copyright 2011 Matti 'ccr' Hämäläinen <ccr@tnsp.org>
e6da5c71be28 Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
4 */
e6da5c71be28 Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
5 package game;
e6da5c71be28 Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
6
e6da5c71be28 Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
7 import java.awt.*;
e6da5c71be28 Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
8 import java.awt.event.*;
e6da5c71be28 Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
9 import java.awt.image.*;
e6da5c71be28 Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
10 import java.awt.geom.*;
e6da5c71be28 Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
11 import javax.imageio.*;
e6da5c71be28 Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
12 import java.io.*;
e6da5c71be28 Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
13
e6da5c71be28 Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
14
e6da5c71be28 Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
15 public class IDMButton extends IDMWidget
e6da5c71be28 Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
16 {
e6da5c71be28 Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
17 enum State { FOCUSED, PRESSED, NORMAL }
e6da5c71be28 Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
18 State state;
e6da5c71be28 Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
19 static BufferedImage imgUp, imgPressed;
e6da5c71be28 Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
20 Point pos;
e6da5c71be28 Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
21 Font font;
e6da5c71be28 Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
22 FontMetrics metrics;
e6da5c71be28 Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
23 String text;
e6da5c71be28 Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
24
e6da5c71be28 Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
25 public IDMButton(Point pos, int keyCode, Font font, String text)
e6da5c71be28 Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
26 {
e6da5c71be28 Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
27 loadImages();
e6da5c71be28 Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
28 this.pos = pos;
e6da5c71be28 Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
29 this.font = font;
e6da5c71be28 Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
30 this.keyCode = keyCode;
e6da5c71be28 Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
31 this.text = text;
e6da5c71be28 Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
32 state = State.NORMAL;
e6da5c71be28 Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
33 }
e6da5c71be28 Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
34
e6da5c71be28 Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
35 public IDMButton(int x, int y, int keyCode, Font font, String text)
e6da5c71be28 Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
36 {
e6da5c71be28 Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
37 this(new Point(x, y), keyCode, font, text);
e6da5c71be28 Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
38 }
e6da5c71be28 Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
39
e6da5c71be28 Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
40 public void move(Point pos)
e6da5c71be28 Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
41 {
e6da5c71be28 Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
42 this.pos = pos;
e6da5c71be28 Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
43 }
e6da5c71be28 Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
44
e6da5c71be28 Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
45 public void move(int x, int y)
e6da5c71be28 Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
46 {
e6da5c71be28 Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
47 this.pos = new Point(x, y);
e6da5c71be28 Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
48 }
e6da5c71be28 Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
49
e6da5c71be28 Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
50 private static void loadImages()
e6da5c71be28 Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
51 {
e6da5c71be28 Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
52 try
e6da5c71be28 Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
53 {
e6da5c71be28 Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
54 ResourceLoader res = new ResourceLoader("graphics/button1_up.png");
e6da5c71be28 Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
55 imgUp = ImageIO.read(res.getStream());
e6da5c71be28 Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
56
e6da5c71be28 Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
57 res = new ResourceLoader("graphics/button1_down.png");
e6da5c71be28 Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
58 imgPressed = ImageIO.read(res.getStream());
e6da5c71be28 Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
59 }
e6da5c71be28 Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
60 catch (IOException e)
e6da5c71be28 Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
61 {
e6da5c71be28 Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
62 System.out.print(e.getMessage());
e6da5c71be28 Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
63 }
e6da5c71be28 Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
64 }
e6da5c71be28 Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
65
e6da5c71be28 Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
66 public void paint(Graphics2D g)
e6da5c71be28 Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
67 {
e6da5c71be28 Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
68 BufferedImage img;
e6da5c71be28 Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
69 int xoffs, yoffs;
e6da5c71be28 Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
70
e6da5c71be28 Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
71 if (state == State.PRESSED)
e6da5c71be28 Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
72 {
e6da5c71be28 Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
73 img = imgPressed;
e6da5c71be28 Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
74 xoffs = yoffs = 15;
e6da5c71be28 Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
75 }
e6da5c71be28 Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
76 else
e6da5c71be28 Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
77 {
e6da5c71be28 Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
78 xoffs = yoffs = 0;
e6da5c71be28 Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
79 img = imgUp;
e6da5c71be28 Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
80 }
e6da5c71be28 Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
81
e6da5c71be28 Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
82 if (metrics == null)
e6da5c71be28 Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
83 metrics = g.getFontMetrics(font);
e6da5c71be28 Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
84
e6da5c71be28 Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
85 int textWidth = metrics.stringWidth(text);
e6da5c71be28 Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
86 g.drawImage(img, pos.x + xoffs, pos.y + yoffs, null);
e6da5c71be28 Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
87
e6da5c71be28 Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
88 g.setFont(font);
e6da5c71be28 Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
89 g.setPaint(Color.black);
e6da5c71be28 Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
90 g.drawString(text,
e6da5c71be28 Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
91 pos.x + xoffs + (img.getWidth() - textWidth) / 2,
e6da5c71be28 Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
92 pos.y + yoffs + (img.getHeight() - metrics.getHeight() / 2) / 2);
e6da5c71be28 Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
93 }
e6da5c71be28 Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
94
e6da5c71be28 Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
95 public boolean contains(Point where)
e6da5c71be28 Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
96 {
e6da5c71be28 Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
97 return (where.x >= pos.x && where.y >= pos.y &&
e6da5c71be28 Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
98 where.x < pos.x + imgUp.getWidth() &&
e6da5c71be28 Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
99 where.y < pos.y + imgUp.getHeight());
e6da5c71be28 Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
100 }
e6da5c71be28 Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
101
e6da5c71be28 Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
102 public void mousePressed(MouseEvent e)
e6da5c71be28 Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
103 {
e6da5c71be28 Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
104 state = State.PRESSED;
e6da5c71be28 Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
105 }
e6da5c71be28 Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
106
e6da5c71be28 Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
107 public void mouseReleased(MouseEvent e)
e6da5c71be28 Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
108 {
e6da5c71be28 Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
109 super.mouseReleased(e);
e6da5c71be28 Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
110 state = State.NORMAL;
e6da5c71be28 Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
111 }
e6da5c71be28 Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
112
e6da5c71be28 Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
113 public void clicked()
e6da5c71be28 Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
114 {
e6da5c71be28 Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
115 }
e6da5c71be28 Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
116 }