Moving the .png generation to the UI layer and making it work on IE8
authorJaroslav Tulach <jaroslav.tulach@apidesign.org>
Sun, 20 Sep 2009 20:04:41 +0200
changeset 1056e55d5c85d3c
parent 104 e37b229e238b
child 106 7d090f2c5b91
Moving the .png generation to the UI layer and making it work on IE8
freemarkerdor/pom.xml
freemarkerdor/src/main/java/cz/xelfi/quoridor/freemarkerdor/BoardImage.java
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	Sun Sep 20 11:00:36 2009 +0200
     1.2 +++ b/freemarkerdor/pom.xml	Sun Sep 20 20:04:41 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.17</version>
     1.8 +  <version>1.18</version>
     1.9    <url>http://maven.apache.org</url>
    1.10    <dependencies>
    1.11      <dependency>
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/freemarkerdor/src/main/java/cz/xelfi/quoridor/freemarkerdor/BoardImage.java	Sun Sep 20 20:04:41 2009 +0200
     2.3 @@ -0,0 +1,109 @@
     2.4 +/*
     2.5 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
     2.6 + *
     2.7 + * The contents of this file are subject to the terms of either the GNU
     2.8 + * General Public License Version 2 only ("GPL") or the Common
     2.9 + * Development and Distribution License("CDDL") (collectively, the
    2.10 + * "License"). You may not use this file except in compliance with the
    2.11 + * License. You can obtain a copy of the License at
    2.12 + * http://www.netbeans.org/cddl-gplv2.html
    2.13 + * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
    2.14 + * specific language governing permissions and limitations under the
    2.15 + * License.  When distributing the software, include this License Header
    2.16 + * Notice in each file and include the License file at
    2.17 + * nbbuild/licenses/CDDL-GPL-2-CP.  Sun designates this
    2.18 + * particular file as subject to the "Classpath" exception as provided
    2.19 + * by Sun in the GPL Version 2 section of the License file that
    2.20 + * accompanied this code. If applicable, add the following below the
    2.21 + * License Header, with the fields enclosed by brackets [] replaced by
    2.22 + * your own identifying information:
    2.23 + * "Portions Copyrighted [year] [name of copyright owner]"
    2.24 + *
    2.25 + * Contributor(s):
    2.26 + *
    2.27 + * Portions Copyrighted 2009 Jaroslav Tulach
    2.28 + */
    2.29 +
    2.30 +package cz.xelfi.quoridor.freemarkerdor;
    2.31 +
    2.32 +import cz.xelfi.quoridor.Board;
    2.33 +import cz.xelfi.quoridor.Fence;
    2.34 +import cz.xelfi.quoridor.Player;
    2.35 +import java.awt.Color;
    2.36 +import java.awt.Font;
    2.37 +import java.awt.Graphics2D;
    2.38 +import java.awt.Image;
    2.39 +import java.awt.Rectangle;
    2.40 +import java.awt.image.BufferedImage;
    2.41 +
    2.42 +/** Convertor of a board to Image.
    2.43 + *
    2.44 + * @author Jaroslav Tulach <jtulach@netbeans.org>
    2.45 + */
    2.46 +final class BoardImage  {
    2.47 +    static Image draw(Board b, int fieldSize) {
    2.48 +        int fifth = fieldSize / 10;
    2.49 +
    2.50 +        BufferedImage img = new BufferedImage(fieldSize * 9, fieldSize * 9, BufferedImage.TYPE_INT_ARGB);
    2.51 +        Graphics2D g = (Graphics2D) img.getGraphics();
    2.52 +
    2.53 +        g.setColor(Color.lightGray);
    2.54 +        for (int i = 0; i < 9; i++) {
    2.55 +            for (int j = 0; j < 9; j++) {
    2.56 +                final Rectangle r = new Rectangle(i * fieldSize, j * fieldSize, fieldSize, fieldSize);
    2.57 +                g.fillRect(r.x + fifth, r.y + fifth, r.width - fifth * 2, r.height - fifth * 2);
    2.58 +            }
    2.59 +        }
    2.60 +
    2.61 +
    2.62 +        g.setColor(Color.BLACK);
    2.63 +        for (Fence f : b.getFences()) {
    2.64 +            int w, h;
    2.65 +            switch (f.getOrientation()) {
    2.66 +                case HORIZONTAL: w = fieldSize - fifth; h = fifth; break;
    2.67 +                case VERTICAL: w = fifth; h = fieldSize - fifth; break;
    2.68 +                default: throw new IllegalStateException();
    2.69 +            }
    2.70 +            int column = (f.getColumn() - 'A') + 1;
    2.71 +            int row = 9 - f.getRow();
    2.72 +            Rectangle r = new Rectangle(
    2.73 +                column * fieldSize - w,
    2.74 +                row * fieldSize - h,
    2.75 +                2 * w,
    2.76 +                2 * h
    2.77 +            );
    2.78 +            g.fill(r);
    2.79 +        }
    2.80 +
    2.81 +        int cnt = 0;
    2.82 +        Color[] colors = { Color.WHITE, Color.BLACK, Color.ORANGE, Color.MAGENTA };
    2.83 +        int diametr = fieldSize - fifth * 4;
    2.84 +        for (Player p : b.getPlayers()) {
    2.85 +            int column = p.getColumn();
    2.86 +            int row = 8 - p.getRow();
    2.87 +            final Rectangle r = new Rectangle(
    2.88 +                column * fieldSize + 2 * fifth,
    2.89 +                row * fieldSize + 2 * fifth,
    2.90 +                diametr,
    2.91 +                diametr
    2.92 +            );
    2.93 +            g.setColor(colors[cnt]);
    2.94 +            g.fillOval(r.x, r.y, r.width, r.height);
    2.95 +            cnt++;
    2.96 +        }
    2.97 +
    2.98 +        g.setColor(Color.BLACK);
    2.99 +        final Font f = new Font("Monospace", Font.BOLD, fifth * 2);
   2.100 +        g.setFont(f);
   2.101 +        for (int i = 0; i < 8; i++) {
   2.102 +            final char ch = (char) ('A' + i);
   2.103 +            g.drawString(Character.toString(ch), i * fieldSize + fieldSize - fifth, fifth + f.getSize() - f.getBaselineFor(ch));
   2.104 +        }
   2.105 +        for (int i = 0; i < 8; i++) {
   2.106 +            String s = "" + (8 - i);
   2.107 +            g.drawString(s, fifth, i * fieldSize + fieldSize - fifth + f.getSize() - f.getBaselineFor(s.charAt(0)));
   2.108 +        }
   2.109 +
   2.110 +        return img;
   2.111 +    }
   2.112 +}
     3.1 --- a/freemarkerdor/src/main/java/cz/xelfi/quoridor/freemarkerdor/UI.java	Sun Sep 20 11:00:36 2009 +0200
     3.2 +++ b/freemarkerdor/src/main/java/cz/xelfi/quoridor/freemarkerdor/UI.java	Sun Sep 20 20:04:41 2009 +0200
     3.3 @@ -36,6 +36,7 @@
     3.4  import com.sun.net.httpserver.HttpServer;
     3.5  import cz.xelfi.quoridor.Board;
     3.6  import cz.xelfi.quoridor.IllegalPositionException;
     3.7 +import java.awt.Image;
     3.8  import java.io.File;
     3.9  import java.io.IOException;
    3.10  import java.io.InputStream;
    3.11 @@ -143,6 +144,24 @@
    3.12          return welcomeImpl();
    3.13      }
    3.14  
    3.15 +    @GET
    3.16 +    @Path("games/{id}.png")
    3.17 +    @Produces("image/png")
    3.18 +    public Image getBoardImage(
    3.19 +        @PathParam("id") String id,
    3.20 +        @QueryParam("fieldSize") @DefaultValue("50") int fieldSize,
    3.21 +        @QueryParam("move") @DefaultValue("-1") int move
    3.22 +    ) throws IllegalPositionException {
    3.23 +        WebResource wr = base.path("games").path(id);
    3.24 +        if (move != -1) {
    3.25 +            wr.queryParam("move", "" + move);
    3.26 +        }
    3.27 +        String txt = wr.accept(MediaType.TEXT_PLAIN).get(String.class);
    3.28 +        Board b = Board.valueOf(txt);
    3.29 +        return BoardImage.draw(b, fieldSize);
    3.30 +    }
    3.31 +
    3.32 +
    3.33      private Response board(String id) {
    3.34          return board(id, "", null);
    3.35      }
     4.1 --- a/freemarkerdor/src/main/resources/cz/xelfi/quoridor/freemarkerdor/UI/game.fmt	Sun Sep 20 11:00:36 2009 +0200
     4.2 +++ b/freemarkerdor/src/main/resources/cz/xelfi/quoridor/freemarkerdor/UI/game.fmt	Sun Sep 20 20:04:41 2009 +0200
     4.3 @@ -112,23 +112,25 @@
     4.4            </form>
     4.5        </#if>
     4.6        <p>
     4.7 -      ${bundle.BOARD_VIEW}
     4.8        <#if format?? && format = "text">
     4.9          <pre>${doc.game.board}</pre>
    4.10 +        ${bundle.BOARD_VIEW}
    4.11          <a href="/games/${doc.game.id.@id}?format=small">${bundle.BOARD_SMALL}</a>
    4.12          <a href="/games/${doc.game.id.@id}?format=image">${bundle.BOARD_IMAGE}</a>
    4.13          ${bundle.BOARD_TEXT}
    4.14        <#elseif format?? && format = "small">
    4.15          <p>
    4.16 -            <img src="/api/games/${doc.game.id.@id}?fieldSize=20<#if doc.game.@currentMove??>&move=${doc.game.@currentMove}</#if>" alt="${bundle.BOARD_TEXT}">
    4.17 +            <img src="/games/${doc.game.id.@id}.png?fieldSize=20<#if doc.game.@currentMove??>&move=${doc.game.@currentMove}</#if>" alt="${bundle.BOARD_TEXT}">
    4.18          </p>
    4.19 +        ${bundle.BOARD_VIEW}
    4.20          ${bundle.BOARD_SMALL}
    4.21          <a href="/games/${doc.game.id.@id}?format=image">${bundle.BOARD_IMAGE}</a>
    4.22          <a href="/games/${doc.game.id.@id}?format=text">${bundle.BOARD_TEXT}</a>
    4.23        <#else>
    4.24          <p>
    4.25 -            <img src="/api/games/${doc.game.id.@id}<#if doc.game.@currentMove??>?move=${doc.game.@currentMove}</#if>" alt="${bundle.BOARD_TEXT}">
    4.26 +            <img src="/games/${doc.game.id.@id}.png<#if doc.game.@currentMove??>?move=${doc.game.@currentMove}</#if>" alt="${bundle.BOARD_TEXT}">
    4.27          </p>
    4.28 +        ${bundle.BOARD_VIEW}
    4.29          <a href="/games/${doc.game.id.@id}?format=small">${bundle.BOARD_SMALL}</a>
    4.30          ${bundle.BOARD_IMAGE}
    4.31          <a href="/games/${doc.game.id.@id}?format=text">${bundle.BOARD_TEXT}</a>
     5.1 --- a/webidor/src/main/java/cz/xelfi/quoridor/webidor/resources/BoardImage.java	Sun Sep 20 11:00:36 2009 +0200
     5.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     5.3 @@ -1,109 +0,0 @@
     5.4 -/*
     5.5 - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
     5.6 - *
     5.7 - * The contents of this file are subject to the terms of either the GNU
     5.8 - * General Public License Version 2 only ("GPL") or the Common
     5.9 - * Development and Distribution License("CDDL") (collectively, the
    5.10 - * "License"). You may not use this file except in compliance with the
    5.11 - * License. You can obtain a copy of the License at
    5.12 - * http://www.netbeans.org/cddl-gplv2.html
    5.13 - * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
    5.14 - * specific language governing permissions and limitations under the
    5.15 - * License.  When distributing the software, include this License Header
    5.16 - * Notice in each file and include the License file at
    5.17 - * nbbuild/licenses/CDDL-GPL-2-CP.  Sun designates this
    5.18 - * particular file as subject to the "Classpath" exception as provided
    5.19 - * by Sun in the GPL Version 2 section of the License file that
    5.20 - * accompanied this code. If applicable, add the following below the
    5.21 - * License Header, with the fields enclosed by brackets [] replaced by
    5.22 - * your own identifying information:
    5.23 - * "Portions Copyrighted [year] [name of copyright owner]"
    5.24 - *
    5.25 - * Contributor(s):
    5.26 - *
    5.27 - * Portions Copyrighted 2009 Jaroslav Tulach
    5.28 - */
    5.29 -
    5.30 -package cz.xelfi.quoridor.webidor.resources;
    5.31 -
    5.32 -import cz.xelfi.quoridor.Board;
    5.33 -import cz.xelfi.quoridor.Fence;
    5.34 -import cz.xelfi.quoridor.Player;
    5.35 -import java.awt.Color;
    5.36 -import java.awt.Font;
    5.37 -import java.awt.Graphics2D;
    5.38 -import java.awt.Image;
    5.39 -import java.awt.Rectangle;
    5.40 -import java.awt.image.BufferedImage;
    5.41 -
    5.42 -/** Convertor of a board to Image.
    5.43 - *
    5.44 - * @author Jaroslav Tulach <jtulach@netbeans.org>
    5.45 - */
    5.46 -final class BoardImage  {
    5.47 -    static Image draw(Board b, int fieldSize) {
    5.48 -        int fifth = fieldSize / 10;
    5.49 -
    5.50 -        BufferedImage img = new BufferedImage(fieldSize * 9, fieldSize * 9, BufferedImage.TYPE_INT_ARGB);
    5.51 -        Graphics2D g = (Graphics2D) img.getGraphics();
    5.52 -
    5.53 -        g.setColor(Color.lightGray);
    5.54 -        for (int i = 0; i < 9; i++) {
    5.55 -            for (int j = 0; j < 9; j++) {
    5.56 -                final Rectangle r = new Rectangle(i * fieldSize, j * fieldSize, fieldSize, fieldSize);
    5.57 -                g.fillRect(r.x + fifth, r.y + fifth, r.width - fifth * 2, r.height - fifth * 2);
    5.58 -            }
    5.59 -        }
    5.60 -
    5.61 -
    5.62 -        g.setColor(Color.BLACK);
    5.63 -        for (Fence f : b.getFences()) {
    5.64 -            int w, h;
    5.65 -            switch (f.getOrientation()) {
    5.66 -                case HORIZONTAL: w = fieldSize - fifth; h = fifth; break;
    5.67 -                case VERTICAL: w = fifth; h = fieldSize - fifth; break;
    5.68 -                default: throw new IllegalStateException();
    5.69 -            }
    5.70 -            int column = (f.getColumn() - 'A') + 1;
    5.71 -            int row = 9 - f.getRow();
    5.72 -            Rectangle r = new Rectangle(
    5.73 -                column * fieldSize - w,
    5.74 -                row * fieldSize - h,
    5.75 -                2 * w,
    5.76 -                2 * h
    5.77 -            );
    5.78 -            g.fill(r);
    5.79 -        }
    5.80 -
    5.81 -        int cnt = 0;
    5.82 -        Color[] colors = { Color.WHITE, Color.BLACK, Color.ORANGE, Color.MAGENTA };
    5.83 -        int diametr = fieldSize - fifth * 4;
    5.84 -        for (Player p : b.getPlayers()) {
    5.85 -            int column = p.getColumn();
    5.86 -            int row = 8 - p.getRow();
    5.87 -            final Rectangle r = new Rectangle(
    5.88 -                column * fieldSize + 2 * fifth,
    5.89 -                row * fieldSize + 2 * fifth,
    5.90 -                diametr,
    5.91 -                diametr
    5.92 -            );
    5.93 -            g.setColor(colors[cnt]);
    5.94 -            g.fillOval(r.x, r.y, r.width, r.height);
    5.95 -            cnt++;
    5.96 -        }
    5.97 -
    5.98 -        g.setColor(Color.BLACK);
    5.99 -        final Font f = new Font("Monospace", Font.BOLD, fifth * 2);
   5.100 -        g.setFont(f);
   5.101 -        for (int i = 0; i < 8; i++) {
   5.102 -            final char ch = (char) ('A' + i);
   5.103 -            g.drawString(Character.toString(ch), i * fieldSize + fieldSize - fifth, fifth + f.getSize() - f.getBaselineFor(ch));
   5.104 -        }
   5.105 -        for (int i = 0; i < 8; i++) {
   5.106 -            String s = "" + (8 - i);
   5.107 -            g.drawString(s, fifth, i * fieldSize + fieldSize - fifth + f.getSize() - f.getBaselineFor(s.charAt(0)));
   5.108 -        }
   5.109 -
   5.110 -        return img;
   5.111 -    }
   5.112 -}
     6.1 --- a/webidor/src/main/java/cz/xelfi/quoridor/webidor/resources/Games.java	Sun Sep 20 11:00:36 2009 +0200
     6.2 +++ b/webidor/src/main/java/cz/xelfi/quoridor/webidor/resources/Games.java	Sun Sep 20 20:04:41 2009 +0200
     6.3 @@ -29,7 +29,6 @@
     6.4  import cz.xelfi.quoridor.IllegalPositionException;
     6.5  import cz.xelfi.quoridor.webidor.*;
     6.6  import cz.xelfi.quoridor.Move;
     6.7 -import java.awt.Image;
     6.8  import java.io.BufferedReader;
     6.9  import java.io.BufferedWriter;
    6.10  import java.io.File;
    6.11 @@ -121,21 +120,6 @@
    6.12  
    6.13      @GET
    6.14      @Path("{id}")
    6.15 -    @Produces("image/png")
    6.16 -    public Image getBoardImage(
    6.17 -        @PathParam("id") String id,
    6.18 -        @QueryParam("fieldSize") @DefaultValue("50") int fieldSize,
    6.19 -        @QueryParam("move") @DefaultValue("-1") int move
    6.20 -    ) {
    6.21 -        Game g = findGame(id, move);
    6.22 -        if (g == null) {
    6.23 -            throw new IllegalArgumentException("Unknown game " + id);
    6.24 -        }
    6.25 -        return BoardImage.draw(g.getBoard(), fieldSize);
    6.26 -    }
    6.27 -
    6.28 -    @GET
    6.29 -    @Path("{id}")
    6.30      @Produces({ MediaType.APPLICATION_JSON, MediaType.TEXT_XML })
    6.31      public Game getBoardInfo(
    6.32          @PathParam("id") String id,