freemarkerdor/src/main/java/cz/xelfi/quoridor/freemarkerdor/BoardImage.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Fri, 01 Jan 2010 20:53:17 +0100
changeset 177 2f8672ac9f1a
parent 176 700003474749
child 264 d60370059c3c
permissions -rw-r--r--
Using gray to indicate active player
jaroslav@95
     1
/*
jaroslav@95
     2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
jaroslav@95
     3
 *
jaroslav@95
     4
 * The contents of this file are subject to the terms of either the GNU
jaroslav@95
     5
 * General Public License Version 2 only ("GPL") or the Common
jaroslav@95
     6
 * Development and Distribution License("CDDL") (collectively, the
jaroslav@95
     7
 * "License"). You may not use this file except in compliance with the
jaroslav@95
     8
 * License. You can obtain a copy of the License at
jaroslav@95
     9
 * http://www.netbeans.org/cddl-gplv2.html
jaroslav@95
    10
 * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
jaroslav@95
    11
 * specific language governing permissions and limitations under the
jaroslav@95
    12
 * License.  When distributing the software, include this License Header
jaroslav@95
    13
 * Notice in each file and include the License file at
jaroslav@95
    14
 * nbbuild/licenses/CDDL-GPL-2-CP.  Sun designates this
jaroslav@95
    15
 * particular file as subject to the "Classpath" exception as provided
jaroslav@95
    16
 * by Sun in the GPL Version 2 section of the License file that
jaroslav@95
    17
 * accompanied this code. If applicable, add the following below the
jaroslav@95
    18
 * License Header, with the fields enclosed by brackets [] replaced by
jaroslav@95
    19
 * your own identifying information:
jaroslav@95
    20
 * "Portions Copyrighted [year] [name of copyright owner]"
jaroslav@95
    21
 *
jaroslav@95
    22
 * Contributor(s):
jaroslav@95
    23
 *
jaroslav@95
    24
 * Portions Copyrighted 2009 Jaroslav Tulach
jaroslav@95
    25
 */
jaroslav@95
    26
jaroslav@105
    27
package cz.xelfi.quoridor.freemarkerdor;
jaroslav@95
    28
jaroslav@95
    29
import cz.xelfi.quoridor.Board;
jaroslav@95
    30
import cz.xelfi.quoridor.Fence;
jaroslav@95
    31
import cz.xelfi.quoridor.Player;
jaroslav@95
    32
import java.awt.Color;
jaroslav@102
    33
import java.awt.Font;
jaroslav@95
    34
import java.awt.Graphics2D;
jaroslav@95
    35
import java.awt.Image;
jaroslav@95
    36
import java.awt.Rectangle;
jaroslav@95
    37
import java.awt.image.BufferedImage;
jaroslav@95
    38
jaroslav@95
    39
/** Convertor of a board to Image.
jaroslav@95
    40
 *
jaroslav@95
    41
 * @author Jaroslav Tulach <jtulach@netbeans.org>
jaroslav@95
    42
 */
jaroslav@95
    43
final class BoardImage  {
jaroslav@100
    44
    static Image draw(Board b, int fieldSize) {
jaroslav@95
    45
        int fifth = fieldSize / 10;
jaroslav@95
    46
jaroslav@95
    47
        BufferedImage img = new BufferedImage(fieldSize * 9, fieldSize * 9, BufferedImage.TYPE_INT_ARGB);
jaroslav@95
    48
        Graphics2D g = (Graphics2D) img.getGraphics();
jaroslav@95
    49
jaroslav@95
    50
        g.setColor(Color.lightGray);
jaroslav@95
    51
        for (int i = 0; i < 9; i++) {
jaroslav@95
    52
            for (int j = 0; j < 9; j++) {
jaroslav@95
    53
                final Rectangle r = new Rectangle(i * fieldSize, j * fieldSize, fieldSize, fieldSize);
jaroslav@95
    54
                g.fillRect(r.x + fifth, r.y + fifth, r.width - fifth * 2, r.height - fifth * 2);
jaroslav@95
    55
            }
jaroslav@95
    56
        }
jaroslav@95
    57
jaroslav@95
    58
jaroslav@95
    59
        g.setColor(Color.BLACK);
jaroslav@95
    60
        for (Fence f : b.getFences()) {
jaroslav@95
    61
            int w, h;
jaroslav@95
    62
            switch (f.getOrientation()) {
jaroslav@95
    63
                case HORIZONTAL: w = fieldSize - fifth; h = fifth; break;
jaroslav@95
    64
                case VERTICAL: w = fifth; h = fieldSize - fifth; break;
jaroslav@95
    65
                default: throw new IllegalStateException();
jaroslav@95
    66
            }
jaroslav@95
    67
            int column = (f.getColumn() - 'A') + 1;
jaroslav@95
    68
            int row = 9 - f.getRow();
jaroslav@95
    69
            Rectangle r = new Rectangle(
jaroslav@95
    70
                column * fieldSize - w,
jaroslav@95
    71
                row * fieldSize - h,
jaroslav@95
    72
                2 * w,
jaroslav@95
    73
                2 * h
jaroslav@95
    74
            );
jaroslav@95
    75
            g.fill(r);
jaroslav@95
    76
        }
jaroslav@95
    77
jaroslav@95
    78
        int cnt = 0;
jaroslav@95
    79
        Color[] colors = { Color.WHITE, Color.BLACK, Color.ORANGE, Color.MAGENTA };
jaroslav@95
    80
        int diametr = fieldSize - fifth * 4;
jaroslav@95
    81
        for (Player p : b.getPlayers()) {
jaroslav@95
    82
            int column = p.getColumn();
jaroslav@95
    83
            int row = 8 - p.getRow();
jaroslav@95
    84
            final Rectangle r = new Rectangle(
jaroslav@95
    85
                column * fieldSize + 2 * fifth,
jaroslav@95
    86
                row * fieldSize + 2 * fifth,
jaroslav@95
    87
                diametr,
jaroslav@95
    88
                diametr
jaroslav@95
    89
            );
jaroslav@95
    90
            g.setColor(colors[cnt]);
jaroslav@95
    91
            g.fillOval(r.x, r.y, r.width, r.height);
jaroslav@176
    92
            if (p == b.getCurrentPlayer()) {
jaroslav@177
    93
                g.setColor(Color.lightGray);
jaroslav@176
    94
                g.fillOval(r.x + r.width / 3, r.y + r.height / 3, r.width / 3, r.height / 3);
jaroslav@176
    95
            }
jaroslav@95
    96
            cnt++;
jaroslav@95
    97
        }
jaroslav@95
    98
jaroslav@102
    99
        g.setColor(Color.BLACK);
jaroslav@102
   100
        final Font f = new Font("Monospace", Font.BOLD, fifth * 2);
jaroslav@102
   101
        g.setFont(f);
jaroslav@102
   102
        for (int i = 0; i < 8; i++) {
jaroslav@102
   103
            final char ch = (char) ('A' + i);
jaroslav@102
   104
            g.drawString(Character.toString(ch), i * fieldSize + fieldSize - fifth, fifth + f.getSize() - f.getBaselineFor(ch));
jaroslav@102
   105
        }
jaroslav@102
   106
        for (int i = 0; i < 8; i++) {
jaroslav@102
   107
            String s = "" + (8 - i);
jaroslav@102
   108
            g.drawString(s, fifth, i * fieldSize + fieldSize - fifth + f.getSize() - f.getBaselineFor(s.charAt(0)));
jaroslav@102
   109
        }
jaroslav@102
   110
jaroslav@95
   111
        return img;
jaroslav@95
   112
    }
jaroslav@95
   113
}