jtulach@18
|
1 |
/*
|
jtulach@18
|
2 |
* To change this template, choose Tools | Templates
|
jtulach@18
|
3 |
* and open the template in the editor.
|
jtulach@18
|
4 |
*/
|
jtulach@18
|
5 |
|
jtulach@18
|
6 |
package visidor;
|
jtulach@18
|
7 |
|
jtulach@23
|
8 |
import cz.xelfi.quoridor.Board;
|
jtulach@23
|
9 |
import cz.xelfi.quoridor.Fence;
|
jtulach@23
|
10 |
import cz.xelfi.quoridor.Fence.Orientation;
|
jtulach@23
|
11 |
import cz.xelfi.quoridor.IllegalPositionException;
|
jtulach@23
|
12 |
import cz.xelfi.quoridor.Move;
|
jtulach@18
|
13 |
import java.awt.Color;
|
jtulach@18
|
14 |
import java.awt.Point;
|
jtulach@18
|
15 |
import java.awt.Rectangle;
|
jtulach@18
|
16 |
import javax.swing.JFrame;
|
jtulach@18
|
17 |
import org.netbeans.api.visual.action.ActionFactory;
|
jtulach@18
|
18 |
import org.netbeans.api.visual.action.MoveProvider;
|
jtulach@18
|
19 |
import org.netbeans.api.visual.anchor.AnchorFactory;
|
jtulach@18
|
20 |
import org.netbeans.api.visual.border.BorderFactory;
|
jtulach@18
|
21 |
import org.netbeans.api.visual.widget.ConnectionWidget;
|
jtulach@18
|
22 |
import org.netbeans.api.visual.widget.ImageWidget;
|
jtulach@18
|
23 |
import org.netbeans.api.visual.widget.LabelWidget;
|
jtulach@18
|
24 |
import org.netbeans.api.visual.widget.LayerWidget;
|
jtulach@18
|
25 |
import org.netbeans.api.visual.widget.Scene;
|
jtulach@18
|
26 |
import org.netbeans.api.visual.widget.Widget;
|
jtulach@18
|
27 |
|
jtulach@18
|
28 |
/**
|
jtulach@18
|
29 |
*
|
jtulach@18
|
30 |
* @author Jaroslav Tulach <jtulach@netbeans.org>
|
jtulach@18
|
31 |
*/
|
jtulach@18
|
32 |
public class Viewer implements MoveProvider {
|
jtulach@18
|
33 |
|
jtulach@18
|
34 |
/**
|
jtulach@18
|
35 |
* @param args the command line arguments
|
jtulach@18
|
36 |
*/
|
jtulach@23
|
37 |
public static void main(String[] args) throws IllegalPositionException {
|
jtulach@23
|
38 |
Board b = Board.empty();
|
jtulach@23
|
39 |
for (int i = 1; i <= 8; i++) {
|
jtulach@23
|
40 |
b = b.apply(Move.fence('A', i, Orientation.values()[i % 2]));
|
jtulach@23
|
41 |
}
|
jtulach@23
|
42 |
for (int i = 1; i <= 8; i++) {
|
jtulach@23
|
43 |
b = b.apply(Move.fence('H', i, Orientation.values()[(i + 1) % 2]));
|
jtulach@23
|
44 |
}
|
jtulach@23
|
45 |
//b = b.apply(Move.fence('H', 5, Orientation.HORIZONTAL));
|
jtulach@23
|
46 |
view(b);
|
jtulach@23
|
47 |
}
|
jtulach@23
|
48 |
|
jtulach@23
|
49 |
private static void view(Board b) {
|
jtulach@18
|
50 |
Scene scene = new Scene();
|
jtulach@18
|
51 |
// Scene layerBoard = scene;
|
jtulach@18
|
52 |
final LayerWidget layerLines = new LayerWidget(scene);
|
jtulach@18
|
53 |
scene.addChild(layerLines);
|
jtulach@18
|
54 |
final LayerWidget layerBoard = new LayerWidget(scene);
|
jtulach@18
|
55 |
layerLines.addChild(layerBoard);
|
jtulach@18
|
56 |
final int WIDTH = 50;
|
jtulach@18
|
57 |
final int HEIGHT = 50;
|
jtulach@18
|
58 |
// conn.setSourceAnchor(ahoj);
|
jtulach@18
|
59 |
Widget[][] fields = new Widget[10][];
|
jtulach@18
|
60 |
for (int i = 0; i < fields.length; i++) {
|
jtulach@18
|
61 |
fields[i] = new Widget[10];
|
jtulach@18
|
62 |
for (int j = 0; j < fields[i].length; j++) {
|
jtulach@18
|
63 |
LabelWidget w = new LabelWidget(scene);
|
jtulach@18
|
64 |
w.setPreferredBounds(new Rectangle(i * WIDTH, j * HEIGHT, WIDTH, HEIGHT));
|
jtulach@18
|
65 |
layerBoard.addChild(w);
|
jtulach@18
|
66 |
fields[i][j] = w;
|
jtulach@18
|
67 |
}
|
jtulach@18
|
68 |
}
|
jtulach@18
|
69 |
for (int i = 0; i < fields.length; i++) {
|
jtulach@18
|
70 |
for (int j = 0; j < fields[i].length; j++) {
|
jtulach@18
|
71 |
if (i > 0) {
|
jtulach@18
|
72 |
ConnectionWidget horiz = new ConnectionWidget(scene);
|
jtulach@18
|
73 |
horiz.setSourceAnchor(AnchorFactory.createCenterAnchor(fields[i - 1][j]));
|
jtulach@18
|
74 |
horiz.setTargetAnchor(AnchorFactory.createCenterAnchor(fields[i][j]));
|
jtulach@18
|
75 |
scene.addChild(horiz);
|
jtulach@18
|
76 |
}
|
jtulach@18
|
77 |
if (j > 0) {
|
jtulach@18
|
78 |
ConnectionWidget vert = new ConnectionWidget(scene);
|
jtulach@18
|
79 |
vert.setSourceAnchor(AnchorFactory.createCenterAnchor(fields[i][j - 1]));
|
jtulach@18
|
80 |
vert.setTargetAnchor(AnchorFactory.createCenterAnchor(fields[i][j]));
|
jtulach@18
|
81 |
scene.addChild(vert);
|
jtulach@18
|
82 |
}
|
jtulach@18
|
83 |
}
|
jtulach@18
|
84 |
}
|
jtulach@18
|
85 |
|
jtulach@18
|
86 |
ImageWidget horizontalWall = new ImageWidget(scene);
|
jtulach@19
|
87 |
horizontalWall.setPreferredBounds(new Rectangle(550, 300, 90, 10));
|
jtulach@18
|
88 |
horizontalWall.setBackground(Color.BLACK);
|
jtulach@18
|
89 |
horizontalWall.getActions().addAction(ActionFactory.createMoveAction(null, new Viewer(true)));
|
jtulach@18
|
90 |
horizontalWall.setBorder(BorderFactory.createLineBorder());
|
jtulach@24
|
91 |
horizontalWall.setOpaque(true);
|
jtulach@18
|
92 |
layerBoard.addChild(horizontalWall);
|
jtulach@18
|
93 |
|
jtulach@18
|
94 |
ImageWidget verticalWall = new ImageWidget(scene);
|
jtulach@19
|
95 |
verticalWall.setPreferredBounds(new Rectangle(600, 150, 10, 90));
|
jtulach@18
|
96 |
verticalWall.setBackground(Color.BLACK);
|
jtulach@18
|
97 |
verticalWall.getActions().addAction(ActionFactory.createMoveAction(null, new Viewer(false)));
|
jtulach@18
|
98 |
verticalWall.setBorder(BorderFactory.createLineBorder());
|
jtulach@24
|
99 |
verticalWall.setOpaque(true);
|
jtulach@18
|
100 |
layerBoard.addChild(verticalWall);
|
jtulach@18
|
101 |
|
jtulach@23
|
102 |
for (Fence f : b.getFences()) {
|
jtulach@23
|
103 |
Rectangle r = fenceRectangle(f.getColumn(), f.getRow(), f.getOrientation());
|
jtulach@23
|
104 |
ImageWidget fenceWall = new ImageWidget(scene);
|
jtulach@23
|
105 |
fenceWall.setPreferredBounds(r);
|
jtulach@23
|
106 |
fenceWall.setBackground(Color.BLACK);
|
jtulach@23
|
107 |
fenceWall.setBorder(BorderFactory.createLineBorder());
|
jtulach@24
|
108 |
fenceWall.setOpaque(true);
|
jtulach@23
|
109 |
layerBoard.addChild(fenceWall);
|
jtulach@23
|
110 |
}
|
jtulach@23
|
111 |
|
jtulach@18
|
112 |
JFrame f = new JFrame();
|
jtulach@18
|
113 |
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
jtulach@18
|
114 |
|
jtulach@18
|
115 |
f.add(scene.createView());
|
jtulach@18
|
116 |
f.pack();
|
jtulach@18
|
117 |
f.setVisible(true);
|
jtulach@18
|
118 |
}
|
jtulach@18
|
119 |
|
jtulach@23
|
120 |
private static Rectangle fenceRectangle(char column, int row, Fence.Orientation o) {
|
jtulach@23
|
121 |
int w, h;
|
jtulach@23
|
122 |
switch (o) {
|
jtulach@23
|
123 |
case HORIZONTAL: w = 45; h = 5; break;
|
jtulach@23
|
124 |
case VERTICAL: w = 5; h = 45; break;
|
jtulach@23
|
125 |
default: throw new IllegalStateException();
|
jtulach@23
|
126 |
}
|
jtulach@23
|
127 |
|
jtulach@23
|
128 |
return new Rectangle(
|
jtulach@23
|
129 |
(column - 'A' + 1) * 50 + 25 - w, row * 50 + 25 - h, 2 * w, 2 * h
|
jtulach@23
|
130 |
);
|
jtulach@23
|
131 |
}
|
jtulach@23
|
132 |
|
jtulach@18
|
133 |
final boolean horizontal;
|
jtulach@18
|
134 |
|
jtulach@18
|
135 |
Point orig;
|
jtulach@18
|
136 |
|
jtulach@18
|
137 |
private Viewer(boolean horizontal) {
|
jtulach@18
|
138 |
this.horizontal = horizontal;
|
jtulach@18
|
139 |
}
|
jtulach@18
|
140 |
public void movementStarted(Widget widget) {
|
jtulach@18
|
141 |
System.err.println("started: " + widget.getBounds());
|
jtulach@18
|
142 |
}
|
jtulach@18
|
143 |
|
jtulach@18
|
144 |
public void movementFinished(Widget widget) {
|
jtulach@18
|
145 |
System.err.println("finished: " + widget.getLocation());
|
jtulach@18
|
146 |
}
|
jtulach@18
|
147 |
|
jtulach@18
|
148 |
public Point getOriginalLocation(Widget widget) {
|
jtulach@18
|
149 |
orig = new Point(widget.getBounds().x, widget.getBounds().y);
|
jtulach@18
|
150 |
System.err.println("orig: " + orig);
|
jtulach@18
|
151 |
return orig;
|
jtulach@18
|
152 |
}
|
jtulach@18
|
153 |
|
jtulach@18
|
154 |
public void setNewLocation(Widget widget, Point location) {
|
jtulach@18
|
155 |
widget.setPreferredBounds(new Rectangle(
|
jtulach@19
|
156 |
round(location.x, true) + (horizontal ? 5 : 0),
|
jtulach@19
|
157 |
round(location.y, false) + (horizontal ? 0 : 5),
|
jtulach@19
|
158 |
horizontal ? 90 : 10,
|
jtulach@19
|
159 |
horizontal ? 10 : 90)
|
jtulach@18
|
160 |
);
|
jtulach@18
|
161 |
}
|
jtulach@18
|
162 |
|
jtulach@18
|
163 |
int round(int p, boolean cmpHori) {
|
jtulach@18
|
164 |
// p = horizontal ? orig.x + p: orig.y + p;
|
jtulach@18
|
165 |
int onboard = p / 50;
|
jtulach@18
|
166 |
System.err.println(cmpHori ? "hori: " + onboard : " vert: " + onboard);
|
jtulach@18
|
167 |
if (onboard < 0) {
|
jtulach@18
|
168 |
onboard = 0;
|
jtulach@18
|
169 |
}
|
jtulach@18
|
170 |
if (onboard >= 9) {
|
jtulach@18
|
171 |
return p;
|
jtulach@18
|
172 |
}
|
jtulach@18
|
173 |
int real = 25 + onboard * 50;
|
jtulach@18
|
174 |
if (horizontal != cmpHori) {
|
jtulach@18
|
175 |
return real - 5;
|
jtulach@18
|
176 |
} else {
|
jtulach@18
|
177 |
return real;
|
jtulach@18
|
178 |
}
|
jtulach@18
|
179 |
}
|
jtulach@18
|
180 |
}
|