From c78e8e64484b887cbdd2f5e903ce25828c93e08a Mon Sep 17 00:00:00 2001 From: Matthias Schiffer Date: Tue, 31 May 2011 01:50:54 +0200 Subject: StonePainter: Paint stones lazily git-svn-id: svn://sunsvr01.isp.uni-luebeck.de/swproj13/trunk@336 72836036-5685-4462-b002-a69064685172 --- src/jrummikub/view/impl/StonePainter.java | 161 ++++++++++++++---------------- 1 file changed, 77 insertions(+), 84 deletions(-) diff --git a/src/jrummikub/view/impl/StonePainter.java b/src/jrummikub/view/impl/StonePainter.java index d9bbdba..572ba76 100644 --- a/src/jrummikub/view/impl/StonePainter.java +++ b/src/jrummikub/view/impl/StonePainter.java @@ -60,8 +60,7 @@ class StonePainter { int g = (int) (color.getGreen() * BRIGHTER_SCALE); int b = (int) (color.getBlue() * BRIGHTER_SCALE); - return new Color(r > 255 ? 255 : r, g > 255 ? 255 : g, b > 255 ? 255 - : b); + return new Color(r > 255 ? 255 : r, g > 255 ? 255 : g, b > 255 ? 255 : b); } private static Color hover(Color color) { @@ -69,28 +68,27 @@ class StonePainter { int g = (int) (color.getGreen() * HOVER_RATIO + 255 * (1 - HOVER_RATIO)); int b = (int) (color.getBlue() * HOVER_RATIO + 255 * (1 - HOVER_RATIO)); - return new Color(r > 255 ? 255 : r, g > 255 ? 255 : g, b > 255 ? 255 - : b); + return new Color(r > 255 ? 255 : r, g > 255 ? 255 : g, b > 255 ? 255 : b); } private static Color getColor(StoneColor color) { switch (color) { - case BLACK: - return new Color(0.15f, 0.15f, 0.15f); - case BLUE: - return new Color(0.0f, 0.0f, 1.0f); - case ORANGE: - return new Color(1.0f, 0.4f, 0.0f); - case RED: - return new Color(0.9f, 0.0f, 0.25f); - case BROWN: - return new Color(0.5f, 0.25f, 0.0f); - case GREEN: - return new Color(0.0f, 0.75f, 0.0f); - case VIOLET: - return new Color(0.75f, 0.325f, 0.75f); - case WHITE: - return new Color(1.0f, 1.0f, 1.0f); + case BLACK: + return new Color(0.15f, 0.15f, 0.15f); + case BLUE: + return new Color(0.0f, 0.0f, 1.0f); + case ORANGE: + return new Color(1.0f, 0.4f, 0.0f); + case RED: + return new Color(0.9f, 0.0f, 0.25f); + case BROWN: + return new Color(0.5f, 0.25f, 0.0f); + case GREEN: + return new Color(0.0f, 0.75f, 0.0f); + case VIOLET: + return new Color(0.75f, 0.325f, 0.75f); + case WHITE: + return new Color(1.0f, 1.0f, 1.0f); } return null; @@ -100,7 +98,7 @@ class StonePainter { * Sets the new grid scale * * @param scale - * the new scale + * the new scale */ public void setScale(float scale) { this.scale = scale; @@ -109,14 +107,14 @@ class StonePainter { this.scale = 1; } - prepaint(); + resetPrepaint(); } /** * @param x - * x position in screen coordinates + * x position in screen coordinates * @param y - * y position in screen coordinates + * y position in screen coordinates * @return position in grid coordinates */ public Position calculatePosition(int x, int y) { @@ -161,56 +159,69 @@ class StonePainter { return img; } - private Map prepaintColor(Color fg, Color bg) { - Map images = new HashMap(); + private BufferedImage getStoneImage(StoneColor color, int value, boolean selected, + boolean hovered) { + Map> stoneMap; + + if (selected) { + if (hovered) { + stoneMap = hoveredSelectedStones; + } else { + stoneMap = selectedStones; + } + } else { + if (hovered) { + stoneMap = hoveredStones; + } else { + stoneMap = defaultStones; + } + } + + BufferedImage image = stoneMap.get(color).get(value); + + if (image == null) { + Color background = BACKGROUND_COLOR; + Color foreground = getColor(color); + if (selected) { + background = background.darker(); + foreground = foreground.darker(); + } + if (hovered) { + background = hover(background); + foreground = hover(foreground); + } + + image = prepaintStone(foreground, background, value); - for (int i = 0; i <= 13; ++i) { - images.put(i, prepaintStone(fg, bg, i)); + stoneMap.get(color).put(value, image); } - return images; + return image; } - private void prepaint() { + private void resetPrepaint() { defaultStones = new HashMap>(); selectedStones = new HashMap>(); hoveredStones = new HashMap>(); hoveredSelectedStones = new HashMap>(); - Color defaultBackground = BACKGROUND_COLOR; - Color selectedBackground = BACKGROUND_COLOR.darker(); - Color hoveredBackground = hover(defaultBackground); - Color hoveredSelectedBackground = hover(selectedBackground); - for (StoneColor color : StoneColor.values()) { - Color defaultFg = getColor(color); - Color selectedFg = defaultFg.darker(); - Color hoveredFg = hover(defaultFg); - Color hoveredSelectedFg = hover(selectedFg); - - defaultStones.put(color, - prepaintColor(defaultFg, defaultBackground)); - selectedStones.put(color, - prepaintColor(selectedFg, selectedBackground)); - hoveredStones.put(color, - prepaintColor(hoveredFg, hoveredBackground)); - hoveredSelectedStones - .put(color, - prepaintColor(hoveredSelectedFg, - hoveredSelectedBackground)); + defaultStones.put(color, new HashMap()); + selectedStones.put(color, new HashMap()); + hoveredStones.put(color, new HashMap()); + hoveredSelectedStones.put(color, new HashMap()); } } /** * @param scale - * the scaling factor for the grid coordinates + * the scaling factor for the grid coordinates */ StonePainter(float scale) { setScale(scale); } - private void paintStoneBackground(Graphics2D g, Rectangle r, - Color background) { + private void paintStoneBackground(Graphics2D g, Rectangle r, Color background) { // Paint background g.setColor(background); g.fillRect(r.x, r.y, r.width, r.height); @@ -310,9 +321,8 @@ class StonePainter { pos + (fm.getAscent() - fm.getDescent()) / 2 + 1); } g.setColor(color); - g.drawString(value, - (int) (r.x + r.width / 2 - stringRect.getWidth() / 2), pos - + (fm.getAscent() - fm.getDescent()) / 2); + g.drawString(value, (int) (r.x + r.width / 2 - stringRect.getWidth() / 2), + pos + (fm.getAscent() - fm.getDescent()) / 2); } private void paintCircle(Graphics2D g, Rectangle r, Color background) { @@ -321,27 +331,26 @@ class StonePainter { // Paint circle g.setColor(background.darker()); - g.drawArc(r.x + r.width / 2 - size / 2, pos - size / 2, size, size, 50, - 170); + g.drawArc(r.x + r.width / 2 - size / 2, pos - size / 2, size, size, 50, 170); g.setColor(brighter(background)); - g.drawArc((int) (r.x + r.width / 2 - size / 2), pos - size / 2, size, - size, -130, 170); + g.drawArc((int) (r.x + r.width / 2 - size / 2), pos - size / 2, size, size, + -130, 170); } /** * Paints a stone * * @param g - * the graphics context to paint the stone on + * the graphics context to paint the stone on * @param stone - * the stone to paint + * the stone to paint * @param p - * the position of the stone + * the position of the stone * @param selected - * if selected is true the stone will be painted darker + * if selected is true the stone will be painted darker * @param hovered - * if hovered is true the stone will be painted brighter + * if hovered is true the stone will be painted brighter */ public void paintStone(Graphics2D g, Stone stone, Position p, boolean selected, boolean hovered) { @@ -349,27 +358,11 @@ class StonePainter { int height = getStoneHeight(); int x = Math.round(p.getX() * width), y = Math.round(p.getY() * height); - Map> stoneMap; - - if (selected) { - if (hovered) { - stoneMap = hoveredSelectedStones; - } else { - stoneMap = selectedStones; - } - } else { - if (hovered) { - stoneMap = hoveredStones; - } else { - stoneMap = defaultStones; - } - } - if (stone.isJoker()) { - g.drawImage(stoneMap.get(stone.getColor()).get(0), x, y, null); + g.drawImage(getStoneImage(stone.getColor(), 0, selected, hovered), x, y, null); } else { - g.drawImage(stoneMap.get(stone.getColor()).get(stone.getValue()), - x, y, null); + g.drawImage(getStoneImage(stone.getColor(), stone.getValue(), selected, hovered), x, y, + null); } } } -- cgit v1.2.3