jtulach@25
|
1 |
package cz.xelfi.quoridor.visidor;
|
jtulach@18
|
2 |
|
jtulach@23
|
3 |
import cz.xelfi.quoridor.Board;
|
jtulach@23
|
4 |
import cz.xelfi.quoridor.Fence;
|
jtulach@23
|
5 |
import cz.xelfi.quoridor.Fence.Orientation;
|
jtulach@23
|
6 |
import cz.xelfi.quoridor.IllegalPositionException;
|
jtulach@23
|
7 |
import cz.xelfi.quoridor.Move;
|
jtulach@29
|
8 |
import cz.xelfi.quoridor.Player;
|
jtulach@18
|
9 |
import java.awt.Color;
|
jtulach@29
|
10 |
import java.awt.Graphics2D;
|
jtulach@18
|
11 |
import java.awt.Point;
|
jtulach@18
|
12 |
import java.awt.Rectangle;
|
jtulach@18
|
13 |
import javax.swing.JFrame;
|
jtulach@29
|
14 |
import javax.swing.JPanel;
|
jtulach@18
|
15 |
import org.netbeans.api.visual.action.MoveProvider;
|
jtulach@18
|
16 |
import org.netbeans.api.visual.border.BorderFactory;
|
jtulach@18
|
17 |
import org.netbeans.api.visual.widget.ImageWidget;
|
jtulach@18
|
18 |
import org.netbeans.api.visual.widget.LayerWidget;
|
jtulach@18
|
19 |
import org.netbeans.api.visual.widget.Scene;
|
jtulach@18
|
20 |
import org.netbeans.api.visual.widget.Widget;
|
jtulach@29
|
21 |
import org.openide.util.Exceptions;
|
jtulach@18
|
22 |
|
jtulach@18
|
23 |
/**
|
jtulach@18
|
24 |
*
|
jtulach@18
|
25 |
* @author Jaroslav Tulach <jtulach@netbeans.org>
|
jtulach@18
|
26 |
*/
|
jtulach@29
|
27 |
final class Viewer extends JPanel {
|
jtulach@29
|
28 |
private Board board;
|
jtulach@29
|
29 |
private final Scene scene;
|
jtulach@29
|
30 |
|
jtulach@29
|
31 |
public Viewer() {
|
jtulach@29
|
32 |
this(Board.empty());
|
jtulach@29
|
33 |
}
|
jtulach@29
|
34 |
|
jtulach@29
|
35 |
private Viewer(Board b) {
|
jtulach@29
|
36 |
this.board = b;
|
jtulach@29
|
37 |
this.scene = new Scene();
|
jtulach@29
|
38 |
add(scene.createView());
|
jtulach@29
|
39 |
view(scene, board);
|
jtulach@29
|
40 |
}
|
jtulach@29
|
41 |
|
jtulach@18
|
42 |
|
jtulach@18
|
43 |
/**
|
jtulach@18
|
44 |
* @param args the command line arguments
|
jtulach@18
|
45 |
*/
|
jtulach@23
|
46 |
public static void main(String[] args) throws IllegalPositionException {
|
jtulach@23
|
47 |
Board b = Board.empty();
|
jtulach@23
|
48 |
for (int i = 1; i <= 8; i++) {
|
jtulach@23
|
49 |
b = b.apply(Move.fence('A', i, Orientation.values()[i % 2]));
|
jtulach@23
|
50 |
}
|
jtulach@23
|
51 |
for (int i = 1; i <= 8; i++) {
|
jtulach@23
|
52 |
b = b.apply(Move.fence('H', i, Orientation.values()[(i + 1) % 2]));
|
jtulach@23
|
53 |
}
|
jtulach@29
|
54 |
|
jtulach@29
|
55 |
JFrame f = new JFrame();
|
jtulach@29
|
56 |
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
jtulach@29
|
57 |
f.add(new Viewer(b));
|
jtulach@29
|
58 |
f.pack();
|
jtulach@29
|
59 |
f.setVisible(true);
|
jtulach@23
|
60 |
}
|
jtulach@23
|
61 |
|
jtulach@29
|
62 |
private void view(Scene scene, Board b) {
|
jtulach@29
|
63 |
scene.removeChildren();
|
jtulach@91
|
64 |
drawBoard(scene, b);
|
jtulach@91
|
65 |
}
|
jtulach@91
|
66 |
|
jtulach@91
|
67 |
static void drawBoard(Scene scene, Board b) {
|
jtulach@18
|
68 |
// Scene layerBoard = scene;
|
jtulach@91
|
69 |
scene.setPreferredLocation(new Point(0, 0));
|
jtulach@18
|
70 |
final LayerWidget layerLines = new LayerWidget(scene);
|
jtulach@18
|
71 |
scene.addChild(layerLines);
|
jtulach@18
|
72 |
final LayerWidget layerBoard = new LayerWidget(scene);
|
jtulach@18
|
73 |
layerLines.addChild(layerBoard);
|
jtulach@29
|
74 |
final int WDTH = 50;
|
jtulach@29
|
75 |
final int HGHT = 50;
|
jtulach@91
|
76 |
|
jtulach@91
|
77 |
|
jtulach@91
|
78 |
for (int i = 0; i < 9; i++) {
|
jtulach@91
|
79 |
for (int j = 0; j < 9; j++) {
|
jtulach@91
|
80 |
Widget w = new FieldWidget(scene);
|
jtulach@29
|
81 |
w.setPreferredBounds(new Rectangle(i * WDTH, j * HGHT, WDTH, HGHT));
|
jtulach@18
|
82 |
layerBoard.addChild(w);
|
jtulach@18
|
83 |
}
|
jtulach@18
|
84 |
}
|
jtulach@18
|
85 |
|
jtulach@91
|
86 |
/*
|
jtulach@18
|
87 |
ImageWidget horizontalWall = new ImageWidget(scene);
|
jtulach@19
|
88 |
horizontalWall.setPreferredBounds(new Rectangle(550, 300, 90, 10));
|
jtulach@18
|
89 |
horizontalWall.setBackground(Color.BLACK);
|
jtulach@29
|
90 |
horizontalWall.getActions().addAction(ActionFactory.createMoveAction(null, new MoveControl(true)));
|
jtulach@18
|
91 |
horizontalWall.setBorder(BorderFactory.createLineBorder());
|
jtulach@24
|
92 |
horizontalWall.setOpaque(true);
|
jtulach@18
|
93 |
layerBoard.addChild(horizontalWall);
|
jtulach@18
|
94 |
|
jtulach@18
|
95 |
ImageWidget verticalWall = new ImageWidget(scene);
|
jtulach@19
|
96 |
verticalWall.setPreferredBounds(new Rectangle(600, 150, 10, 90));
|
jtulach@18
|
97 |
verticalWall.setBackground(Color.BLACK);
|
jtulach@29
|
98 |
verticalWall.getActions().addAction(ActionFactory.createMoveAction(null, new MoveControl(false)));
|
jtulach@18
|
99 |
verticalWall.setBorder(BorderFactory.createLineBorder());
|
jtulach@24
|
100 |
verticalWall.setOpaque(true);
|
jtulach@18
|
101 |
layerBoard.addChild(verticalWall);
|
jtulach@91
|
102 |
*/
|
jtulach@23
|
103 |
for (Fence f : b.getFences()) {
|
jtulach@23
|
104 |
Rectangle r = fenceRectangle(f.getColumn(), f.getRow(), f.getOrientation());
|
jtulach@23
|
105 |
ImageWidget fenceWall = new ImageWidget(scene);
|
jtulach@23
|
106 |
fenceWall.setPreferredBounds(r);
|
jtulach@23
|
107 |
fenceWall.setBackground(Color.BLACK);
|
jtulach@23
|
108 |
fenceWall.setBorder(BorderFactory.createLineBorder());
|
jtulach@24
|
109 |
fenceWall.setOpaque(true);
|
jtulach@23
|
110 |
layerBoard.addChild(fenceWall);
|
jtulach@23
|
111 |
}
|
jtulach@23
|
112 |
|
jtulach@29
|
113 |
int cnt = 0;
|
jtulach@91
|
114 |
Color[] colors = { Color.WHITE, Color.BLACK, Color.ORANGE, Color.MAGENTA };
|
jtulach@29
|
115 |
for (Player p : b.getPlayers()) {
|
jtulach@91
|
116 |
PlayerWidget pw = new PlayerWidget(scene, colors[cnt]);
|
jtulach@91
|
117 |
pw.setPreferredBounds(new Rectangle(p.getColumn() * WDTH, p.getRow() * HGHT, WDTH, HGHT));
|
jtulach@29
|
118 |
layerBoard.addChild(pw);
|
jtulach@29
|
119 |
|
jtulach@29
|
120 |
cnt++;
|
jtulach@29
|
121 |
}
|
jtulach@18
|
122 |
}
|
jtulach@18
|
123 |
|
jtulach@23
|
124 |
private static Rectangle fenceRectangle(char column, int row, Fence.Orientation o) {
|
jtulach@23
|
125 |
int w, h;
|
jtulach@23
|
126 |
switch (o) {
|
jtulach@23
|
127 |
case HORIZONTAL: w = 45; h = 5; break;
|
jtulach@23
|
128 |
case VERTICAL: w = 5; h = 45; break;
|
jtulach@23
|
129 |
default: throw new IllegalStateException();
|
jtulach@23
|
130 |
}
|
jtulach@23
|
131 |
|
jtulach@23
|
132 |
return new Rectangle(
|
jtulach@91
|
133 |
(column - 'A' + 1) * 50 - w, row * 50 - h, 2 * w, 2 * h
|
jtulach@23
|
134 |
);
|
jtulach@23
|
135 |
}
|
jtulach@23
|
136 |
|
jtulach@29
|
137 |
private class MoveControl implements MoveProvider {
|
jtulach@29
|
138 |
final boolean horizontal;
|
jtulach@18
|
139 |
|
jtulach@29
|
140 |
Point orig;
|
jtulach@18
|
141 |
|
jtulach@29
|
142 |
private MoveControl(boolean horizontal) {
|
jtulach@29
|
143 |
this.horizontal = horizontal;
|
jtulach@29
|
144 |
}
|
jtulach@29
|
145 |
public void movementStarted(Widget widget) {
|
jtulach@29
|
146 |
System.err.println("started: " + widget.getBounds());
|
jtulach@29
|
147 |
}
|
jtulach@29
|
148 |
|
jtulach@29
|
149 |
public void movementFinished(Widget widget) {
|
jtulach@29
|
150 |
try {
|
jtulach@32
|
151 |
final Rectangle bounds = widget.getBounds();
|
jtulach@32
|
152 |
final Move m = createMoveForDrop( bounds);
|
jtulach@32
|
153 |
System.err.println("finish: " + m);
|
jtulach@32
|
154 |
board = board.apply(m);
|
jtulach@29
|
155 |
} catch (IllegalPositionException ex) {
|
jtulach@29
|
156 |
Exceptions.printStackTrace(ex);
|
jtulach@29
|
157 |
}
|
jtulach@29
|
158 |
|
jtulach@29
|
159 |
view(scene, board);
|
jtulach@29
|
160 |
}
|
jtulach@29
|
161 |
|
jtulach@29
|
162 |
public Point getOriginalLocation(Widget widget) {
|
jtulach@32
|
163 |
final Rectangle b = widget.getBounds();
|
jtulach@32
|
164 |
orig = new Point(b.x, b.y);
|
jtulach@32
|
165 |
try {
|
jtulach@32
|
166 |
System.err.println(" where: " + createMoveForDrop(b));
|
jtulach@32
|
167 |
} catch (IllegalPositionException ex) {
|
jtulach@32
|
168 |
Exceptions.printStackTrace(ex);
|
jtulach@32
|
169 |
}
|
jtulach@29
|
170 |
return orig;
|
jtulach@29
|
171 |
}
|
jtulach@29
|
172 |
|
jtulach@29
|
173 |
public void setNewLocation(Widget widget, Point location) {
|
jtulach@32
|
174 |
final Rectangle b = new Rectangle(
|
jtulach@29
|
175 |
round(location.x, true) + (horizontal ? 5 : 0),
|
jtulach@29
|
176 |
round(location.y, false) + (horizontal ? 0 : 5),
|
jtulach@29
|
177 |
horizontal ? 90 : 10,
|
jtulach@32
|
178 |
horizontal ? 10 : 90
|
jtulach@29
|
179 |
);
|
jtulach@32
|
180 |
widget.setPreferredBounds(b);
|
jtulach@32
|
181 |
try {
|
jtulach@32
|
182 |
System.err.println("move: " + createMoveForDrop(b));
|
jtulach@32
|
183 |
} catch (IllegalPositionException ex) {
|
jtulach@32
|
184 |
System.err.println("no move");
|
jtulach@32
|
185 |
}
|
jtulach@32
|
186 |
}
|
jtulach@32
|
187 |
|
jtulach@32
|
188 |
Move createMoveForDrop(final Rectangle bounds) throws IllegalPositionException {
|
jtulach@32
|
189 |
int column = bounds.x / 50;
|
jtulach@32
|
190 |
int row = bounds.y / 50;
|
jtulach@32
|
191 |
Orientation o = bounds.width > bounds.height ? Orientation.HORIZONTAL : Orientation.VERTICAL;
|
jtulach@32
|
192 |
switch (o) {
|
jtulach@32
|
193 |
case HORIZONTAL: row--; break;
|
jtulach@32
|
194 |
case VERTICAL: column--; break;
|
jtulach@32
|
195 |
default: assert false;
|
jtulach@32
|
196 |
}
|
jtulach@32
|
197 |
if (column < 0) {
|
jtulach@32
|
198 |
column = 0;
|
jtulach@32
|
199 |
}
|
jtulach@32
|
200 |
if (column >= 9) {
|
jtulach@32
|
201 |
column = 8;
|
jtulach@32
|
202 |
}
|
jtulach@32
|
203 |
if (row < 0) {
|
jtulach@32
|
204 |
row = 0;
|
jtulach@32
|
205 |
}
|
jtulach@32
|
206 |
if (row > 8) {
|
jtulach@32
|
207 |
row = 8;
|
jtulach@32
|
208 |
}
|
jtulach@32
|
209 |
Move m = Move.fence((char) ('A' + column), 1 + row, o);
|
jtulach@32
|
210 |
return m;
|
jtulach@29
|
211 |
}
|
jtulach@29
|
212 |
|
jtulach@29
|
213 |
int round(int p, boolean cmpHori) {
|
jtulach@29
|
214 |
// p = horizontal ? orig.x + p: orig.y + p;
|
jtulach@29
|
215 |
int onboard = p / 50;
|
jtulach@29
|
216 |
if (onboard < 0) {
|
jtulach@29
|
217 |
onboard = 0;
|
jtulach@29
|
218 |
}
|
jtulach@29
|
219 |
if (onboard >= 9) {
|
jtulach@29
|
220 |
return p;
|
jtulach@29
|
221 |
}
|
jtulach@29
|
222 |
int real = 25 + onboard * 50;
|
jtulach@29
|
223 |
if (horizontal != cmpHori) {
|
jtulach@29
|
224 |
return real - 5;
|
jtulach@29
|
225 |
} else {
|
jtulach@29
|
226 |
return real;
|
jtulach@29
|
227 |
}
|
jtulach@29
|
228 |
}
|
jtulach@18
|
229 |
}
|
jtulach@18
|
230 |
|
jtulach@29
|
231 |
private static final class PlayerWidget extends Widget {
|
jtulach@91
|
232 |
private final Color color;
|
jtulach@91
|
233 |
|
jtulach@91
|
234 |
public PlayerWidget(Scene s, Color color) {
|
jtulach@91
|
235 |
super(s);
|
jtulach@91
|
236 |
this.color = color;
|
jtulach@91
|
237 |
}
|
jtulach@91
|
238 |
|
jtulach@91
|
239 |
@Override
|
jtulach@91
|
240 |
protected Rectangle calculateClientArea() {
|
jtulach@91
|
241 |
return getBounds();
|
jtulach@91
|
242 |
}
|
jtulach@91
|
243 |
|
jtulach@91
|
244 |
@Override
|
jtulach@91
|
245 |
protected void paintWidget() {
|
jtulach@91
|
246 |
Graphics2D g = getGraphics();
|
jtulach@91
|
247 |
Rectangle b = getBounds();
|
jtulach@91
|
248 |
g.setColor(color);
|
jtulach@91
|
249 |
g.fillOval(b.x + 8, b.y + 8, b.width - 16, b.height - 16);
|
jtulach@91
|
250 |
}
|
jtulach@91
|
251 |
}
|
jtulach@91
|
252 |
private static final class FieldWidget extends Widget {
|
jtulach@91
|
253 |
public FieldWidget(Scene s) {
|
jtulach@29
|
254 |
super(s);
|
jtulach@29
|
255 |
}
|
jtulach@18
|
256 |
|
jtulach@29
|
257 |
@Override
|
jtulach@29
|
258 |
protected Rectangle calculateClientArea() {
|
jtulach@33
|
259 |
return getBounds();
|
jtulach@29
|
260 |
}
|
jtulach@18
|
261 |
|
jtulach@29
|
262 |
@Override
|
jtulach@29
|
263 |
protected void paintWidget() {
|
jtulach@29
|
264 |
Graphics2D g = getGraphics();
|
jtulach@33
|
265 |
Rectangle b = getBounds();
|
jtulach@91
|
266 |
g.setColor(Color.lightGray);
|
jtulach@91
|
267 |
g.fillRect(b.x + 5, b.y + 5, b.width - 10, b.height - 10);
|
jtulach@18
|
268 |
}
|
jtulach@18
|
269 |
}
|
jtulach@18
|
270 |
}
|