comparison ScreenAdjuster.java @ 1:c5bdb7bee4a7

Refactor displayMessage. Enable holding messages. Cleanup.
author Tom Fredrik Blenning Klaussen <bfg@bfgconsult.no>
date Wed, 08 Jan 2014 19:00:08 +0100
parents 855307f4bf5e
children 77e2655ac09d
comparison
equal deleted inserted replaced
0:855307f4bf5e 1:c5bdb7bee4a7
5 import java.awt.geom.*; 5 import java.awt.geom.*;
6 import java.util.*; 6 import java.util.*;
7 7
8 class ScreenAdjuster extends JFrame implements ActionListener 8 class ScreenAdjuster extends JFrame implements ActionListener
9 { 9 {
10 final float TIMEOUT_IN_SECONDS = 2.5f;
10 int frameNumber = -1; 11 int frameNumber = -1;
11 //JLabel messageText = new JLabel("HEI");
12 Vector<String> messageText = null; 12 Vector<String> messageText = null;
13 Color backgroundColor = Color.BLACK; 13 Color backgroundColor = Color.BLACK;
14 Color foregroundColor; 14 Color foregroundColor;
15 Color outlineColor; 15 Color outlineColor;
16 javax.swing.Timer messageTimeout = new javax.swing.Timer(2000, this);
17 boolean messageVisible = false; 16 boolean messageVisible = false;
18 boolean messageOutline = false; 17 boolean messageOutline = false;
19 boolean messageBBox = false; 18 boolean messageBBox = false;
20 19
20 enum MessageStatus { TIMEOUT, SHOW, HIDE };
21
22 javax.swing.Timer messageTimeout =
23 new javax.swing.Timer((int)(TIMEOUT_IN_SECONDS * 1000), this);
24
21 ScreenAdjuster(boolean fullScreen) 25 ScreenAdjuster(boolean fullScreen)
22 { 26 {
23
24 messageTimeout.setRepeats(false); 27 messageTimeout.setRepeats(false);
25 28
26 nextFrame(); 29 nextFrame();
27 30
28 if (fullScreen) { 31 if (fullScreen) {
61 repaint(); 64 repaint();
62 } 65 }
63 66
64 protected void processKeyEvent(KeyEvent e) 67 protected void processKeyEvent(KeyEvent e)
65 { 68 {
69 int increase = 1;
70 MessageStatus messageStatus = MessageStatus.TIMEOUT;
66 if (e.getID() == KeyEvent.KEY_PRESSED) { 71 if (e.getID() == KeyEvent.KEY_PRESSED) {
67 Log.DEFAULT.println(e.paramString()); 72 Log.DEFAULT.println(e.paramString());
68 if (e.getKeyText(e.getKeyCode()).equals("Q")) { 73 if (e.getKeyCode() == KeyEvent.VK_Q) {
69 System.exit(0); 74 System.exit(0);
70 } 75 }
71 if (e.getKeyCode() == KeyEvent.VK_BACK_SPACE) { 76 if (e.getKeyCode() == KeyEvent.VK_BACK_SPACE) {
72 frameNumber -= 2; 77 increase = -1;
73 if (frameNumber < 0) 78 }
74 frameNumber += numFrames(); 79 else if (e.getKeyCode() == KeyEvent.VK_S) {
80 messageTimeout.stop();
81 increase = 0;
82 messageStatus = MessageStatus.SHOW;
83 }
84 else if (e.getKeyCode() == KeyEvent.VK_H) {
85 messageTimeout.stop();
86 increase = 0;
87 messageStatus = MessageStatus.HIDE;
75 } 88 }
76 Log.DEFAULT.println(e.getKeyText(e.getKeyCode())); 89 Log.DEFAULT.println(e.getKeyText(e.getKeyCode()));
77 nextFrame(); 90 displayFrame(increase, messageStatus);
78 } 91 }
79 } 92 }
80 93
81 static Color complementary(Color in) 94 static Color complementary(Color in)
82 { 95 {
89 return Color.BLACK; 102 return Color.BLACK;
90 } 103 }
91 104
92 void nextFrame() 105 void nextFrame()
93 { 106 {
94 frameNumber = (frameNumber + 1) % numFrames(); 107 displayFrame(1, MessageStatus.TIMEOUT);
108 }
109
110 void displayFrame(int increase, MessageStatus messageStatus)
111 {
112 int numFrames = this.numFrames();
113 frameNumber = (frameNumber + increase + numFrames) % numFrames;
95 foregroundColor = null; 114 foregroundColor = null;
96 backgroundColor = null; 115 backgroundColor = null;
97 outlineColor = null; 116 outlineColor = null;
98 messageText = new Vector<String>(0); 117 messageText = new Vector<String>(0);
99 messageBBox = false; 118 messageBBox = false;
100
101 119
102 switch (frameNumber) { 120 switch (frameNumber) {
103 case 0: 121 case 0:
104 messageText.add("This screen should appear all BLACK!"); 122 messageText.add("This screen should appear all BLACK!");
105 messageText.add("Please check for any dead pixels."); 123 messageText.add("Please check for any dead pixels.");
178 outlineColor = Color.BLACK; 196 outlineColor = Color.BLACK;
179 messageBBox = true; 197 messageBBox = true;
180 break; 198 break;
181 } 199 }
182 messageTimeout.stop(); 200 messageTimeout.stop();
183 messageVisible = true; 201 messageVisible = !(messageStatus == MessageStatus.HIDE);
184 202
185 messageTimeout.start(); 203 if (messageStatus == MessageStatus.TIMEOUT)
204 messageTimeout.start();
186 205
187 if (foregroundColor == null) 206 if (foregroundColor == null)
188 foregroundColor = complementary(backgroundColor); 207 foregroundColor = complementary(backgroundColor);
189 //messageText.setForeground(foregroundColor); 208 //messageText.setForeground(foregroundColor);
190 209
265 Log.DEFAULT.println(frameNumber); 284 Log.DEFAULT.println(frameNumber);
266 285
267 286
268 if (backgroundColor != null) { 287 if (backgroundColor != null) {
269 g.setColor(backgroundColor); 288 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); 289 g.fillRect(bounds.x, bounds.y, bounds.width, bounds.height);
272 } 290 }
273 291
274 switch (frameNumber) { 292 switch (frameNumber) {
275 case 0: 293 case 0:
336 /* 354 /*
337 if (messageText.isVisible()) 355 if (messageText.isVisible())
338 messageText.paint(g); 356 messageText.paint(g);
339 */ 357 */
340 if (messageVisible) { 358 if (messageVisible) {
341 Font font = g.getFont(); 359 drawMessage(g2);
342 font = new Font(font.getFontName(), 360 }
343 font.getStyle(), 361 }
344 (int)(font.getSize2D() * 2)); 362
345 double fontHeight = font.getMaxCharBounds(g2.getFontRenderContext()).getHeight(); 363 void drawMessage(Graphics2D g2)
346 double totalHeight = fontHeight * messageText.size(); 364 {
347 double y = (bounds.height - totalHeight) /2.0; 365 Font font = g2.getFont();
348 366 Rectangle bounds = getBounds();
349 for (int i = 0; i < messageText.size(); ++i) { 367
350 String text = messageText.get(i); 368 font = new Font(font.getFontName(),
351 if (text.length() > 0 ) { 369 font.getStyle(),
352 TextLayout tl = 370 (int)(font.getSize2D() * 2));
353 new TextLayout(messageText.get(i), 371 double fontHeight = font.getMaxCharBounds(g2.getFontRenderContext()).getHeight();
354 font, g2.getFontRenderContext()); 372 double totalHeight = fontHeight * messageText.size();
355 double textWidth = tl.getBounds().getWidth(); 373 double y = (bounds.height - totalHeight) /2.0;
356 double x = (bounds.width - textWidth) /2.0; 374
357 375 for (int i = 0; i < messageText.size(); ++i) {
358 Shape outline = 376 String text = messageText.get(i);
359 tl.getOutline(AffineTransform.getTranslateInstance(x,y)); 377 if (text.length() > 0 ) {
378 TextLayout tl =
379 new TextLayout(messageText.get(i),
380 font, g2.getFontRenderContext());
381 double textWidth = tl.getBounds().getWidth();
382 double x = (bounds.width - textWidth) /2.0;
383
384 Shape outline =
385 tl.getOutline(AffineTransform.getTranslateInstance(x,y));
360 386
361 Rectangle tBounds = outline.getBounds(); 387 Rectangle tBounds = outline.getBounds();
362 388
363 if (messageBBox) { 389 if (messageBBox) {
364 g2.setColor(backgroundColor); 390 g2.setColor(backgroundColor);
365 g2.fillRect(tBounds.x, tBounds.y, tBounds.width, tBounds.height); 391 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 } 392 }
374 y += fontHeight; 393 g2.setColor(foregroundColor);
375 } 394 g2.fill(outline);
376 } 395 if (outlineColor != null) {
396 g2.setColor(outlineColor);
397 }
398 g2.draw(outline);
399 }
400 y += fontHeight;
401 }
402
377 } 403 }
378 404
379 } 405 }