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