jtulach@18
|
1 |
/*
|
jaroslav@178
|
2 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
|
jaroslav@178
|
3 |
*
|
jaroslav@178
|
4 |
* The contents of this file are subject to the terms of either the GNU
|
jaroslav@178
|
5 |
* General Public License Version 2 only ("GPL") or the Common
|
jaroslav@178
|
6 |
* Development and Distribution License("CDDL") (collectively, the
|
jaroslav@178
|
7 |
* "License"). You may not use this file except in compliance with the
|
jaroslav@178
|
8 |
* License. You can obtain a copy of the License at
|
jaroslav@178
|
9 |
* http://www.netbeans.org/cddl-gplv2.html
|
jaroslav@178
|
10 |
* or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
|
jaroslav@178
|
11 |
* specific language governing permissions and limitations under the
|
jaroslav@178
|
12 |
* License. When distributing the software, include this License Header
|
jaroslav@178
|
13 |
* Notice in each file and include the License file at
|
jaroslav@178
|
14 |
* nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
|
jaroslav@178
|
15 |
* particular file as subject to the "Classpath" exception as provided
|
jaroslav@178
|
16 |
* by Sun in the GPL Version 2 section of the License file that
|
jaroslav@178
|
17 |
* accompanied this code. If applicable, add the following below the
|
jaroslav@178
|
18 |
* License Header, with the fields enclosed by brackets [] replaced by
|
jaroslav@178
|
19 |
* your own identifying information:
|
jaroslav@178
|
20 |
* "Portions Copyrighted [year] [name of copyright owner]"
|
jaroslav@178
|
21 |
*
|
jaroslav@178
|
22 |
* Contributor(s):
|
jaroslav@178
|
23 |
*
|
jaroslav@178
|
24 |
* Portions Copyrighted 2009 Jaroslav Tulach
|
jtulach@18
|
25 |
*/
|
jtulach@25
|
26 |
package cz.xelfi.quoridor.visidor;
|
jtulach@18
|
27 |
|
jtulach@23
|
28 |
import cz.xelfi.quoridor.Board;
|
jtulach@23
|
29 |
import cz.xelfi.quoridor.Fence;
|
jtulach@23
|
30 |
import cz.xelfi.quoridor.Fence.Orientation;
|
jtulach@23
|
31 |
import cz.xelfi.quoridor.IllegalPositionException;
|
jtulach@23
|
32 |
import cz.xelfi.quoridor.Move;
|
jtulach@29
|
33 |
import cz.xelfi.quoridor.Player;
|
jtulach@18
|
34 |
import java.awt.Color;
|
jaroslav@248
|
35 |
import java.awt.Font;
|
jaroslav@248
|
36 |
import java.awt.Graphics;
|
jtulach@29
|
37 |
import java.awt.Graphics2D;
|
jtulach@18
|
38 |
import java.awt.Rectangle;
|
jtulach@18
|
39 |
import javax.swing.JFrame;
|
jtulach@29
|
40 |
import javax.swing.JPanel;
|
jtulach@18
|
41 |
|
jtulach@18
|
42 |
/**
|
jtulach@18
|
43 |
*
|
jtulach@18
|
44 |
* @author Jaroslav Tulach <jtulach@netbeans.org>
|
jtulach@18
|
45 |
*/
|
jtulach@29
|
46 |
final class Viewer extends JPanel {
|
jtulach@29
|
47 |
private Board board;
|
jtulach@29
|
48 |
|
jtulach@29
|
49 |
public Viewer() {
|
jtulach@29
|
50 |
this(Board.empty());
|
jtulach@29
|
51 |
}
|
jtulach@29
|
52 |
|
jtulach@29
|
53 |
private Viewer(Board b) {
|
jtulach@29
|
54 |
this.board = b;
|
jtulach@29
|
55 |
}
|
jtulach@29
|
56 |
|
jtulach@18
|
57 |
|
jtulach@18
|
58 |
/**
|
jtulach@18
|
59 |
* @param args the command line arguments
|
jtulach@18
|
60 |
*/
|
jtulach@23
|
61 |
public static void main(String[] args) throws IllegalPositionException {
|
jtulach@23
|
62 |
Board b = Board.empty();
|
jtulach@23
|
63 |
for (int i = 1; i <= 8; i++) {
|
jtulach@23
|
64 |
b = b.apply(Move.fence('A', i, Orientation.values()[i % 2]));
|
jtulach@23
|
65 |
}
|
jtulach@23
|
66 |
for (int i = 1; i <= 8; i++) {
|
jtulach@23
|
67 |
b = b.apply(Move.fence('H', i, Orientation.values()[(i + 1) % 2]));
|
jtulach@23
|
68 |
}
|
jtulach@29
|
69 |
|
jtulach@29
|
70 |
JFrame f = new JFrame();
|
jtulach@29
|
71 |
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
jtulach@29
|
72 |
f.add(new Viewer(b));
|
jtulach@29
|
73 |
f.pack();
|
jtulach@29
|
74 |
f.setVisible(true);
|
jtulach@23
|
75 |
}
|
jtulach@23
|
76 |
|
jaroslav@248
|
77 |
@Override
|
jaroslav@248
|
78 |
protected void paintComponent(Graphics gg) {
|
jaroslav@248
|
79 |
int fieldSize = Math.min(getSize().width, getSize().height) / 9;
|
jaroslav@248
|
80 |
int fifth = fieldSize / 10;
|
jaroslav@248
|
81 |
Graphics2D g = (Graphics2D) gg;
|
jaroslav@248
|
82 |
|
jaroslav@248
|
83 |
int xdelta = getSize().width - fieldSize * 9;
|
jaroslav@248
|
84 |
int ydelta = getSize().height - fieldSize * 9;
|
jaroslav@248
|
85 |
|
jaroslav@248
|
86 |
g.translate(xdelta / 2, ydelta / 2);
|
jaroslav@248
|
87 |
|
jaroslav@248
|
88 |
g.setColor(Color.lightGray);
|
jaroslav@248
|
89 |
for (int i = 0; i < 9; i++) {
|
jaroslav@248
|
90 |
for (int j = 0; j < 9; j++) {
|
jaroslav@248
|
91 |
final Rectangle r = new Rectangle(i * fieldSize, j * fieldSize, fieldSize, fieldSize);
|
jaroslav@248
|
92 |
g.fillRect(r.x + fifth, r.y + fifth, r.width - fifth * 2, r.height - fifth * 2);
|
jtulach@18
|
93 |
}
|
jtulach@18
|
94 |
}
|
jtulach@18
|
95 |
|
jtulach@18
|
96 |
|
jaroslav@248
|
97 |
g.setColor(Color.BLACK);
|
jaroslav@248
|
98 |
for (Fence f : board.getFences()) {
|
jaroslav@248
|
99 |
int w, h;
|
jaroslav@248
|
100 |
switch (f.getOrientation()) {
|
jaroslav@248
|
101 |
case HORIZONTAL: w = fieldSize - fifth; h = fifth; break;
|
jaroslav@248
|
102 |
case VERTICAL: w = fifth; h = fieldSize - fifth; break;
|
jaroslav@248
|
103 |
default: throw new IllegalStateException();
|
jaroslav@248
|
104 |
}
|
jaroslav@248
|
105 |
int column = (f.getColumn() - 'A') + 1;
|
jaroslav@248
|
106 |
int row = 9 - f.getRow();
|
jaroslav@248
|
107 |
Rectangle r = new Rectangle(
|
jaroslav@248
|
108 |
column * fieldSize - w,
|
jaroslav@248
|
109 |
row * fieldSize - h,
|
jaroslav@248
|
110 |
2 * w,
|
jaroslav@248
|
111 |
2 * h
|
jaroslav@248
|
112 |
);
|
jaroslav@248
|
113 |
g.fill(r);
|
jtulach@23
|
114 |
}
|
jtulach@23
|
115 |
|
jtulach@29
|
116 |
int cnt = 0;
|
jaroslav@248
|
117 |
Color[] colors = { Color.WHITE, Color.BLACK, Color.ORANGE, Color.MAGENTA };
|
jaroslav@248
|
118 |
int diametr = fieldSize - fifth * 4;
|
jaroslav@248
|
119 |
for (Player p : board.getPlayers()) {
|
jaroslav@248
|
120 |
int column = p.getColumn();
|
jaroslav@248
|
121 |
int row = 8 - p.getRow();
|
jaroslav@248
|
122 |
final Rectangle r = new Rectangle(
|
jaroslav@248
|
123 |
column * fieldSize + 2 * fifth,
|
jaroslav@248
|
124 |
row * fieldSize + 2 * fifth,
|
jaroslav@248
|
125 |
diametr,
|
jaroslav@248
|
126 |
diametr
|
jaroslav@248
|
127 |
);
|
jaroslav@248
|
128 |
g.setColor(colors[cnt]);
|
jaroslav@248
|
129 |
g.fillOval(r.x, r.y, r.width, r.height);
|
jaroslav@248
|
130 |
if (p == board.getCurrentPlayer()) {
|
jaroslav@248
|
131 |
g.setColor(Color.lightGray);
|
jaroslav@248
|
132 |
g.fillOval(r.x + r.width / 3, r.y + r.height / 3, r.width / 3, r.height / 3);
|
jtulach@29
|
133 |
}
|
jaroslav@248
|
134 |
cnt++;
|
jaroslav@248
|
135 |
}
|
jtulach@29
|
136 |
|
jaroslav@248
|
137 |
g.setColor(Color.BLACK);
|
jaroslav@248
|
138 |
final Font f = new Font("Monospace", Font.BOLD, fifth * 2);
|
jaroslav@248
|
139 |
g.setFont(f);
|
jaroslav@248
|
140 |
for (int i = 0; i < 8; i++) {
|
jaroslav@248
|
141 |
final char ch = (char) ('A' + i);
|
jaroslav@248
|
142 |
g.drawString(Character.toString(ch), i * fieldSize + fieldSize - fifth, fifth + f.getSize() - f.getBaselineFor(ch));
|
jaroslav@248
|
143 |
}
|
jaroslav@248
|
144 |
for (int i = 0; i < 8; i++) {
|
jaroslav@248
|
145 |
String s = "" + (8 - i);
|
jaroslav@248
|
146 |
g.drawString(s, fifth, i * fieldSize + fieldSize - fifth + f.getSize() - f.getBaselineFor(s.charAt(0)));
|
jtulach@29
|
147 |
}
|
jtulach@18
|
148 |
}
|
jtulach@18
|
149 |
|
jtulach@18
|
150 |
}
|