changeset 4:265f66a2f14f

Doublebuffering.
author Tom Fredrik Blenning Klaussen <bfg@bfgconsult.no>
date Tue, 14 Jul 2015 15:54:01 +0200
parents 061c76d0f567
children 09dcc2e5058a
files ScreenAdjuster.java
diffstat 1 files changed, 31 insertions(+), 21 deletions(-) [+]
line wrap: on
line diff
--- a/ScreenAdjuster.java	Thu Jan 09 18:45:15 2014 +0100
+++ b/ScreenAdjuster.java	Tue Jul 14 15:54:01 2015 +0200
@@ -4,6 +4,7 @@
 import java.awt.font.*;
 import java.awt.geom.*;
 import java.util.*;
+import java.awt.image.*;
 
 class ScreenAdjuster extends JFrame implements ActionListener
 {
@@ -17,11 +18,25 @@
     boolean messageOutline = false;
     boolean messageBBox = false;
 
+    BufferedImage bufferedImage;
+    Graphics2D g2;
+    Rectangle curBounds;
+    
     enum MessageStatus { TIMEOUT, SHOW, HIDE };
 
     javax.swing.Timer messageTimeout =
 	new javax.swing.Timer((int)(TIMEOUT_IN_SECONDS * 1000), this);
 
+    void resetSize()
+    {
+	Rectangle bounds = getBounds();
+	if (!bounds.equals(curBounds)) {
+	    bufferedImage = new BufferedImage(bounds.width, bounds.height, BufferedImage.TYPE_INT_ARGB);
+	    g2 = bufferedImage.createGraphics();
+	    curBounds = bounds;
+	}
+    }
+    
     ScreenAdjuster(boolean fullScreen)
     {
 	messageTimeout.setRepeats(false);
@@ -226,7 +241,6 @@
 	if (args.length >= 1) {
 	    fullscreen = false;
 	}
-	//fullscreen = false;
 
 	final JFrame fullscreenFrame = new ScreenAdjuster(fullscreen);
 
@@ -237,7 +251,6 @@
 	Rectangle cbounds = g.getClipBounds();
 
 	g.setColor(Color.WHITE);
-	//g.fillRect(cbounds.x, cbounds.y, cbounds.width, cbounds.height);
 	g.fillRect(bounds.x, bounds.y, bounds.width, bounds.height);
 
 	//Offset xmin by 1, if cliparea is on an odd boundary
@@ -276,8 +289,8 @@
 
     public void paint(Graphics g)
     {
-	super.paint(g);
-	Graphics2D g2 = (Graphics2D) g;
+	resetSize();
+	super.paint(g2);
 	Rectangle cbounds = g.getClipBounds();
 	Rectangle bounds = getBounds();
 
@@ -285,8 +298,8 @@
 
 
 	if (backgroundColor != null) {
-	    g.setColor(backgroundColor);
-	    g.fillRect(bounds.x, bounds.y, bounds.width, bounds.height);
+	    g2.setColor(backgroundColor);
+	    g2.fillRect(bounds.x, bounds.y, bounds.width, bounds.height);
 	}
 
 	switch (frameNumber) {
@@ -297,24 +310,24 @@
 	case 4:
 	    break;
 	case 11: {
-	    g.setColor(Color.BLACK);
+	    g2.setColor(Color.BLACK);
 	    //Offset xmin by 1, if cliparea is on an odd boundary
 	    int xmin = cbounds.x + ((cbounds.x - bounds.x) & 0x1);
 	    int xmax = cbounds.x + cbounds.width;
 	    for (int x = xmin; x < xmax; x += 2) {
-		g.drawLine(x, bounds.y, x, bounds.y + bounds.height);
+		g2.drawLine(x, bounds.y, x, bounds.y + bounds.height);
 	    }
 	    for (int y = bounds.y; y < bounds.y + bounds.height; y += 2) {
-		g.drawLine(bounds.x, y, bounds.x + bounds.width, y);
+		g2.drawLine(bounds.x, y, bounds.x + bounds.width, y);
 	    }
 	    break;
 	}
 	case 7: {
-	    paintCheckers(g, bounds, 50, 2, 3, true);
+	    paintCheckers(g2, bounds, 50, 2, 3, true);
 	    break;
 	}
 	case 10: {
-	    paintCheckers(g, bounds, 5, 2, 3, false);
+	    paintCheckers(g2, bounds, 5, 2, 3, false);
 	    break;
 	}
 	case 9: {
@@ -332,32 +345,29 @@
 		y += (height - width) /2;
 	    }
 	    Log.DEFAULT.println("Geometry:"+bounds.x+"x"+bounds.y+":"+bounds.width + "x" + bounds.height);
-	    g.setColor(Color.BLACK);
-	    g.fillOval(x, y, diameter, diameter);
+	    g2.setColor(Color.BLACK);
+	    g2.fillOval(x, y, diameter, diameter);
 
 	    break;
 	}
 	case 8: {
-	    g.setColor(Color.YELLOW);
-	    g.drawRect(bounds.x+1, bounds.y+1, bounds.width-2, bounds.height-2);
+	    g2.setColor(Color.YELLOW);
+	    g2.drawRect(bounds.x+1, bounds.y+1, bounds.width-2, bounds.height-2);
 	    break;
 	}
 	case 5: {
-	    paintCheckers(g, bounds, 50, 2, 2, true);
+	    paintCheckers(g2, bounds, 50, 2, 2, true);
 	    break;
 	}
 	case 6: {
-	    paintCheckers(g, bounds, 50, 2, 1, true);
+	    paintCheckers(g2, bounds, 50, 2, 1, true);
 	    break;
 	}
 	};
-	/*
-	if (messageText.isVisible())
-	    messageText.paint(g);
-	*/
 	if (messageVisible) {
 	    drawMessage(g2);
 	}
+	((Graphics2D) g).drawImage(bufferedImage, null, 0, 0);  
     }
 
     void drawMessage(Graphics2D g2)