changeset 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
files ScreenAdjuster.java
diffstat 1 files changed, 69 insertions(+), 43 deletions(-) [+]
line wrap: on
line diff
--- a/ScreenAdjuster.java	Fri Feb 22 17:24:40 2013 +0100
+++ b/ScreenAdjuster.java	Wed Jan 08 19:00:08 2014 +0100
@@ -7,20 +7,23 @@
 
 class ScreenAdjuster extends JFrame implements ActionListener
 {
+    final float TIMEOUT_IN_SECONDS = 2.5f;
     int frameNumber = -1;
-    //JLabel messageText = new JLabel("HEI");
     Vector<String> messageText = null;
     Color backgroundColor = Color.BLACK;
     Color foregroundColor;
     Color outlineColor;
-    javax.swing.Timer messageTimeout = new javax.swing.Timer(2000, this);
     boolean messageVisible = false;
     boolean messageOutline = false;
     boolean messageBBox = false;
 
+    enum MessageStatus { TIMEOUT, SHOW, HIDE };
+
+    javax.swing.Timer messageTimeout =
+	new javax.swing.Timer((int)(TIMEOUT_IN_SECONDS * 1000), this);
+
     ScreenAdjuster(boolean fullScreen)
     {
-
 	messageTimeout.setRepeats(false);
 
 	nextFrame();
@@ -63,18 +66,28 @@
 
     protected void processKeyEvent(KeyEvent e)
     {
+	int increase = 1;
+	MessageStatus messageStatus = MessageStatus.TIMEOUT;
 	if (e.getID() == KeyEvent.KEY_PRESSED) {
 	    Log.DEFAULT.println(e.paramString());
-	    if (e.getKeyText(e.getKeyCode()).equals("Q")) {
+	    if (e.getKeyCode() == KeyEvent.VK_Q) {
 		System.exit(0);
 	    }
 	    if (e.getKeyCode() == KeyEvent.VK_BACK_SPACE) {
-		frameNumber -= 2;
-		if (frameNumber < 0)
-		    frameNumber += numFrames();
+		increase = -1;
+	    }
+	    else if (e.getKeyCode() == KeyEvent.VK_S) {
+		messageTimeout.stop();
+		increase = 0;
+		messageStatus = MessageStatus.SHOW;
+	    }
+	    else if (e.getKeyCode() == KeyEvent.VK_H) {
+		messageTimeout.stop();
+		increase = 0;
+		messageStatus = MessageStatus.HIDE;
 	    }
 	    Log.DEFAULT.println(e.getKeyText(e.getKeyCode()));
-	    nextFrame();
+	    displayFrame(increase, messageStatus);
 	}
     }
 
@@ -91,14 +104,19 @@
 
     void nextFrame()
     {
-	frameNumber = (frameNumber + 1) % numFrames();
+	displayFrame(1, MessageStatus.TIMEOUT);
+    }
+
+    void displayFrame(int increase, MessageStatus messageStatus)
+    {
+	int numFrames = this.numFrames();
+	frameNumber = (frameNumber + increase + numFrames) % numFrames;
 	foregroundColor = null;
 	backgroundColor = null;
 	outlineColor = null;
 	messageText = new Vector<String>(0);
 	messageBBox = false;
 
-
 	switch (frameNumber) {
 	case 0:
 	    messageText.add("This screen should appear all BLACK!");
@@ -180,9 +198,10 @@
 	    break;
 	}
 	messageTimeout.stop();
-	messageVisible = true;
+	messageVisible = !(messageStatus == MessageStatus.HIDE);
 
-	messageTimeout.start();
+	if (messageStatus == MessageStatus.TIMEOUT)
+	    messageTimeout.start();
 
 	if (foregroundColor == null)
 	    foregroundColor = complementary(backgroundColor);
@@ -267,7 +286,6 @@
 
 	if (backgroundColor != null) {
 	    g.setColor(backgroundColor);
-	    //g.fillRect(cbounds.x, cbounds.y, cbounds.width, cbounds.height);
 	    g.fillRect(bounds.x, bounds.y, bounds.width, bounds.height);
 	}
 
@@ -338,42 +356,50 @@
 	    messageText.paint(g);
 	*/
 	if (messageVisible) {
-	    Font font = g.getFont();
-	    font = new Font(font.getFontName(),
-			    font.getStyle(),
-			    (int)(font.getSize2D() * 2));
-	    double fontHeight = font.getMaxCharBounds(g2.getFontRenderContext()).getHeight();
-	    double totalHeight = fontHeight * messageText.size();
-	    double y = (bounds.height - totalHeight) /2.0;
+	    drawMessage(g2);
+	}		
+    }
 
-	    for (int i = 0; i < messageText.size(); ++i) {
-		String text = messageText.get(i);
-		if (text.length() > 0 ) {
-		    TextLayout tl = 
-			new TextLayout(messageText.get(i),
-				       font, g2.getFontRenderContext());
-		    double textWidth = tl.getBounds().getWidth();
-		    double x = (bounds.width - textWidth) /2.0;
+    void drawMessage(Graphics2D g2)
+    {
+	Font font = g2.getFont();
+	Rectangle bounds = getBounds();
+
+	font = new Font(font.getFontName(),
+			font.getStyle(),
+			(int)(font.getSize2D() * 2));
+	double fontHeight = font.getMaxCharBounds(g2.getFontRenderContext()).getHeight();
+	double totalHeight = fontHeight * messageText.size();
+	double y = (bounds.height - totalHeight) /2.0;
 
-		    Shape outline =
-			tl.getOutline(AffineTransform.getTranslateInstance(x,y));
+	for (int i = 0; i < messageText.size(); ++i) {
+	    String text = messageText.get(i);
+	    if (text.length() > 0 ) {
+		TextLayout tl = 
+		    new TextLayout(messageText.get(i),
+				   font, g2.getFontRenderContext());
+		double textWidth = tl.getBounds().getWidth();
+		double x = (bounds.width - textWidth) /2.0;
+
+		Shape outline =
+		    tl.getOutline(AffineTransform.getTranslateInstance(x,y));
 		
-		    Rectangle tBounds = outline.getBounds();
+		Rectangle tBounds = outline.getBounds();
 
-		    if (messageBBox) {
-			g2.setColor(backgroundColor);
-			g2.fillRect(tBounds.x, tBounds.y, tBounds.width, tBounds.height);
-		    }
-		    g2.setColor(foregroundColor);
-		    g2.fill(outline);
-		    if (outlineColor != null) {
-			g2.setColor(outlineColor);
-		    }
-		    g2.draw(outline);
+		if (messageBBox) {
+		    g2.setColor(backgroundColor);
+		    g2.fillRect(tBounds.x, tBounds.y, tBounds.width, tBounds.height);
 		}
-		y += fontHeight;
+		g2.setColor(foregroundColor);
+		g2.fill(outline);
+		if (outlineColor != null) {
+		    g2.setColor(outlineColor);
+		}
+		g2.draw(outline);
 	    }
-	}		
+	    y += fontHeight;
+	}
+
     }
  
 }