comparison ScreenAdjuster.java @ 4:265f66a2f14f

Doublebuffering.
author Tom Fredrik Blenning Klaussen <bfg@bfgconsult.no>
date Tue, 14 Jul 2015 15:54:01 +0200
parents 77e2655ac09d
children 09dcc2e5058a
comparison
equal deleted inserted replaced
3:061c76d0f567 4:265f66a2f14f
2 import java.awt.*; 2 import java.awt.*;
3 import java.awt.event.*; 3 import java.awt.event.*;
4 import java.awt.font.*; 4 import java.awt.font.*;
5 import java.awt.geom.*; 5 import java.awt.geom.*;
6 import java.util.*; 6 import java.util.*;
7 import java.awt.image.*;
7 8
8 class ScreenAdjuster extends JFrame implements ActionListener 9 class ScreenAdjuster extends JFrame implements ActionListener
9 { 10 {
10 final float TIMEOUT_IN_SECONDS = 2.5f; 11 final float TIMEOUT_IN_SECONDS = 2.5f;
11 int frameNumber = -1; 12 int frameNumber = -1;
15 Color outlineColor; 16 Color outlineColor;
16 boolean messageVisible = false; 17 boolean messageVisible = false;
17 boolean messageOutline = false; 18 boolean messageOutline = false;
18 boolean messageBBox = false; 19 boolean messageBBox = false;
19 20
21 BufferedImage bufferedImage;
22 Graphics2D g2;
23 Rectangle curBounds;
24
20 enum MessageStatus { TIMEOUT, SHOW, HIDE }; 25 enum MessageStatus { TIMEOUT, SHOW, HIDE };
21 26
22 javax.swing.Timer messageTimeout = 27 javax.swing.Timer messageTimeout =
23 new javax.swing.Timer((int)(TIMEOUT_IN_SECONDS * 1000), this); 28 new javax.swing.Timer((int)(TIMEOUT_IN_SECONDS * 1000), this);
24 29
30 void resetSize()
31 {
32 Rectangle bounds = getBounds();
33 if (!bounds.equals(curBounds)) {
34 bufferedImage = new BufferedImage(bounds.width, bounds.height, BufferedImage.TYPE_INT_ARGB);
35 g2 = bufferedImage.createGraphics();
36 curBounds = bounds;
37 }
38 }
39
25 ScreenAdjuster(boolean fullScreen) 40 ScreenAdjuster(boolean fullScreen)
26 { 41 {
27 messageTimeout.setRepeats(false); 42 messageTimeout.setRepeats(false);
28 43
29 nextFrame(); 44 nextFrame();
224 boolean fullscreen = true; 239 boolean fullscreen = true;
225 240
226 if (args.length >= 1) { 241 if (args.length >= 1) {
227 fullscreen = false; 242 fullscreen = false;
228 } 243 }
229 //fullscreen = false;
230 244
231 final JFrame fullscreenFrame = new ScreenAdjuster(fullscreen); 245 final JFrame fullscreenFrame = new ScreenAdjuster(fullscreen);
232 246
233 } 247 }
234 248
235 public static void paintCheckers(Graphics g, Rectangle bounds, int size, int numColors, int mode, boolean alternativeColors) 249 public static void paintCheckers(Graphics g, Rectangle bounds, int size, int numColors, int mode, boolean alternativeColors)
236 { 250 {
237 Rectangle cbounds = g.getClipBounds(); 251 Rectangle cbounds = g.getClipBounds();
238 252
239 g.setColor(Color.WHITE); 253 g.setColor(Color.WHITE);
240 //g.fillRect(cbounds.x, cbounds.y, cbounds.width, cbounds.height);
241 g.fillRect(bounds.x, bounds.y, bounds.width, bounds.height); 254 g.fillRect(bounds.x, bounds.y, bounds.width, bounds.height);
242 255
243 //Offset xmin by 1, if cliparea is on an odd boundary 256 //Offset xmin by 1, if cliparea is on an odd boundary
244 int xmin = bounds.x; 257 int xmin = bounds.x;
245 int xmax = bounds.x + bounds.width; 258 int xmax = bounds.x + bounds.width;
274 } 287 }
275 } 288 }
276 289
277 public void paint(Graphics g) 290 public void paint(Graphics g)
278 { 291 {
279 super.paint(g); 292 resetSize();
280 Graphics2D g2 = (Graphics2D) g; 293 super.paint(g2);
281 Rectangle cbounds = g.getClipBounds(); 294 Rectangle cbounds = g.getClipBounds();
282 Rectangle bounds = getBounds(); 295 Rectangle bounds = getBounds();
283 296
284 Log.DEFAULT.println(frameNumber); 297 Log.DEFAULT.println(frameNumber);
285 298
286 299
287 if (backgroundColor != null) { 300 if (backgroundColor != null) {
288 g.setColor(backgroundColor); 301 g2.setColor(backgroundColor);
289 g.fillRect(bounds.x, bounds.y, bounds.width, bounds.height); 302 g2.fillRect(bounds.x, bounds.y, bounds.width, bounds.height);
290 } 303 }
291 304
292 switch (frameNumber) { 305 switch (frameNumber) {
293 case 0: 306 case 0:
294 case 1: 307 case 1:
295 case 2: 308 case 2:
296 case 3: 309 case 3:
297 case 4: 310 case 4:
298 break; 311 break;
299 case 11: { 312 case 11: {
300 g.setColor(Color.BLACK); 313 g2.setColor(Color.BLACK);
301 //Offset xmin by 1, if cliparea is on an odd boundary 314 //Offset xmin by 1, if cliparea is on an odd boundary
302 int xmin = cbounds.x + ((cbounds.x - bounds.x) & 0x1); 315 int xmin = cbounds.x + ((cbounds.x - bounds.x) & 0x1);
303 int xmax = cbounds.x + cbounds.width; 316 int xmax = cbounds.x + cbounds.width;
304 for (int x = xmin; x < xmax; x += 2) { 317 for (int x = xmin; x < xmax; x += 2) {
305 g.drawLine(x, bounds.y, x, bounds.y + bounds.height); 318 g2.drawLine(x, bounds.y, x, bounds.y + bounds.height);
306 } 319 }
307 for (int y = bounds.y; y < bounds.y + bounds.height; y += 2) { 320 for (int y = bounds.y; y < bounds.y + bounds.height; y += 2) {
308 g.drawLine(bounds.x, y, bounds.x + bounds.width, y); 321 g2.drawLine(bounds.x, y, bounds.x + bounds.width, y);
309 } 322 }
310 break; 323 break;
311 } 324 }
312 case 7: { 325 case 7: {
313 paintCheckers(g, bounds, 50, 2, 3, true); 326 paintCheckers(g2, bounds, 50, 2, 3, true);
314 break; 327 break;
315 } 328 }
316 case 10: { 329 case 10: {
317 paintCheckers(g, bounds, 5, 2, 3, false); 330 paintCheckers(g2, bounds, 5, 2, 3, false);
318 break; 331 break;
319 } 332 }
320 case 9: { 333 case 9: {
321 int x = bounds.x; 334 int x = bounds.x;
322 int y = bounds.y; 335 int y = bounds.y;
330 else { 343 else {
331 diameter = width; 344 diameter = width;
332 y += (height - width) /2; 345 y += (height - width) /2;
333 } 346 }
334 Log.DEFAULT.println("Geometry:"+bounds.x+"x"+bounds.y+":"+bounds.width + "x" + bounds.height); 347 Log.DEFAULT.println("Geometry:"+bounds.x+"x"+bounds.y+":"+bounds.width + "x" + bounds.height);
335 g.setColor(Color.BLACK); 348 g2.setColor(Color.BLACK);
336 g.fillOval(x, y, diameter, diameter); 349 g2.fillOval(x, y, diameter, diameter);
337 350
338 break; 351 break;
339 } 352 }
340 case 8: { 353 case 8: {
341 g.setColor(Color.YELLOW); 354 g2.setColor(Color.YELLOW);
342 g.drawRect(bounds.x+1, bounds.y+1, bounds.width-2, bounds.height-2); 355 g2.drawRect(bounds.x+1, bounds.y+1, bounds.width-2, bounds.height-2);
343 break; 356 break;
344 } 357 }
345 case 5: { 358 case 5: {
346 paintCheckers(g, bounds, 50, 2, 2, true); 359 paintCheckers(g2, bounds, 50, 2, 2, true);
347 break; 360 break;
348 } 361 }
349 case 6: { 362 case 6: {
350 paintCheckers(g, bounds, 50, 2, 1, true); 363 paintCheckers(g2, bounds, 50, 2, 1, true);
351 break; 364 break;
352 } 365 }
353 }; 366 };
354 /*
355 if (messageText.isVisible())
356 messageText.paint(g);
357 */
358 if (messageVisible) { 367 if (messageVisible) {
359 drawMessage(g2); 368 drawMessage(g2);
360 } 369 }
370 ((Graphics2D) g).drawImage(bufferedImage, null, 0, 0);
361 } 371 }
362 372
363 void drawMessage(Graphics2D g2) 373 void drawMessage(Graphics2D g2)
364 { 374 {
365 Font font = g2.getFont(); 375 Font font = g2.getFont();