From 41bc398cfb530ab790f1f7f2e5f3a2c3f0e350c2 Mon Sep 17 00:00:00 2001 From: Starla Insigna Date: Sat, 14 Feb 2009 13:19:09 -0500 Subject: Engine: Refactored retieval of FontMetrics --- src/com/fourisland/fourpuzzle/Display.java | 22 ++++++++++++++++++++++ .../fourisland/fourpuzzle/window/ChoiceWindow.java | 9 +++------ .../fourpuzzle/window/MessageWindow.java | 12 ++---------- 3 files changed, 27 insertions(+), 16 deletions(-) (limited to 'src') diff --git a/src/com/fourisland/fourpuzzle/Display.java b/src/com/fourisland/fourpuzzle/Display.java index 0a9bfe1..a265806 100755 --- a/src/com/fourisland/fourpuzzle/Display.java +++ b/src/com/fourisland/fourpuzzle/Display.java @@ -14,6 +14,7 @@ import com.fourisland.fourpuzzle.transition.TransitionUnsupportedException; import com.fourisland.fourpuzzle.util.Renderable; import java.awt.Font; import java.awt.FontFormatException; +import java.awt.FontMetrics; import java.awt.Graphics2D; import java.awt.Image; import java.awt.Toolkit; @@ -227,6 +228,16 @@ public class Display { g.setFont(theFont); } + public static Font getFont() + { + if (theFont == null) + { + initalizeFont(); + } + + return theFont; + } + public static BufferedImage createCanvas(int width, int height) { BufferedImage temp = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB); @@ -234,4 +245,15 @@ public class Display { return temp; } + private static FontMetrics fontMetrics = null; + public static FontMetrics getFontMetrics() + { + if (fontMetrics == null) + { + fontMetrics = createCanvas(1, 1).createGraphics().getFontMetrics(getFont()); + } + + return fontMetrics; + } + } diff --git a/src/com/fourisland/fourpuzzle/window/ChoiceWindow.java b/src/com/fourisland/fourpuzzle/window/ChoiceWindow.java index b2a0af7..ef57226 100755 --- a/src/com/fourisland/fourpuzzle/window/ChoiceWindow.java +++ b/src/com/fourisland/fourpuzzle/window/ChoiceWindow.java @@ -39,18 +39,15 @@ public class ChoiceWindow implements Renderable { numChoices = choices.size(); this.center = center; - Graphics2D g3 = Display.createCanvas(1, 1).createGraphics(); - Display.setFont(g3); - for (String choice : choices) { - int l = g3.getFontMetrics().stringWidth(choice); + int l = Display.getFontMetrics().stringWidth(choice); if (l > getWidth()) { width = l; } - height += g3.getFontMetrics().getHeight() + SPACER; + height += Display.getFontMetrics().getHeight() + SPACER; } cacheBase = Window.Default.getImage(width, height); @@ -85,7 +82,7 @@ public class ChoiceWindow implements Renderable { g2.setPaint(new TexturePaint(SystemGraphic.getTextColor(), new Rectangle(tx, ty, fw, fh))); g2.drawString(choice, tx, ty); - ty+=(SPACER+g2.getFontMetrics().getHeight()); + ty+=(SPACER+fh); } } diff --git a/src/com/fourisland/fourpuzzle/window/MessageWindow.java b/src/com/fourisland/fourpuzzle/window/MessageWindow.java index 595fb55..fb90f04 100644 --- a/src/com/fourisland/fourpuzzle/window/MessageWindow.java +++ b/src/com/fourisland/fourpuzzle/window/MessageWindow.java @@ -28,15 +28,7 @@ import java.util.concurrent.CountDownLatch; public class MessageWindow implements Renderable { private static final int SPACER = 4; - private static final int HEIGHT; - - static - { - BufferedImage d = Display.createCanvas(1, 1); - Display.setFont(d.createGraphics()); - - HEIGHT = 3*(d.createGraphics().getFontMetrics().getHeight()+SPACER); - } + private static final int HEIGHT = (4*(Display.getFontMetrics().getHeight()+SPACER)); String message; private volatile List messages; @@ -191,7 +183,7 @@ public class MessageWindow implements Renderable { g2.setPaint(new TexturePaint(SystemGraphic.getTextColor(), new Rectangle(tx, ty, fw, fh))); g2.drawString(message.substring(0, Math.min(toPrint, message.length())), tx, ty); - ty+=(SPACER+g2.getFontMetrics().getHeight()); + ty+=(SPACER+fh); toPrint -= Math.min(toPrint, message.length()); } -- cgit 1.4.1