1 package com.jpeterson.virtlcd.swing.ui;
2
3 import java.awt.Color;
4 import java.awt.Component;
5 import java.awt.Container;
6 import java.awt.Dimension;
7 import java.awt.Graphics;
8 import java.awt.Insets;
9 import java.awt.LayoutManager;
10 import java.awt.Point;
11
12 import javax.swing.JComponent;
13 import javax.swing.event.ChangeEvent;
14 import javax.swing.event.ChangeListener;
15 import javax.swing.plaf.ComponentUI;
16
17 import com.jpeterson.virtlcd.swing.DotMatrixModel;
18 import com.jpeterson.virtlcd.swing.JDotMatrixSixByEight;
19
20
21
22
23
24
25 public class BasicDotMatrixSixByEightUI extends DotMatrixSixByEightUI {
26
27
28
29 protected JDotMatrixSixByEight dotMatrixSixByEight;
30
31 protected ChangeListener dotMatrixSixByEightChangeListener;
32
33 protected static final int PIXEL_WIDTH = 2;
34
35 protected static final int PIXEL_HEIGHT = 2;
36
37 protected static final int PIXEL_INNER_WIDTH = 1;
38
39 protected static final int PIXEL_INNER_HEIGHT = 1;
40
41 protected static final int NUM_PIXELS_WIDE = 6;
42
43 protected static final int NUM_PIXELS_HIGH = 8;
44
45 protected static final int WIDTH = (NUM_PIXELS_WIDE * PIXEL_WIDTH)
46 + ((NUM_PIXELS_WIDE - 1) * PIXEL_INNER_WIDTH);
47
48 protected static final int HEIGHT = (NUM_PIXELS_HIGH * PIXEL_HEIGHT)
49 + ((NUM_PIXELS_HIGH - 1) * PIXEL_INNER_HEIGHT);
50
51 protected static Point coords[][] = new Point[6][8];
52
53 static {
54
55 for (int i = 0; i < NUM_PIXELS_WIDE; i++) {
56 for (int j = 0; j < NUM_PIXELS_HIGH; j++) {
57 coords[i][j] = new Point();
58 coords[i][j].x = (PIXEL_WIDTH * i) + (PIXEL_INNER_WIDTH * i);
59 coords[i][j].y = (PIXEL_HEIGHT * j) + (PIXEL_INNER_HEIGHT * j);
60 }
61 }
62 }
63
64
65
66
67
68
69 public static ComponentUI createUI(JComponent c) {
70 return new BasicDotMatrixSixByEightUI();
71 }
72
73
74
75
76
77
78 public void installUI(JComponent c) {
79 this.dotMatrixSixByEight = (JDotMatrixSixByEight) c;
80 installDefaults();
81 installComponents();
82 installListeners();
83
84 c.setLayout(createLayoutManager());
85
86 }
87
88
89
90
91
92
93 public void uninstallUI(JComponent c) {
94 c.setLayout(null);
95 uninstallListeners();
96 uninstallComponents();
97 uninstallDefaults();
98
99 this.dotMatrixSixByEight = null;
100 }
101
102 public void installDefaults() {
103
104 }
105
106 public void installComponents() {
107 }
108
109 public void installListeners() {
110 this.dotMatrixSixByEightChangeListener = new ChangeListener() {
111 public void stateChanged(ChangeEvent e) {
112 dotMatrixSixByEight.repaint();
113 }
114 };
115 this.dotMatrixSixByEight.getModel().addChangeListener(
116 this.dotMatrixSixByEightChangeListener);
117 }
118
119 public void uninstallDefaults() {
120 }
121
122 public void uninstallComponents() {
123
124 }
125
126 public void uninstallListeners() {
127 this.dotMatrixSixByEight.getModel().removeChangeListener(
128 this.dotMatrixSixByEightChangeListener);
129 this.dotMatrixSixByEightChangeListener = null;
130 }
131
132 @Override
133 public void paint(Graphics g, JComponent c) {
134 super.paint(g, c);
135 this.paintPixels(g);
136 }
137
138 protected void paintPixels(Graphics g) {
139 DotMatrixModel model = dotMatrixSixByEight.getModel();
140 char c = model.getChar();
141 Color origColor = g.getColor();
142
143
144 g.setColor(dotMatrixSixByEight.getBackground());
145 g.fillRect(0, 0, WIDTH, HEIGHT);
146
147
148 g.setColor(dotMatrixSixByEight.getForeground());
149
150 try {
151 for (int i = 0; i < NUM_PIXELS_WIDE; i++) {
152 for (int j = 0; j < NUM_PIXELS_HIGH; j++) {
153 if (CharSet.BITMAP[c][i][j] == 1) {
154 g.fillRect(coords[i][j].x, coords[i][j].y, PIXEL_WIDTH,
155 PIXEL_HEIGHT);
156 }
157 }
158 }
159 } catch (ArrayIndexOutOfBoundsException e) {
160
161 System.err.println("No character defined for (" + c
162 + ") decimal value [" + (int) c + "]");
163 }
164
165 g.setColor(origColor);
166 }
167
168
169
170
171
172
173
174 protected LayoutManager createLayoutManager() {
175 return new DotMatrixSixByEightLayout();
176 }
177
178
179
180
181 protected class DotMatrixSixByEightLayout implements LayoutManager {
182
183
184
185
186
187
188 public void addLayoutComponent(String name, Component c) {
189 }
190
191
192
193
194
195
196 public void removeLayoutComponent(Component c) {
197 }
198
199
200
201
202
203
204 public Dimension preferredLayoutSize(Container c) {
205 int width = 0;
206 int height = 0;
207 Insets ins = c.getInsets();
208
209 width = WIDTH;
210
211
212 height = HEIGHT + 1;
213
214 return new Dimension(width + ins.left + ins.right, height + ins.top
215 + ins.bottom);
216 }
217
218
219
220
221
222
223 public Dimension minimumLayoutSize(Container c) {
224 return this.preferredLayoutSize(c);
225 }
226
227
228
229
230
231
232 public void layoutContainer(Container c) {
233
234
235
236
237 }
238 }
239 }