freemarkerdor/src/main/java/cz/xelfi/quoridor/freemarkerdor/BoardImage.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Tue, 14 Sep 2010 08:56:13 +0200
changeset 264 d60370059c3c
parent 177 2f8672ac9f1a
permissions -rw-r--r--
Changing headers to GPLv3
jaroslav@95
     1
/*
jaroslav@264
     2
 * Quoridor server and related libraries
jaroslav@264
     3
 * Copyright (C) 2009-2010 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
jaroslav@95
     4
 *
jaroslav@264
     5
 * This program is free software: you can redistribute it and/or modify
jaroslav@264
     6
 * it under the terms of the GNU General Public License as published by
jaroslav@264
     7
 * the Free Software Foundation, either version 3 of the License.
jaroslav@95
     8
 *
jaroslav@264
     9
 * This program is distributed in the hope that it will be useful,
jaroslav@264
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
jaroslav@264
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
jaroslav@264
    12
 * GNU General Public License for more details.
jaroslav@95
    13
 *
jaroslav@264
    14
 * You should have received a copy of the GNU General Public License
jaroslav@264
    15
 * along with this program. Look for COPYING file in the top folder.
jaroslav@264
    16
 * If not, see http://www.gnu.org/licenses/.
jaroslav@95
    17
 */
jaroslav@95
    18
jaroslav@105
    19
package cz.xelfi.quoridor.freemarkerdor;
jaroslav@95
    20
jaroslav@95
    21
import cz.xelfi.quoridor.Board;
jaroslav@95
    22
import cz.xelfi.quoridor.Fence;
jaroslav@95
    23
import cz.xelfi.quoridor.Player;
jaroslav@95
    24
import java.awt.Color;
jaroslav@102
    25
import java.awt.Font;
jaroslav@95
    26
import java.awt.Graphics2D;
jaroslav@95
    27
import java.awt.Image;
jaroslav@95
    28
import java.awt.Rectangle;
jaroslav@95
    29
import java.awt.image.BufferedImage;
jaroslav@95
    30
jaroslav@95
    31
/** Convertor of a board to Image.
jaroslav@95
    32
 *
jaroslav@95
    33
 * @author Jaroslav Tulach <jtulach@netbeans.org>
jaroslav@95
    34
 */
jaroslav@95
    35
final class BoardImage  {
jaroslav@100
    36
    static Image draw(Board b, int fieldSize) {
jaroslav@95
    37
        int fifth = fieldSize / 10;
jaroslav@95
    38
jaroslav@95
    39
        BufferedImage img = new BufferedImage(fieldSize * 9, fieldSize * 9, BufferedImage.TYPE_INT_ARGB);
jaroslav@95
    40
        Graphics2D g = (Graphics2D) img.getGraphics();
jaroslav@95
    41
jaroslav@95
    42
        g.setColor(Color.lightGray);
jaroslav@95
    43
        for (int i = 0; i < 9; i++) {
jaroslav@95
    44
            for (int j = 0; j < 9; j++) {
jaroslav@95
    45
                final Rectangle r = new Rectangle(i * fieldSize, j * fieldSize, fieldSize, fieldSize);
jaroslav@95
    46
                g.fillRect(r.x + fifth, r.y + fifth, r.width - fifth * 2, r.height - fifth * 2);
jaroslav@95
    47
            }
jaroslav@95
    48
        }
jaroslav@95
    49
jaroslav@95
    50
jaroslav@95
    51
        g.setColor(Color.BLACK);
jaroslav@95
    52
        for (Fence f : b.getFences()) {
jaroslav@95
    53
            int w, h;
jaroslav@95
    54
            switch (f.getOrientation()) {
jaroslav@95
    55
                case HORIZONTAL: w = fieldSize - fifth; h = fifth; break;
jaroslav@95
    56
                case VERTICAL: w = fifth; h = fieldSize - fifth; break;
jaroslav@95
    57
                default: throw new IllegalStateException();
jaroslav@95
    58
            }
jaroslav@95
    59
            int column = (f.getColumn() - 'A') + 1;
jaroslav@95
    60
            int row = 9 - f.getRow();
jaroslav@95
    61
            Rectangle r = new Rectangle(
jaroslav@95
    62
                column * fieldSize - w,
jaroslav@95
    63
                row * fieldSize - h,
jaroslav@95
    64
                2 * w,
jaroslav@95
    65
                2 * h
jaroslav@95
    66
            );
jaroslav@95
    67
            g.fill(r);
jaroslav@95
    68
        }
jaroslav@95
    69
jaroslav@95
    70
        int cnt = 0;
jaroslav@95
    71
        Color[] colors = { Color.WHITE, Color.BLACK, Color.ORANGE, Color.MAGENTA };
jaroslav@95
    72
        int diametr = fieldSize - fifth * 4;
jaroslav@95
    73
        for (Player p : b.getPlayers()) {
jaroslav@95
    74
            int column = p.getColumn();
jaroslav@95
    75
            int row = 8 - p.getRow();
jaroslav@95
    76
            final Rectangle r = new Rectangle(
jaroslav@95
    77
                column * fieldSize + 2 * fifth,
jaroslav@95
    78
                row * fieldSize + 2 * fifth,
jaroslav@95
    79
                diametr,
jaroslav@95
    80
                diametr
jaroslav@95
    81
            );
jaroslav@95
    82
            g.setColor(colors[cnt]);
jaroslav@95
    83
            g.fillOval(r.x, r.y, r.width, r.height);
jaroslav@176
    84
            if (p == b.getCurrentPlayer()) {
jaroslav@177
    85
                g.setColor(Color.lightGray);
jaroslav@176
    86
                g.fillOval(r.x + r.width / 3, r.y + r.height / 3, r.width / 3, r.height / 3);
jaroslav@176
    87
            }
jaroslav@95
    88
            cnt++;
jaroslav@95
    89
        }
jaroslav@95
    90
jaroslav@102
    91
        g.setColor(Color.BLACK);
jaroslav@102
    92
        final Font f = new Font("Monospace", Font.BOLD, fifth * 2);
jaroslav@102
    93
        g.setFont(f);
jaroslav@102
    94
        for (int i = 0; i < 8; i++) {
jaroslav@102
    95
            final char ch = (char) ('A' + i);
jaroslav@102
    96
            g.drawString(Character.toString(ch), i * fieldSize + fieldSize - fifth, fifth + f.getSize() - f.getBaselineFor(ch));
jaroslav@102
    97
        }
jaroslav@102
    98
        for (int i = 0; i < 8; i++) {
jaroslav@102
    99
            String s = "" + (8 - i);
jaroslav@102
   100
            g.drawString(s, fifth, i * fieldSize + fieldSize - fifth + f.getSize() - f.getBaselineFor(s.charAt(0)));
jaroslav@102
   101
        }
jaroslav@102
   102
jaroslav@95
   103
        return img;
jaroslav@95
   104
    }
jaroslav@95
   105
}