First attempt to show the board as image display-image
authorJaroslav Tulach <jtulach@netbeans.org>
Wed, 16 Sep 2009 22:28:11 +0200
branchdisplay-image
changeset 91786df32c496b
parent 90 63ec24914da9
First attempt to show the board as image
freemarkerdor/src/main/resources/cz/xelfi/quoridor/freemarkerdor/UI/game.fmt
visidor/src/main/java/cz/xelfi/quoridor/visidor/Viewer.java
visidor/src/main/java/cz/xelfi/quoridor/visidor/Visidor.java
visidor/src/test/java/cz/xelfi/quoridor/visidor/ViewerTest.java
webidor/pom.xml
webidor/src/main/java/cz/xelfi/quoridor/webidor/resources/Games.java
     1.1 --- a/freemarkerdor/src/main/resources/cz/xelfi/quoridor/freemarkerdor/UI/game.fmt	Tue Sep 15 21:46:30 2009 +0200
     1.2 +++ b/freemarkerdor/src/main/resources/cz/xelfi/quoridor/freemarkerdor/UI/game.fmt	Wed Sep 16 22:28:11 2009 +0200
     1.3 @@ -85,6 +85,7 @@
     1.4                <input type="submit" value="${bundle.RESIGN}" />
     1.5            </form>
     1.6        </#if>
     1.7 +      <img src="/api/games/${doc.game.id.@id}" alt="board"/>
     1.8        <pre>${doc.game.board}</pre>
     1.9  
    1.10        <h3>${bundle.MOVES}</h3>
     2.1 --- a/visidor/src/main/java/cz/xelfi/quoridor/visidor/Viewer.java	Tue Sep 15 21:46:30 2009 +0200
     2.2 +++ b/visidor/src/main/java/cz/xelfi/quoridor/visidor/Viewer.java	Wed Sep 16 22:28:11 2009 +0200
     2.3 @@ -1,8 +1,3 @@
     2.4 -/*
     2.5 - * To change this template, choose Tools | Templates
     2.6 - * and open the template in the editor.
     2.7 - */
     2.8 -
     2.9  package cz.xelfi.quoridor.visidor;
    2.10  
    2.11  import cz.xelfi.quoridor.Board;
    2.12 @@ -17,13 +12,9 @@
    2.13  import java.awt.Rectangle;
    2.14  import javax.swing.JFrame;
    2.15  import javax.swing.JPanel;
    2.16 -import org.netbeans.api.visual.action.ActionFactory;
    2.17  import org.netbeans.api.visual.action.MoveProvider;
    2.18 -import org.netbeans.api.visual.anchor.AnchorFactory;
    2.19  import org.netbeans.api.visual.border.BorderFactory;
    2.20 -import org.netbeans.api.visual.widget.ConnectionWidget;
    2.21  import org.netbeans.api.visual.widget.ImageWidget;
    2.22 -import org.netbeans.api.visual.widget.LabelWidget;
    2.23  import org.netbeans.api.visual.widget.LayerWidget;
    2.24  import org.netbeans.api.visual.widget.Scene;
    2.25  import org.netbeans.api.visual.widget.Widget;
    2.26 @@ -70,41 +61,29 @@
    2.27  
    2.28      private void view(Scene scene, Board b) {
    2.29          scene.removeChildren();
    2.30 +        drawBoard(scene, b);
    2.31 +    }
    2.32 +
    2.33 +    static void drawBoard(Scene scene, Board b) {
    2.34  //        Scene layerBoard = scene;
    2.35 +        scene.setPreferredLocation(new Point(0, 0));
    2.36          final LayerWidget layerLines = new LayerWidget(scene);
    2.37          scene.addChild(layerLines);
    2.38          final LayerWidget layerBoard = new LayerWidget(scene);
    2.39          layerLines.addChild(layerBoard);
    2.40          final int WDTH = 50;
    2.41          final int HGHT = 50;
    2.42 -//        conn.setSourceAnchor(ahoj);
    2.43 -        Widget[][] fields = new Widget[10][];
    2.44 -        for (int i = 0; i < fields.length; i++) {
    2.45 -            fields[i] = new Widget[10];
    2.46 -            for (int j = 0; j < fields[i].length; j++) {
    2.47 -                LabelWidget w = new LabelWidget(scene);
    2.48 +
    2.49 +        
    2.50 +        for (int i = 0; i < 9; i++) {
    2.51 +            for (int j = 0; j < 9; j++) {
    2.52 +                Widget w = new FieldWidget(scene);
    2.53                  w.setPreferredBounds(new Rectangle(i * WDTH, j * HGHT, WDTH, HGHT));
    2.54                  layerBoard.addChild(w);
    2.55 -                fields[i][j] = w;
    2.56 -            }
    2.57 -        }
    2.58 -        for (int i = 0; i < fields.length; i++) {
    2.59 -            for (int j = 0; j < fields[i].length; j++) {
    2.60 -                if (i > 0) {
    2.61 -                    ConnectionWidget horiz = new ConnectionWidget(scene);
    2.62 -                    horiz.setSourceAnchor(AnchorFactory.createCenterAnchor(fields[i - 1][j]));
    2.63 -                    horiz.setTargetAnchor(AnchorFactory.createCenterAnchor(fields[i][j]));
    2.64 -                    scene.addChild(horiz);
    2.65 -                }
    2.66 -                if (j > 0) {
    2.67 -                    ConnectionWidget vert = new ConnectionWidget(scene);
    2.68 -                    vert.setSourceAnchor(AnchorFactory.createCenterAnchor(fields[i][j - 1]));
    2.69 -                    vert.setTargetAnchor(AnchorFactory.createCenterAnchor(fields[i][j]));
    2.70 -                    scene.addChild(vert);
    2.71 -                }
    2.72              }
    2.73          }
    2.74  
    2.75 +/*
    2.76          ImageWidget horizontalWall = new ImageWidget(scene);
    2.77          horizontalWall.setPreferredBounds(new Rectangle(550, 300, 90, 10));
    2.78          horizontalWall.setBackground(Color.BLACK);
    2.79 @@ -120,7 +99,7 @@
    2.80          verticalWall.setBorder(BorderFactory.createLineBorder());
    2.81          verticalWall.setOpaque(true);
    2.82          layerBoard.addChild(verticalWall);
    2.83 -
    2.84 +*/
    2.85          for (Fence f : b.getFences()) {
    2.86              Rectangle r = fenceRectangle(f.getColumn(), f.getRow(), f.getOrientation());
    2.87              ImageWidget fenceWall = new ImageWidget(scene);
    2.88 @@ -132,19 +111,10 @@
    2.89          }
    2.90  
    2.91          int cnt = 0;
    2.92 +        Color[] colors = { Color.WHITE, Color.BLACK, Color.ORANGE, Color.MAGENTA };
    2.93          for (Player p : b.getPlayers()) {
    2.94 -            LabelWidget lw = new LabelWidget(scene);
    2.95 -            lw.setLabel("" + p.getFences());
    2.96 -            if (cnt == 0) {
    2.97 -                lw.setPreferredBounds(new Rectangle(550, 200, 50, 50));
    2.98 -            } else {
    2.99 -                lw.setPreferredBounds(new Rectangle(550, 450, 50, 50));
   2.100 -            }
   2.101 -            layerBoard.addChild(lw);
   2.102 -
   2.103 -            PlayerWidget pw = new PlayerWidget(scene);
   2.104 -            System.err.println("p: " + p);
   2.105 -            pw.setPreferredBounds(new Rectangle(p.getColumn() * 50 + 25, p.getRow() * 50 + 25, 50, 50));
   2.106 +            PlayerWidget pw = new PlayerWidget(scene, colors[cnt]);
   2.107 +            pw.setPreferredBounds(new Rectangle(p.getColumn() * WDTH, p.getRow() * HGHT, WDTH, HGHT));
   2.108              layerBoard.addChild(pw);
   2.109  
   2.110              cnt++;
   2.111 @@ -160,7 +130,7 @@
   2.112          }
   2.113  
   2.114          return new Rectangle(
   2.115 -            (column - 'A' + 1) * 50 + 25 - w, row * 50 + 25 - h, 2 * w, 2 * h
   2.116 +            (column - 'A' + 1) * 50 - w, row * 50 - h, 2 * w, 2 * h
   2.117          );
   2.118      }
   2.119  
   2.120 @@ -259,7 +229,28 @@
   2.121      }
   2.122  
   2.123      private static final class PlayerWidget extends Widget {
   2.124 -        public PlayerWidget(Scene s) {
   2.125 +        private final Color color;
   2.126 +
   2.127 +        public PlayerWidget(Scene s, Color color) {
   2.128 +            super(s);
   2.129 +            this.color = color;
   2.130 +        }
   2.131 +
   2.132 +        @Override
   2.133 +        protected Rectangle calculateClientArea() {
   2.134 +            return getBounds();
   2.135 +        }
   2.136 +
   2.137 +        @Override
   2.138 +        protected void paintWidget() {
   2.139 +            Graphics2D g = getGraphics();
   2.140 +            Rectangle b = getBounds();
   2.141 +            g.setColor(color);
   2.142 +            g.fillOval(b.x + 8, b.y + 8, b.width - 16, b.height - 16);
   2.143 +        }
   2.144 +    }
   2.145 +    private static final class FieldWidget extends Widget {
   2.146 +        public FieldWidget(Scene s) {
   2.147              super(s);
   2.148          }
   2.149  
   2.150 @@ -272,7 +263,8 @@
   2.151          protected void paintWidget() {
   2.152              Graphics2D g = getGraphics();
   2.153              Rectangle b = getBounds();
   2.154 -            g.drawOval(b.x + 5, b.y + 5, b.width - 10, b.height - 10);
   2.155 +            g.setColor(Color.lightGray);
   2.156 +            g.fillRect(b.x + 5, b.y + 5, b.width - 10, b.height - 10);
   2.157          }
   2.158      }
   2.159  }
     3.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.2 +++ b/visidor/src/main/java/cz/xelfi/quoridor/visidor/Visidor.java	Wed Sep 16 22:28:11 2009 +0200
     3.3 @@ -0,0 +1,26 @@
     3.4 +package cz.xelfi.quoridor.visidor;
     3.5 +
     3.6 +import cz.xelfi.quoridor.Board;
     3.7 +import java.awt.Image;
     3.8 +import java.io.IOException;
     3.9 +import org.netbeans.api.visual.export.SceneExporter;
    3.10 +import org.netbeans.api.visual.widget.Scene;
    3.11 +
    3.12 +/** Utilities to draw board into an image.
    3.13 + *
    3.14 + * @author Jaroslav Tulach <jtulach@netbeans.org>
    3.15 + */
    3.16 +public final class Visidor {
    3.17 +    private Visidor() {}
    3.18 +
    3.19 +    public static Image draw(Board b) {
    3.20 +        Scene scene = new Scene();
    3.21 +        Viewer.drawBoard(scene, b);
    3.22 +        try {
    3.23 +            return SceneExporter.createImage(scene, null, SceneExporter.ImageType.PNG, SceneExporter.ZoomType.ACTUAL_SIZE, true, false, 10, 450, 450);
    3.24 +        } catch (IOException ex) {
    3.25 +            throw new IllegalStateException();
    3.26 +        }
    3.27 +    }
    3.28 +
    3.29 +}
     4.1 --- a/visidor/src/test/java/cz/xelfi/quoridor/visidor/ViewerTest.java	Tue Sep 15 21:46:30 2009 +0200
     4.2 +++ b/visidor/src/test/java/cz/xelfi/quoridor/visidor/ViewerTest.java	Wed Sep 16 22:28:11 2009 +0200
     4.3 @@ -1,5 +1,7 @@
     4.4  package cz.xelfi.quoridor.visidor;
     4.5  
     4.6 +import cz.xelfi.quoridor.Board;
     4.7 +import java.awt.Image;
     4.8  import junit.framework.Test;
     4.9  import junit.framework.TestCase;
    4.10  import junit.framework.TestSuite;
    4.11 @@ -17,6 +19,8 @@
    4.12          return new TestSuite(ViewerTest.class);
    4.13      }
    4.14  
    4.15 -    public void testEmpty() {
    4.16 +    public void testDrawBoard() {
    4.17 +        Image img = Visidor.draw(Board.empty());
    4.18 +        assertNotNull("Something has been drawn", img);
    4.19      }
    4.20  }
     5.1 --- a/webidor/pom.xml	Tue Sep 15 21:46:30 2009 +0200
     5.2 +++ b/webidor/pom.xml	Wed Sep 16 22:28:11 2009 +0200
     5.3 @@ -72,6 +72,11 @@
     5.4        <version>1.0</version>
     5.5        <type>jar</type>
     5.6      </dependency>
     5.7 +    <dependency>
     5.8 +      <groupId>${project.groupId}</groupId>
     5.9 +      <artifactId>visidor</artifactId>
    5.10 +      <version>1.0</version>
    5.11 +    </dependency>
    5.12    </dependencies>
    5.13    <build>
    5.14      <plugins>
    5.15 @@ -97,3 +102,4 @@
    5.16  </project>
    5.17  
    5.18  
    5.19 +
     6.1 --- a/webidor/src/main/java/cz/xelfi/quoridor/webidor/resources/Games.java	Tue Sep 15 21:46:30 2009 +0200
     6.2 +++ b/webidor/src/main/java/cz/xelfi/quoridor/webidor/resources/Games.java	Wed Sep 16 22:28:11 2009 +0200
     6.3 @@ -29,6 +29,8 @@
     6.4  import cz.xelfi.quoridor.IllegalPositionException;
     6.5  import cz.xelfi.quoridor.webidor.*;
     6.6  import cz.xelfi.quoridor.Move;
     6.7 +import cz.xelfi.quoridor.visidor.Visidor;
     6.8 +import java.awt.Image;
     6.9  import java.io.BufferedReader;
    6.10  import java.io.BufferedWriter;
    6.11  import java.io.File;
    6.12 @@ -118,6 +120,17 @@
    6.13  
    6.14      @GET
    6.15      @Path("{id}")
    6.16 +    @Produces("image/png")
    6.17 +    public Image getBoardImage(@PathParam("id") String id) {
    6.18 +        Game g = findGame(id);
    6.19 +        if (g == null) {
    6.20 +            throw new IllegalArgumentException("Unknown game " + id);
    6.21 +        }
    6.22 +        return Visidor.draw(g.getBoard());
    6.23 +    }
    6.24 +
    6.25 +    @GET
    6.26 +    @Path("{id}")
    6.27      @Produces({ MediaType.APPLICATION_JSON, MediaType.TEXT_XML })
    6.28      public Game getBoardInfo(@PathParam("id") String id) {
    6.29          Game g = findGame(id);