From 11651c651b6472ca4130484063c220a800cca587 Mon Sep 17 00:00:00 2001 From: Starla Insigna Date: Tue, 10 Feb 2009 15:49:48 -0500 Subject: Engine: Refactored SystemGraphic transparency Previously, every time a transparent section of SystemGraphic had to be rendered, it would be to be fed through Toolkit, FilteredImageSource, TransparentImageFilter and converted from a BufferedImage to an Image to a BufferedImage again. This has been replaced (and optimized) by preforming this when the SystemGraphic is loaded into memory, instead of every time it is used. Refs #5 --- .../fourisland/fourpuzzle/window/ChoiceWindow.java | 8 +--- .../fourpuzzle/window/MessageWindow.java | 6 +-- .../fourpuzzle/window/SystemGraphic.java | 50 +++++++++++++++++++--- src/com/fourisland/fourpuzzle/window/Window.java | 25 ++++++++--- 4 files changed, 65 insertions(+), 24 deletions(-) (limited to 'src') diff --git a/src/com/fourisland/fourpuzzle/window/ChoiceWindow.java b/src/com/fourisland/fourpuzzle/window/ChoiceWindow.java index 6a82c6a..bf193c1 100755 --- a/src/com/fourisland/fourpuzzle/window/ChoiceWindow.java +++ b/src/com/fourisland/fourpuzzle/window/ChoiceWindow.java @@ -5,7 +5,6 @@ package com.fourisland.fourpuzzle.window; -import com.fourisland.fourpuzzle.window.SystemGraphic; import com.fourisland.fourpuzzle.Audio; import com.fourisland.fourpuzzle.database.Database; import com.fourisland.fourpuzzle.database.Sound; @@ -54,11 +53,7 @@ public class ChoiceWindow { width += SPACER*2; - cacheBase = new BufferedImage(Window.Default.getFullWidth(width), Window.Default.getFullHeight(height), BufferedImage.TYPE_INT_ARGB); - Graphics2D g2 = cacheBase.createGraphics(); - - g2.drawImage(SystemGraphic.getMessageBackground(), 1, 1, Window.Default.getFullWidth(width)-2, Window.Default.getFullHeight(height)-2, null); - g2.drawImage(Window.Default.getImage(width, height), 0, 0, null); + cacheBase = Window.Default.getImage(width, height); } public void render(Graphics2D g2, int x, int y) @@ -81,7 +76,6 @@ public class ChoiceWindow { if (getSelected().equals(choice)) { - g2.drawImage(SystemGraphic.getSelectionBackground(), tx-1, ty-fh+3, fw+SPACER-2, fh+SPACER-2, null); g2.drawImage(Window.Selector.getImage(fw-Window.Selector.getLeftX(), fh-Window.Selector.getTopY()), tx-SPACER, ty-fh, null); } diff --git a/src/com/fourisland/fourpuzzle/window/MessageWindow.java b/src/com/fourisland/fourpuzzle/window/MessageWindow.java index 9e482ba..ea34ab9 100644 --- a/src/com/fourisland/fourpuzzle/window/MessageWindow.java +++ b/src/com/fourisland/fourpuzzle/window/MessageWindow.java @@ -38,11 +38,7 @@ public class MessageWindow { initalizeMessages(message, new BufferedImage(1, 1, BufferedImage.TYPE_INT_ARGB).createGraphics()); - cacheBase = new BufferedImage(Game.WIDTH, Window.Default.getFullHeight(HEIGHT), BufferedImage.TYPE_INT_ARGB); - Graphics2D g2 = cacheBase.createGraphics(); - - g2.drawImage(SystemGraphic.getMessageBackground(), 1, 1, Game.WIDTH-2, Window.Default.getFullHeight(HEIGHT)-2, null); - g2.drawImage(Window.Default.getImage(width, HEIGHT), 0, 0, null); + cacheBase = Window.Default.getImage(width, HEIGHT); } private void initalizeMessages(String message, Graphics2D g) diff --git a/src/com/fourisland/fourpuzzle/window/SystemGraphic.java b/src/com/fourisland/fourpuzzle/window/SystemGraphic.java index 99941d9..bc13d0f 100755 --- a/src/com/fourisland/fourpuzzle/window/SystemGraphic.java +++ b/src/com/fourisland/fourpuzzle/window/SystemGraphic.java @@ -6,10 +6,12 @@ package com.fourisland.fourpuzzle.window; import com.fourisland.fourpuzzle.util.ObjectLoader; +import com.fourisland.fourpuzzle.util.TransparentPixelFilter; import com.fourisland.fourpuzzle.window.Window.SystemArea; -import java.awt.Color; import java.awt.Rectangle; +import java.awt.Toolkit; import java.awt.image.BufferedImage; +import java.awt.image.FilteredImageSource; /** * @@ -17,7 +19,7 @@ import java.awt.image.BufferedImage; */ public class SystemGraphic { - private static BufferedImage systemGraphic; + private static BufferedImage systemGraphic = null; private static String filename = "System"; public static void setGraphic(String filename) { @@ -26,32 +28,70 @@ public class SystemGraphic { public static void initalize() { - systemGraphic = ObjectLoader.getImage("Picture", filename); + BufferedImage temp = ObjectLoader.getImage("Picture", filename); + systemGraphic = new BufferedImage(160, 80, BufferedImage.TYPE_INT_ARGB); + + systemGraphic.createGraphics().drawImage(Toolkit.getDefaultToolkit().createImage(new FilteredImageSource(temp.getSource(), new TransparentPixelFilter(temp.getRGB(159, 0)))),0,0,null); } public static BufferedImage getMessageBackground() { + if (systemGraphic == null) + { + initalize(); + } + return systemGraphic.getSubimage(0, 0, 32, 32); } public static BufferedImage getSelectionBackground() { + if (systemGraphic == null) + { + initalize(); + } + return systemGraphic.getSubimage(Window.Selector.getX(SystemArea.TOP_LEFT)+15, 15, 1, 1); } public static BufferedImage getChoiceArea(Rectangle sca) { + if (systemGraphic == null) + { + initalize(); + } + return systemGraphic.getSubimage(sca.x, sca.y, sca.width, sca.height); } public static BufferedImage getTextColor() { + if (systemGraphic == null) + { + initalize(); + } + return systemGraphic.getSubimage(0, 48, 16, 16); } - public static Color getTransparentColor() + public static BufferedImage getUpArrow() { - return new Color(systemGraphic.getRGB(159, 0)); + if (systemGraphic == null) + { + initalize(); + } + + return systemGraphic.getSubimage(43, 10, 10, 6); + } + + public static BufferedImage getDownArrow() + { + if (systemGraphic == null) + { + initalize(); + } + + return systemGraphic.getSubimage(43, 18, 10, 6); } } diff --git a/src/com/fourisland/fourpuzzle/window/Window.java b/src/com/fourisland/fourpuzzle/window/Window.java index 8cdaf9a..da3216a 100644 --- a/src/com/fourisland/fourpuzzle/window/Window.java +++ b/src/com/fourisland/fourpuzzle/window/Window.java @@ -6,13 +6,9 @@ package com.fourisland.fourpuzzle.window; import com.fourisland.fourpuzzle.util.Interval; -import com.fourisland.fourpuzzle.util.TransparentPixelFilter; import java.awt.Graphics2D; -import java.awt.Image; import java.awt.Rectangle; -import java.awt.Toolkit; import java.awt.image.BufferedImage; -import java.awt.image.FilteredImageSource; /** * @@ -20,7 +16,13 @@ import java.awt.image.FilteredImageSource; */ public enum Window { - Default(32), + Default(32) + { + protected BufferedImage getBackground() + { + return SystemGraphic.getMessageBackground(); + } + }, Selector(64) { Interval in = Interval.createTickInterval(4); @@ -41,6 +43,11 @@ public enum Window return super.getX(sa); } } + + protected BufferedImage getBackground() + { + return SystemGraphic.getSelectionBackground(); + } }; protected enum SystemArea @@ -118,11 +125,15 @@ public enum Window return getHeight(SystemArea.TOP_LEFT); } - public Image getImage(int width, int height) + protected abstract BufferedImage getBackground(); + + public BufferedImage getImage(int width, int height) { BufferedImage temp = new BufferedImage(getFullWidth(width), getFullHeight(height), BufferedImage.TYPE_INT_ARGB); Graphics2D g = temp.createGraphics(); + g.drawImage(getBackground(), 1, 1, getFullWidth(width)-2, getFullHeight(height)-2, null); + g.drawImage(SystemGraphic.getChoiceArea(getBounds(SystemArea.TOP_LEFT)), 0, 0, null); g.drawImage(SystemGraphic.getChoiceArea(getBounds(SystemArea.TOP)), getWidth(SystemArea.TOP_LEFT), 0, width, getHeight(SystemArea.TOP), null); g.drawImage(SystemGraphic.getChoiceArea(getBounds(SystemArea.TOP_RIGHT)), getWidth(SystemArea.TOP_LEFT)+width, 0, null); @@ -132,6 +143,6 @@ public enum Window g.drawImage(SystemGraphic.getChoiceArea(getBounds(SystemArea.BOTTOM_RIGHT)), getWidth(SystemArea.BOTTOM_RIGHT)+width, getHeight(SystemArea.TOP_RIGHT)+height, null); g.drawImage(SystemGraphic.getChoiceArea(getBounds(SystemArea.RIGHT)), (width+getWidth(SystemArea.TOP_LEFT)+getWidth(SystemArea.TOP_RIGHT))-getWidth(SystemArea.RIGHT), getHeight(SystemArea.TOP_RIGHT), getWidth(SystemArea.RIGHT), height, null); - return Toolkit.getDefaultToolkit().createImage(new FilteredImageSource(temp.getSource(), new TransparentPixelFilter(SystemGraphic.getTransparentColor().getRGB()))); + return temp; } } \ No newline at end of file -- cgit 1.4.1