From 652e8149919c8a5f7ad0161420e92a87d255fe00 Mon Sep 17 00:00:00 2001 From: Matthias Schiffer Date: Thu, 28 Apr 2011 19:46:25 +0200 Subject: Added StonePainter git-svn-id: svn://sunsvr01.isp.uni-luebeck.de/swproj13/trunk@13 72836036-5685-4462-b002-a69064685172 --- src/jrummikub/view/Board.java | 11 +++++ src/jrummikub/view/StonePainter.java | 93 ++++++++++++++++++++++++++++++++++++ 2 files changed, 104 insertions(+) create mode 100644 src/jrummikub/view/StonePainter.java (limited to 'src/jrummikub/view') diff --git a/src/jrummikub/view/Board.java b/src/jrummikub/view/Board.java index b73b00a..d0a3173 100644 --- a/src/jrummikub/view/Board.java +++ b/src/jrummikub/view/Board.java @@ -6,6 +6,9 @@ import java.awt.Graphics; import javax.swing.ImageIcon; import javax.swing.JPanel; +import jrummikub.model.Stone; +import jrummikub.model.StoneColor; + @SuppressWarnings("serial") public class Board extends JPanel implements IBoard { private final static ImageIcon background = new ImageIcon(Board.class.getResource("resource/wood.png")); @@ -23,5 +26,13 @@ public class Board extends JPanel implements IBoard { } // TODO Rest of painting code + + // FIXME Test code + StonePainter.paintStone(g, new Stone(1, StoneColor.ORANGE, false), + 0, 0, StonePainter.BOARD_SCALE); + StonePainter.paintStone(g, new Stone(10, StoneColor.BLUE, false), + 1, 0, StonePainter.BOARD_SCALE); + StonePainter.paintStone(g, new Stone(5, StoneColor.RED, false), + 0.5f, 1, StonePainter.BOARD_SCALE); } } diff --git a/src/jrummikub/view/StonePainter.java b/src/jrummikub/view/StonePainter.java new file mode 100644 index 0000000..dbdf3c8 --- /dev/null +++ b/src/jrummikub/view/StonePainter.java @@ -0,0 +1,93 @@ +package jrummikub.view; + +import java.awt.Color; +import java.awt.Font; +import java.awt.FontMetrics; +import java.awt.Graphics; +import java.awt.Graphics2D; +import java.awt.RenderingHints; +import java.awt.geom.Rectangle2D; + +import jrummikub.model.Stone; +import jrummikub.model.StoneColor; + +class StonePainter { + private static final float ASPECT_RATIO = 0.75f; + private static final float DEFAULT_WIDTH = 40; + private static final float CIRCLE_WIDTH = 0.5f; + + private static final Color BACKGROUND_COLOR = new Color(0.9f, 0.9f, 0.6f); + + public static final float BOARD_SCALE = 75.0f/DEFAULT_WIDTH*ASPECT_RATIO; + + + private static Color getColor(StoneColor color) { + switch(color) { + case BLACK: + return Color.BLACK; + case BLUE: + return Color.BLUE; + case ORANGE: + return new Color(1.0f, 0.6f, 0); + case RED: + return Color.RED; + } + + return null; + } + + public static void paintStone(Graphics g, Stone stone, float x, float y, float scale) { + if (g instanceof Graphics2D) { + ((Graphics2D)g).setRenderingHint(RenderingHints.KEY_ANTIALIASING, + RenderingHints.VALUE_ANTIALIAS_ON); + } + + int width = (int)(DEFAULT_WIDTH*scale); + int height = (int)(width/ASPECT_RATIO); + + int xpos = (int)(x*width); + int ypos = (int)(y*height); + + // Paint background + g.setColor(BACKGROUND_COLOR); + g.fillRect(xpos, ypos, width, height); + + // Paint bevel border + g.setColor(BACKGROUND_COLOR.brighter().brighter()); + g.fillRect(xpos, ypos, 1, height); + g.setColor(BACKGROUND_COLOR.brighter()); + g.fillRect(xpos+1, ypos+1, 1, height-2); + + g.setColor(BACKGROUND_COLOR.brighter().brighter()); + g.fillRect(xpos, ypos, width, 1); + g.setColor(BACKGROUND_COLOR.brighter()); + g.fillRect(xpos+1, ypos+1, width-2, 1); + + g.setColor(BACKGROUND_COLOR.darker().darker()); + g.fillRect(xpos+width-1, ypos, 1, height); + g.setColor(BACKGROUND_COLOR.darker()); + g.fillRect(xpos+width-2, ypos+1, 1, height-2); + + g.setColor(BACKGROUND_COLOR.darker().darker()); + g.fillRect(xpos, ypos+height-1, width, 1); + g.setColor(BACKGROUND_COLOR.darker()); + g.fillRect(xpos+1, ypos+height-2, width-2, 1); + + // Paint number + g.setFont(new Font("SansSerif", Font.BOLD, height/4)); + FontMetrics fm = g.getFontMetrics(); + String value = Integer.toString(stone.getValue()); + Rectangle2D stringRect = fm.getStringBounds(value, g); + + g.setColor(getColor(stone.getColor()).darker()); + g.drawString(value, (int)(xpos+width/2-stringRect.getWidth()/2)+1, ypos+height/4+(fm.getAscent()-fm.getDescent())/2+1); + g.setColor(getColor(stone.getColor())); + g.drawString(value, (int)(xpos+width/2-stringRect.getWidth()/2), ypos+height/4+(fm.getAscent()-fm.getDescent())/2); + + g.setColor(BACKGROUND_COLOR.darker()); + g.drawArc((int)(xpos+width/2-width*CIRCLE_WIDTH/2), (int)(ypos+height*0.65f-width*CIRCLE_WIDTH/2), (int)(width*CIRCLE_WIDTH), (int)(width*CIRCLE_WIDTH), 50, 170); + + g.setColor(BACKGROUND_COLOR.brighter()); + g.drawArc((int)(xpos+width/2-width*CIRCLE_WIDTH/2), (int)(ypos+height*0.65f-width*CIRCLE_WIDTH/2), (int)(width*CIRCLE_WIDTH), (int)(width*CIRCLE_WIDTH), -130, 170); + } +} -- cgit v1.2.3