2 * Quoridor server and related libraries
3 * Copyright (C) 2009-2010 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
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.
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.
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/.
19 package cz.xelfi.quoridor.freemarkerdor;
21 import cz.xelfi.quoridor.Board;
22 import cz.xelfi.quoridor.Fence;
23 import cz.xelfi.quoridor.Player;
24 import java.awt.Color;
26 import java.awt.Graphics2D;
27 import java.awt.Image;
28 import java.awt.Rectangle;
29 import java.awt.image.BufferedImage;
31 /** Convertor of a board to Image.
33 * @author Jaroslav Tulach <jtulach@netbeans.org>
35 final class BoardImage {
36 static Image draw(Board b, int fieldSize) {
37 int fifth = fieldSize / 10;
39 BufferedImage img = new BufferedImage(fieldSize * 9, fieldSize * 9, BufferedImage.TYPE_INT_ARGB);
40 Graphics2D g = (Graphics2D) img.getGraphics();
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);
51 g.setColor(Color.BLACK);
52 for (Fence f : b.getFences()) {
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();
59 int column = (f.getColumn() - 'A') + 1;
60 int row = 9 - f.getRow();
61 Rectangle r = new Rectangle(
62 column * fieldSize - w,
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,
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);
91 g.setColor(Color.BLACK);
92 final Font f = new Font("Monospace", Font.BOLD, fifth * 2);
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));
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)));