comparison ScreenAdjuster.java @ 0:855307f4bf5e

Working version.
author Tom Fredrik Blenning Klaussen <bfg@bfgconsult.no>
date Fri, 22 Feb 2013 17:24:40 +0100
parents
children c5bdb7bee4a7
comparison
equal deleted inserted replaced
-1:000000000000 0:855307f4bf5e
1 import javax.swing.*;
2 import java.awt.*;
3 import java.awt.event.*;
4 import java.awt.font.*;
5 import java.awt.geom.*;
6 import java.util.*;
7
8 class ScreenAdjuster extends JFrame implements ActionListener
9 {
10 int frameNumber = -1;
11 //JLabel messageText = new JLabel("HEI");
12 Vector<String> messageText = null;
13 Color backgroundColor = Color.BLACK;
14 Color foregroundColor;
15 Color outlineColor;
16 javax.swing.Timer messageTimeout = new javax.swing.Timer(2000, this);
17 boolean messageVisible = false;
18 boolean messageOutline = false;
19 boolean messageBBox = false;
20
21 ScreenAdjuster(boolean fullScreen)
22 {
23
24 messageTimeout.setRepeats(false);
25
26 nextFrame();
27
28 if (fullScreen) {
29 setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
30 setUndecorated(true);
31 //setResizable(false);
32 /*
33 Rectangle bounds = getBounds();
34 setBounds(bounds.x,bounds.y,200,200);
35 setVisible(true);
36 */
37 /*
38 fullscreenFrame.add(new JLabel("Press ALT+F4 to exit fullscreen.",
39 SwingConstants.CENTER),
40 BorderLayout.CENTER);
41 */
42 validate();
43
44 setVisible(true);
45 GraphicsEnvironment.getLocalGraphicsEnvironment()
46 .getDefaultScreenDevice().setFullScreenWindow(this);
47 }
48 else {
49 Rectangle bounds = getBounds();
50 setBounds(bounds.x,bounds.y,200,200);
51 setVisible(true);
52
53 }
54 enableEvents(AWTEvent.KEY_EVENT_MASK);
55
56 }
57
58 public void actionPerformed(ActionEvent evt)
59 {
60 messageVisible = false;
61 repaint();
62 }
63
64 protected void processKeyEvent(KeyEvent e)
65 {
66 if (e.getID() == KeyEvent.KEY_PRESSED) {
67 Log.DEFAULT.println(e.paramString());
68 if (e.getKeyText(e.getKeyCode()).equals("Q")) {
69 System.exit(0);
70 }
71 if (e.getKeyCode() == KeyEvent.VK_BACK_SPACE) {
72 frameNumber -= 2;
73 if (frameNumber < 0)
74 frameNumber += numFrames();
75 }
76 Log.DEFAULT.println(e.getKeyText(e.getKeyCode()));
77 nextFrame();
78 }
79 }
80
81 static Color complementary(Color in)
82 {
83 if (in != null) {
84 float hsb [] = Color.RGBtoHSB(in.getRed(),
85 in.getGreen(),
86 in.getBlue(), null);
87 return Color.getHSBColor(hsb[0], hsb[1], 1 - hsb[2]);
88 }
89 return Color.BLACK;
90 }
91
92 void nextFrame()
93 {
94 frameNumber = (frameNumber + 1) % numFrames();
95 foregroundColor = null;
96 backgroundColor = null;
97 outlineColor = null;
98 messageText = new Vector<String>(0);
99 messageBBox = false;
100
101
102 switch (frameNumber) {
103 case 0:
104 messageText.add("This screen should appear all BLACK!");
105 messageText.add("Please check for any dead pixels.");
106 messageText.add("");
107 messageText.add("Appart from this, please use this opportunity to clean your screen.");
108 backgroundColor = Color.BLACK;
109 break;
110 case 1:
111 messageText.add("This screen should appear all WHITE!");
112 messageText.add("Please check for any dead pixels.");
113 messageText.add("");
114 messageText.add("Appart from this, please use this opportunity to clean your screen.");
115 backgroundColor = Color.WHITE;
116 break;
117 case 2:
118 messageText.add("This screen should appear all RED!");
119 messageText.add("Please check for any dead pixels.");
120 backgroundColor = Color.RED;
121 break;
122 case 3:
123 messageText.add("This screen should appear all GREEN!");
124 messageText.add("Please check for any dead pixels.");
125 backgroundColor = Color.GREEN;
126 break;
127 case 4:
128 messageText.add("This screen should appear all BLUE!");
129 messageText.add("Please check for any dead pixels.");
130 backgroundColor = Color.BLUE;
131 break;
132 case 5:
133 messageText.add("This screen contains vertical stripes");
134 messageText.add("Please adjust your screen so they are perfectly vertical");
135 messageText.add("");
136 messageText.add("(Primarily applies to CRT screens)");
137 foregroundColor = Color.BLACK;
138 break;
139 case 6:
140 messageText.add("This screen contains horizontal stripes");
141 messageText.add("Please adjust your screen so they are perfectly horizontal");
142 messageText.add("");
143 messageText.add("(Primarily applies to CRT screens)");
144 foregroundColor = Color.BLACK;
145 break;
146 case 7:
147 messageText.add("This screen contains a chess pattern");
148 messageText.add("Please adjust your screen so the pattern is perfectly horizontal and vertical");
149 messageText.add("");
150 messageText.add("(Primarily applies to CRT screens)");
151 foregroundColor = Color.BLACK;
152 break;
153 case 8:
154 messageText.add("This screen should be all black, with a hairline yellow border");
155 messageText.add("Please adjust your screen so the border is perfectly aligned with the sides of your screen");
156 backgroundColor = Color.BLACK;
157 break;
158 case 9:
159 messageText.add("This screen contains a round circle");
160 messageText.add("Please adjust your screen so that the aspect ratio makes this perfectly round.");
161 foregroundColor = Color.WHITE;
162 backgroundColor = Color.WHITE;
163 outlineColor = Color.BLACK;
164 break;
165 case 10:
166 messageText.add("This screen contains a medium size chess pattern");
167 messageText.add("Please adjust your brightness and contrast.");
168 foregroundColor = Color.RED;
169 backgroundColor = Color.WHITE;
170 outlineColor = Color.BLACK;
171 messageBBox = true;
172 break;
173 case 11:
174 messageText.add("This screen contains a pixel size chess pattern");
175 messageText.add("Please adjust your brightness and contrast.");
176 foregroundColor = Color.RED;
177 backgroundColor = Color.WHITE;
178 outlineColor = Color.BLACK;
179 messageBBox = true;
180 break;
181 }
182 messageTimeout.stop();
183 messageVisible = true;
184
185 messageTimeout.start();
186
187 if (foregroundColor == null)
188 foregroundColor = complementary(backgroundColor);
189 //messageText.setForeground(foregroundColor);
190
191 repaint();
192 }
193
194 int numFrames()
195 {
196 return 12;
197 }
198
199 public static final void main(final String[] args) throws Exception
200 {
201 UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
202
203 Log.DEFAULT = new Log(new VoidStream());
204
205 boolean fullscreen = true;
206
207 if (args.length >= 1) {
208 fullscreen = false;
209 }
210 //fullscreen = false;
211
212 final JFrame fullscreenFrame = new ScreenAdjuster(fullscreen);
213
214 }
215
216 public static void paintCheckers(Graphics g, Rectangle bounds, int size, int numColors, int mode, boolean alternativeColors)
217 {
218 Rectangle cbounds = g.getClipBounds();
219
220 g.setColor(Color.WHITE);
221 //g.fillRect(cbounds.x, cbounds.y, cbounds.width, cbounds.height);
222 g.fillRect(bounds.x, bounds.y, bounds.width, bounds.height);
223
224 //Offset xmin by 1, if cliparea is on an odd boundary
225 int xmin = bounds.x;
226 int xmax = bounds.x + bounds.width;
227 int ymin = bounds.y;
228 int ymax = bounds.y + bounds.height;
229 int hcolor = -1;
230
231 Color colors[] = { Color.BLACK, Color.RED,
232 Color.GREEN, Color.BLUE, Color.WHITE };
233
234 if (numColors <= 2) {
235 if (alternativeColors) {
236 colors[0] = Color.YELLOW;
237 colors[1] = Color.BLUE;
238 }
239 else {
240 colors[1] = Color.WHITE;
241 }
242 }
243
244 g.setColor(Color.BLACK);
245 for (int x = xmin; x < xmax; x += size) {
246 if ((mode & 0x1) > 0)
247 hcolor = (hcolor + 1) % numColors;
248 int color = hcolor;
249 for (int y = ymin; y < ymax; y += size) {
250 if ((mode & 0x2) > 0)
251 color = (color + 1) % numColors;
252 g.setColor(colors[color]);
253 g.fillRect(x, y, size, size);
254 }
255 }
256 }
257
258 public void paint(Graphics g)
259 {
260 super.paint(g);
261 Graphics2D g2 = (Graphics2D) g;
262 Rectangle cbounds = g.getClipBounds();
263 Rectangle bounds = getBounds();
264
265 Log.DEFAULT.println(frameNumber);
266
267
268 if (backgroundColor != null) {
269 g.setColor(backgroundColor);
270 //g.fillRect(cbounds.x, cbounds.y, cbounds.width, cbounds.height);
271 g.fillRect(bounds.x, bounds.y, bounds.width, bounds.height);
272 }
273
274 switch (frameNumber) {
275 case 0:
276 case 1:
277 case 2:
278 case 3:
279 case 4:
280 break;
281 case 11: {
282 g.setColor(Color.BLACK);
283 //Offset xmin by 1, if cliparea is on an odd boundary
284 int xmin = cbounds.x + ((cbounds.x - bounds.x) & 0x1);
285 int xmax = cbounds.x + cbounds.width;
286 for (int x = xmin; x < xmax; x += 2) {
287 g.drawLine(x, bounds.y, x, bounds.y + bounds.height);
288 }
289 for (int y = bounds.y; y < bounds.y + bounds.height; y += 2) {
290 g.drawLine(bounds.x, y, bounds.x + bounds.width, y);
291 }
292 break;
293 }
294 case 7: {
295 paintCheckers(g, bounds, 50, 2, 3, true);
296 break;
297 }
298 case 10: {
299 paintCheckers(g, bounds, 5, 2, 3, false);
300 break;
301 }
302 case 9: {
303 int x = bounds.x;
304 int y = bounds.y;
305 int height = bounds.height - y;
306 int width = bounds.width - x;
307 int diameter;
308 if (height < width) {
309 diameter = height;
310 x += (width - height) /2;
311 }
312 else {
313 diameter = width;
314 y += (height - width) /2;
315 }
316 Log.DEFAULT.println("Geometry:"+bounds.x+"x"+bounds.y+":"+bounds.width + "x" + bounds.height);
317 g.setColor(Color.BLACK);
318 g.fillOval(x, y, diameter, diameter);
319
320 break;
321 }
322 case 8: {
323 g.setColor(Color.YELLOW);
324 g.drawRect(bounds.x+1, bounds.y+1, bounds.width-2, bounds.height-2);
325 break;
326 }
327 case 5: {
328 paintCheckers(g, bounds, 50, 2, 2, true);
329 break;
330 }
331 case 6: {
332 paintCheckers(g, bounds, 50, 2, 1, true);
333 break;
334 }
335 };
336 /*
337 if (messageText.isVisible())
338 messageText.paint(g);
339 */
340 if (messageVisible) {
341 Font font = g.getFont();
342 font = new Font(font.getFontName(),
343 font.getStyle(),
344 (int)(font.getSize2D() * 2));
345 double fontHeight = font.getMaxCharBounds(g2.getFontRenderContext()).getHeight();
346 double totalHeight = fontHeight * messageText.size();
347 double y = (bounds.height - totalHeight) /2.0;
348
349 for (int i = 0; i < messageText.size(); ++i) {
350 String text = messageText.get(i);
351 if (text.length() > 0 ) {
352 TextLayout tl =
353 new TextLayout(messageText.get(i),
354 font, g2.getFontRenderContext());
355 double textWidth = tl.getBounds().getWidth();
356 double x = (bounds.width - textWidth) /2.0;
357
358 Shape outline =
359 tl.getOutline(AffineTransform.getTranslateInstance(x,y));
360
361 Rectangle tBounds = outline.getBounds();
362
363 if (messageBBox) {
364 g2.setColor(backgroundColor);
365 g2.fillRect(tBounds.x, tBounds.y, tBounds.width, tBounds.height);
366 }
367 g2.setColor(foregroundColor);
368 g2.fill(outline);
369 if (outlineColor != null) {
370 g2.setColor(outlineColor);
371 }
372 g2.draw(outline);
373 }
374 y += fontHeight;
375 }
376 }
377 }
378
379 }