# HG changeset patch # User Jaroslav Tulach # Date 1253132891 -7200 # Node ID 786df32c496b1aa8a062b89cdecb928cb037db8a # Parent 63ec24914da9bf69d05dfcdc2f927c1543fd18e2 First attempt to show the board as image diff -r 63ec24914da9 -r 786df32c496b freemarkerdor/src/main/resources/cz/xelfi/quoridor/freemarkerdor/UI/game.fmt --- a/freemarkerdor/src/main/resources/cz/xelfi/quoridor/freemarkerdor/UI/game.fmt Tue Sep 15 21:46:30 2009 +0200 +++ b/freemarkerdor/src/main/resources/cz/xelfi/quoridor/freemarkerdor/UI/game.fmt Wed Sep 16 22:28:11 2009 +0200 @@ -85,6 +85,7 @@ + board
${doc.game.board}

${bundle.MOVES}

diff -r 63ec24914da9 -r 786df32c496b visidor/src/main/java/cz/xelfi/quoridor/visidor/Viewer.java --- a/visidor/src/main/java/cz/xelfi/quoridor/visidor/Viewer.java Tue Sep 15 21:46:30 2009 +0200 +++ b/visidor/src/main/java/cz/xelfi/quoridor/visidor/Viewer.java Wed Sep 16 22:28:11 2009 +0200 @@ -1,8 +1,3 @@ -/* - * To change this template, choose Tools | Templates - * and open the template in the editor. - */ - package cz.xelfi.quoridor.visidor; import cz.xelfi.quoridor.Board; @@ -17,13 +12,9 @@ import java.awt.Rectangle; import javax.swing.JFrame; import javax.swing.JPanel; -import org.netbeans.api.visual.action.ActionFactory; import org.netbeans.api.visual.action.MoveProvider; -import org.netbeans.api.visual.anchor.AnchorFactory; import org.netbeans.api.visual.border.BorderFactory; -import org.netbeans.api.visual.widget.ConnectionWidget; import org.netbeans.api.visual.widget.ImageWidget; -import org.netbeans.api.visual.widget.LabelWidget; import org.netbeans.api.visual.widget.LayerWidget; import org.netbeans.api.visual.widget.Scene; import org.netbeans.api.visual.widget.Widget; @@ -70,41 +61,29 @@ private void view(Scene scene, Board b) { scene.removeChildren(); + drawBoard(scene, b); + } + + static void drawBoard(Scene scene, Board b) { // Scene layerBoard = scene; + scene.setPreferredLocation(new Point(0, 0)); final LayerWidget layerLines = new LayerWidget(scene); scene.addChild(layerLines); final LayerWidget layerBoard = new LayerWidget(scene); layerLines.addChild(layerBoard); final int WDTH = 50; final int HGHT = 50; -// conn.setSourceAnchor(ahoj); - Widget[][] fields = new Widget[10][]; - for (int i = 0; i < fields.length; i++) { - fields[i] = new Widget[10]; - for (int j = 0; j < fields[i].length; j++) { - LabelWidget w = new LabelWidget(scene); + + + for (int i = 0; i < 9; i++) { + for (int j = 0; j < 9; j++) { + Widget w = new FieldWidget(scene); w.setPreferredBounds(new Rectangle(i * WDTH, j * HGHT, WDTH, HGHT)); layerBoard.addChild(w); - fields[i][j] = w; - } - } - for (int i = 0; i < fields.length; i++) { - for (int j = 0; j < fields[i].length; j++) { - if (i > 0) { - ConnectionWidget horiz = new ConnectionWidget(scene); - horiz.setSourceAnchor(AnchorFactory.createCenterAnchor(fields[i - 1][j])); - horiz.setTargetAnchor(AnchorFactory.createCenterAnchor(fields[i][j])); - scene.addChild(horiz); - } - if (j > 0) { - ConnectionWidget vert = new ConnectionWidget(scene); - vert.setSourceAnchor(AnchorFactory.createCenterAnchor(fields[i][j - 1])); - vert.setTargetAnchor(AnchorFactory.createCenterAnchor(fields[i][j])); - scene.addChild(vert); - } } } +/* ImageWidget horizontalWall = new ImageWidget(scene); horizontalWall.setPreferredBounds(new Rectangle(550, 300, 90, 10)); horizontalWall.setBackground(Color.BLACK); @@ -120,7 +99,7 @@ verticalWall.setBorder(BorderFactory.createLineBorder()); verticalWall.setOpaque(true); layerBoard.addChild(verticalWall); - +*/ for (Fence f : b.getFences()) { Rectangle r = fenceRectangle(f.getColumn(), f.getRow(), f.getOrientation()); ImageWidget fenceWall = new ImageWidget(scene); @@ -132,19 +111,10 @@ } int cnt = 0; + Color[] colors = { Color.WHITE, Color.BLACK, Color.ORANGE, Color.MAGENTA }; for (Player p : b.getPlayers()) { - LabelWidget lw = new LabelWidget(scene); - lw.setLabel("" + p.getFences()); - if (cnt == 0) { - lw.setPreferredBounds(new Rectangle(550, 200, 50, 50)); - } else { - lw.setPreferredBounds(new Rectangle(550, 450, 50, 50)); - } - layerBoard.addChild(lw); - - PlayerWidget pw = new PlayerWidget(scene); - System.err.println("p: " + p); - pw.setPreferredBounds(new Rectangle(p.getColumn() * 50 + 25, p.getRow() * 50 + 25, 50, 50)); + PlayerWidget pw = new PlayerWidget(scene, colors[cnt]); + pw.setPreferredBounds(new Rectangle(p.getColumn() * WDTH, p.getRow() * HGHT, WDTH, HGHT)); layerBoard.addChild(pw); cnt++; @@ -160,7 +130,7 @@ } return new Rectangle( - (column - 'A' + 1) * 50 + 25 - w, row * 50 + 25 - h, 2 * w, 2 * h + (column - 'A' + 1) * 50 - w, row * 50 - h, 2 * w, 2 * h ); } @@ -259,7 +229,28 @@ } private static final class PlayerWidget extends Widget { - public PlayerWidget(Scene s) { + private final Color color; + + public PlayerWidget(Scene s, Color color) { + super(s); + this.color = color; + } + + @Override + protected Rectangle calculateClientArea() { + return getBounds(); + } + + @Override + protected void paintWidget() { + Graphics2D g = getGraphics(); + Rectangle b = getBounds(); + g.setColor(color); + g.fillOval(b.x + 8, b.y + 8, b.width - 16, b.height - 16); + } + } + private static final class FieldWidget extends Widget { + public FieldWidget(Scene s) { super(s); } @@ -272,7 +263,8 @@ protected void paintWidget() { Graphics2D g = getGraphics(); Rectangle b = getBounds(); - g.drawOval(b.x + 5, b.y + 5, b.width - 10, b.height - 10); + g.setColor(Color.lightGray); + g.fillRect(b.x + 5, b.y + 5, b.width - 10, b.height - 10); } } } diff -r 63ec24914da9 -r 786df32c496b visidor/src/main/java/cz/xelfi/quoridor/visidor/Visidor.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/visidor/src/main/java/cz/xelfi/quoridor/visidor/Visidor.java Wed Sep 16 22:28:11 2009 +0200 @@ -0,0 +1,26 @@ +package cz.xelfi.quoridor.visidor; + +import cz.xelfi.quoridor.Board; +import java.awt.Image; +import java.io.IOException; +import org.netbeans.api.visual.export.SceneExporter; +import org.netbeans.api.visual.widget.Scene; + +/** Utilities to draw board into an image. + * + * @author Jaroslav Tulach + */ +public final class Visidor { + private Visidor() {} + + public static Image draw(Board b) { + Scene scene = new Scene(); + Viewer.drawBoard(scene, b); + try { + return SceneExporter.createImage(scene, null, SceneExporter.ImageType.PNG, SceneExporter.ZoomType.ACTUAL_SIZE, true, false, 10, 450, 450); + } catch (IOException ex) { + throw new IllegalStateException(); + } + } + +} diff -r 63ec24914da9 -r 786df32c496b visidor/src/test/java/cz/xelfi/quoridor/visidor/ViewerTest.java --- a/visidor/src/test/java/cz/xelfi/quoridor/visidor/ViewerTest.java Tue Sep 15 21:46:30 2009 +0200 +++ b/visidor/src/test/java/cz/xelfi/quoridor/visidor/ViewerTest.java Wed Sep 16 22:28:11 2009 +0200 @@ -1,5 +1,7 @@ package cz.xelfi.quoridor.visidor; +import cz.xelfi.quoridor.Board; +import java.awt.Image; import junit.framework.Test; import junit.framework.TestCase; import junit.framework.TestSuite; @@ -17,6 +19,8 @@ return new TestSuite(ViewerTest.class); } - public void testEmpty() { + public void testDrawBoard() { + Image img = Visidor.draw(Board.empty()); + assertNotNull("Something has been drawn", img); } } diff -r 63ec24914da9 -r 786df32c496b webidor/pom.xml --- a/webidor/pom.xml Tue Sep 15 21:46:30 2009 +0200 +++ b/webidor/pom.xml Wed Sep 16 22:28:11 2009 +0200 @@ -72,6 +72,11 @@ 1.0 jar + + ${project.groupId} + visidor + 1.0 + @@ -97,3 +102,4 @@ + diff -r 63ec24914da9 -r 786df32c496b webidor/src/main/java/cz/xelfi/quoridor/webidor/resources/Games.java --- a/webidor/src/main/java/cz/xelfi/quoridor/webidor/resources/Games.java Tue Sep 15 21:46:30 2009 +0200 +++ b/webidor/src/main/java/cz/xelfi/quoridor/webidor/resources/Games.java Wed Sep 16 22:28:11 2009 +0200 @@ -29,6 +29,8 @@ import cz.xelfi.quoridor.IllegalPositionException; import cz.xelfi.quoridor.webidor.*; import cz.xelfi.quoridor.Move; +import cz.xelfi.quoridor.visidor.Visidor; +import java.awt.Image; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; @@ -118,6 +120,17 @@ @GET @Path("{id}") + @Produces("image/png") + public Image getBoardImage(@PathParam("id") String id) { + Game g = findGame(id); + if (g == null) { + throw new IllegalArgumentException("Unknown game " + id); + } + return Visidor.draw(g.getBoard()); + } + + @GET + @Path("{id}") @Produces({ MediaType.APPLICATION_JSON, MediaType.TEXT_XML }) public Game getBoardInfo(@PathParam("id") String id) { Game g = findGame(id);