Ability to display the board as image
authorJaroslav Tulach <jaroslav.tulach@apidesign.org>
Fri, 18 Sep 2009 23:28:13 +0200
changeset 9536ace6ba1dc1
parent 94 54d1f3879d0f
child 96 2eeaa41236c3
Ability to display the board as image
freemarkerdor/pom.xml
freemarkerdor/src/main/java/cz/xelfi/quoridor/freemarkerdor/UI.java
freemarkerdor/src/main/resources/cz/xelfi/quoridor/freemarkerdor/UI/game.fmt
webidor/src/main/java/cz/xelfi/quoridor/webidor/resources/BoardImage.java
webidor/src/main/java/cz/xelfi/quoridor/webidor/resources/Games.java
     1.1 --- a/freemarkerdor/pom.xml	Fri Sep 18 07:37:45 2009 +0200
     1.2 +++ b/freemarkerdor/pom.xml	Fri Sep 18 23:28:13 2009 +0200
     1.3 @@ -10,7 +10,7 @@
     1.4    <groupId>org.apidesign</groupId>
     1.5    <artifactId>freemarkerdor</artifactId>
     1.6    <name>freemarkerdor</name>
     1.7 -  <version>1.10</version>
     1.8 +  <version>1.11</version>
     1.9    <url>http://maven.apache.org</url>
    1.10    <dependencies>
    1.11      <dependency>
     2.1 --- a/freemarkerdor/src/main/java/cz/xelfi/quoridor/freemarkerdor/UI.java	Fri Sep 18 07:37:45 2009 +0200
     2.2 +++ b/freemarkerdor/src/main/java/cz/xelfi/quoridor/freemarkerdor/UI.java	Fri Sep 18 23:28:13 2009 +0200
     2.3 @@ -136,19 +136,22 @@
     2.4          return welcomeImpl();
     2.5      }
     2.6  
     2.7 +    private Viewable board(String id) {
     2.8 +        return board(id, "");
     2.9 +    }
    2.10      @GET
    2.11      @Path("games/{id}/")
    2.12      @Produces(MediaType.TEXT_HTML)
    2.13 -    public Viewable board(@PathParam("id") String id) {
    2.14 -        return board(id, null);
    2.15 +    public Viewable board(@PathParam("id") String id, @QueryParam("format") @DefaultValue("") String format) {
    2.16 +        return board(id, null, format);
    2.17      }
    2.18 -    private Viewable board(@PathParam("id") String id, String msg) {
    2.19 +    private Viewable board(@PathParam("id") String id, String msg, String format) {
    2.20          Viewable v = checkLogin();
    2.21          if (v != null) {
    2.22              return v;
    2.23          }
    2.24          Document doc = base.path("games").path(id).accept(MediaType.TEXT_XML).get(Document.class);
    2.25 -        return viewable("game.fmt", doc, "message", msg);
    2.26 +        return viewable("game.fmt", doc, "message", msg, "format", format);
    2.27      }
    2.28  
    2.29      @GET
     3.1 --- a/freemarkerdor/src/main/resources/cz/xelfi/quoridor/freemarkerdor/UI/game.fmt	Fri Sep 18 07:37:45 2009 +0200
     3.2 +++ b/freemarkerdor/src/main/resources/cz/xelfi/quoridor/freemarkerdor/UI/game.fmt	Fri Sep 18 23:28:13 2009 +0200
     3.3 @@ -91,7 +91,18 @@
     3.4                <input type="submit" value="${bundle.RESIGN}" />
     3.5            </form>
     3.6        </#if>
     3.7 -      <pre>${doc.game.board}</pre>
     3.8 +
     3.9 +      <#if format?? && format = "text">
    3.10 +        <pre>${doc.game.board}</pre>
    3.11 +        <a href="/games/${doc.game.id.@id}?format=image">Image</a>
    3.12 +      <#else>
    3.13 +        <p>
    3.14 +        <img src="/api/games/${doc.game.id.@id}" alt="text">
    3.15 +        </p>
    3.16 +        <a href="/games/${doc.game.id.@id}?format=text">Text</a>
    3.17 +
    3.18 +      </#if>
    3.19 +
    3.20  
    3.21        <h3>${bundle.MOVES}</h3>
    3.22  
     4.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     4.2 +++ b/webidor/src/main/java/cz/xelfi/quoridor/webidor/resources/BoardImage.java	Fri Sep 18 23:28:13 2009 +0200
     4.3 @@ -0,0 +1,102 @@
     4.4 +/*
     4.5 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
     4.6 + *
     4.7 + * The contents of this file are subject to the terms of either the GNU
     4.8 + * General Public License Version 2 only ("GPL") or the Common
     4.9 + * Development and Distribution License("CDDL") (collectively, the
    4.10 + * "License"). You may not use this file except in compliance with the
    4.11 + * License. You can obtain a copy of the License at
    4.12 + * http://www.netbeans.org/cddl-gplv2.html
    4.13 + * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
    4.14 + * specific language governing permissions and limitations under the
    4.15 + * License.  When distributing the software, include this License Header
    4.16 + * Notice in each file and include the License file at
    4.17 + * nbbuild/licenses/CDDL-GPL-2-CP.  Sun designates this
    4.18 + * particular file as subject to the "Classpath" exception as provided
    4.19 + * by Sun in the GPL Version 2 section of the License file that
    4.20 + * accompanied this code. If applicable, add the following below the
    4.21 + * License Header, with the fields enclosed by brackets [] replaced by
    4.22 + * your own identifying information:
    4.23 + * "Portions Copyrighted [year] [name of copyright owner]"
    4.24 + *
    4.25 + * Contributor(s):
    4.26 + *
    4.27 + * Portions Copyrighted 2009 Jaroslav Tulach
    4.28 + */
    4.29 +
    4.30 +package cz.xelfi.quoridor.webidor.resources;
    4.31 +
    4.32 +import cz.xelfi.quoridor.Board;
    4.33 +import cz.xelfi.quoridor.Fence;
    4.34 +import cz.xelfi.quoridor.Player;
    4.35 +import java.awt.Color;
    4.36 +import java.awt.Graphics2D;
    4.37 +import java.awt.Image;
    4.38 +import java.awt.Rectangle;
    4.39 +import java.awt.image.BufferedImage;
    4.40 +
    4.41 +/** Convertor of a board to Image.
    4.42 + *
    4.43 + * @author Jaroslav Tulach <jtulach@netbeans.org>
    4.44 + */
    4.45 +final class BoardImage  {
    4.46 +
    4.47 +
    4.48 +    public static Image draw(Board b) {
    4.49 +        return draw(b, 50);
    4.50 +    }
    4.51 +
    4.52 +    private static Image draw(Board b, int fieldSize) {
    4.53 +        int fifth = fieldSize / 10;
    4.54 +
    4.55 +        BufferedImage img = new BufferedImage(fieldSize * 9, fieldSize * 9, BufferedImage.TYPE_INT_ARGB);
    4.56 +        Graphics2D g = (Graphics2D) img.getGraphics();
    4.57 +
    4.58 +        g.setColor(Color.lightGray);
    4.59 +        for (int i = 0; i < 9; i++) {
    4.60 +            for (int j = 0; j < 9; j++) {
    4.61 +                final Rectangle r = new Rectangle(i * fieldSize, j * fieldSize, fieldSize, fieldSize);
    4.62 +                g.fillRect(r.x + fifth, r.y + fifth, r.width - fifth * 2, r.height - fifth * 2);
    4.63 +            }
    4.64 +        }
    4.65 +
    4.66 +
    4.67 +        g.setColor(Color.BLACK);
    4.68 +        for (Fence f : b.getFences()) {
    4.69 +            int w, h;
    4.70 +            switch (f.getOrientation()) {
    4.71 +                case HORIZONTAL: w = fieldSize - fifth; h = fifth; break;
    4.72 +                case VERTICAL: w = fifth; h = fieldSize - fifth; break;
    4.73 +                default: throw new IllegalStateException();
    4.74 +            }
    4.75 +            int column = (f.getColumn() - 'A') + 1;
    4.76 +            int row = 9 - f.getRow();
    4.77 +            Rectangle r = new Rectangle(
    4.78 +                column * fieldSize - w,
    4.79 +                row * fieldSize - h,
    4.80 +                2 * w,
    4.81 +                2 * h
    4.82 +            );
    4.83 +            g.fill(r);
    4.84 +        }
    4.85 +
    4.86 +        int cnt = 0;
    4.87 +        Color[] colors = { Color.WHITE, Color.BLACK, Color.ORANGE, Color.MAGENTA };
    4.88 +        int diametr = fieldSize - fifth * 4;
    4.89 +        for (Player p : b.getPlayers()) {
    4.90 +            int column = p.getColumn();
    4.91 +            int row = 8 - p.getRow();
    4.92 +            final Rectangle r = new Rectangle(
    4.93 +                column * fieldSize + 2 * fifth,
    4.94 +                row * fieldSize + 2 * fifth,
    4.95 +                diametr,
    4.96 +                diametr
    4.97 +            );
    4.98 +            g.setColor(colors[cnt]);
    4.99 +            g.fillOval(r.x, r.y, r.width, r.height);
   4.100 +            cnt++;
   4.101 +        }
   4.102 +
   4.103 +        return img;
   4.104 +    }
   4.105 +}
     5.1 --- a/webidor/src/main/java/cz/xelfi/quoridor/webidor/resources/Games.java	Fri Sep 18 07:37:45 2009 +0200
     5.2 +++ b/webidor/src/main/java/cz/xelfi/quoridor/webidor/resources/Games.java	Fri Sep 18 23:28:13 2009 +0200
     5.3 @@ -29,6 +29,7 @@
     5.4  import cz.xelfi.quoridor.IllegalPositionException;
     5.5  import cz.xelfi.quoridor.webidor.*;
     5.6  import cz.xelfi.quoridor.Move;
     5.7 +import java.awt.Image;
     5.8  import java.io.BufferedReader;
     5.9  import java.io.BufferedWriter;
    5.10  import java.io.File;
    5.11 @@ -118,6 +119,17 @@
    5.12  
    5.13      @GET
    5.14      @Path("{id}")
    5.15 +    @Produces("image/png")
    5.16 +    public Image getBoardImage(@PathParam("id") String id) {
    5.17 +        Game g = findGame(id);
    5.18 +        if (g == null) {
    5.19 +            throw new IllegalArgumentException("Unknown game " + id);
    5.20 +        }
    5.21 +        return BoardImage.draw(g.getBoard());
    5.22 +    }
    5.23 +
    5.24 +    @GET
    5.25 +    @Path("{id}")
    5.26      @Produces({ MediaType.APPLICATION_JSON, MediaType.TEXT_XML })
    5.27      public Game getBoardInfo(@PathParam("id") String id) {
    5.28          Game g = findGame(id);