2 * To change this template, choose Tools | Templates
3 * and open the template in the editor.
8 import cz.xelfi.quoridor.Board;
9 import cz.xelfi.quoridor.Fence;
10 import cz.xelfi.quoridor.Fence.Orientation;
11 import cz.xelfi.quoridor.IllegalPositionException;
12 import cz.xelfi.quoridor.Move;
13 import java.awt.Color;
14 import java.awt.Point;
15 import java.awt.Rectangle;
16 import javax.swing.JFrame;
17 import org.netbeans.api.visual.action.ActionFactory;
18 import org.netbeans.api.visual.action.MoveProvider;
19 import org.netbeans.api.visual.anchor.AnchorFactory;
20 import org.netbeans.api.visual.border.BorderFactory;
21 import org.netbeans.api.visual.widget.ConnectionWidget;
22 import org.netbeans.api.visual.widget.ImageWidget;
23 import org.netbeans.api.visual.widget.LabelWidget;
24 import org.netbeans.api.visual.widget.LayerWidget;
25 import org.netbeans.api.visual.widget.Scene;
26 import org.netbeans.api.visual.widget.Widget;
30 * @author Jaroslav Tulach <jtulach@netbeans.org>
32 public class Viewer implements MoveProvider {
35 * @param args the command line arguments
37 public static void main(String[] args) throws IllegalPositionException {
38 Board b = Board.empty();
39 for (int i = 1; i <= 8; i++) {
40 b = b.apply(Move.fence('A', i, Orientation.values()[i % 2]));
42 for (int i = 1; i <= 8; i++) {
43 b = b.apply(Move.fence('H', i, Orientation.values()[(i + 1) % 2]));
45 //b = b.apply(Move.fence('H', 5, Orientation.HORIZONTAL));
49 private static void view(Board b) {
50 Scene scene = new Scene();
51 // Scene layerBoard = scene;
52 final LayerWidget layerLines = new LayerWidget(scene);
53 scene.addChild(layerLines);
54 final LayerWidget layerBoard = new LayerWidget(scene);
55 layerLines.addChild(layerBoard);
57 final int HEIGHT = 50;
58 // conn.setSourceAnchor(ahoj);
59 Widget[][] fields = new Widget[10][];
60 for (int i = 0; i < fields.length; i++) {
61 fields[i] = new Widget[10];
62 for (int j = 0; j < fields[i].length; j++) {
63 LabelWidget w = new LabelWidget(scene);
64 w.setPreferredBounds(new Rectangle(i * WIDTH, j * HEIGHT, WIDTH, HEIGHT));
65 layerBoard.addChild(w);
69 for (int i = 0; i < fields.length; i++) {
70 for (int j = 0; j < fields[i].length; j++) {
72 ConnectionWidget horiz = new ConnectionWidget(scene);
73 horiz.setSourceAnchor(AnchorFactory.createCenterAnchor(fields[i - 1][j]));
74 horiz.setTargetAnchor(AnchorFactory.createCenterAnchor(fields[i][j]));
75 scene.addChild(horiz);
78 ConnectionWidget vert = new ConnectionWidget(scene);
79 vert.setSourceAnchor(AnchorFactory.createCenterAnchor(fields[i][j - 1]));
80 vert.setTargetAnchor(AnchorFactory.createCenterAnchor(fields[i][j]));
86 ImageWidget horizontalWall = new ImageWidget(scene);
87 horizontalWall.setPreferredBounds(new Rectangle(550, 300, 90, 10));
88 horizontalWall.setBackground(Color.BLACK);
89 horizontalWall.getActions().addAction(ActionFactory.createMoveAction(null, new Viewer(true)));
90 horizontalWall.setBorder(BorderFactory.createLineBorder());
91 horizontalWall.setOpaque(true);
92 layerBoard.addChild(horizontalWall);
94 ImageWidget verticalWall = new ImageWidget(scene);
95 verticalWall.setPreferredBounds(new Rectangle(600, 150, 10, 90));
96 verticalWall.setBackground(Color.BLACK);
97 verticalWall.getActions().addAction(ActionFactory.createMoveAction(null, new Viewer(false)));
98 verticalWall.setBorder(BorderFactory.createLineBorder());
99 verticalWall.setOpaque(true);
100 layerBoard.addChild(verticalWall);
102 for (Fence f : b.getFences()) {
103 Rectangle r = fenceRectangle(f.getColumn(), f.getRow(), f.getOrientation());
104 ImageWidget fenceWall = new ImageWidget(scene);
105 fenceWall.setPreferredBounds(r);
106 fenceWall.setBackground(Color.BLACK);
107 fenceWall.setBorder(BorderFactory.createLineBorder());
108 fenceWall.setOpaque(true);
109 layerBoard.addChild(fenceWall);
112 JFrame f = new JFrame();
113 f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
115 f.add(scene.createView());
120 private static Rectangle fenceRectangle(char column, int row, Fence.Orientation o) {
123 case HORIZONTAL: w = 45; h = 5; break;
124 case VERTICAL: w = 5; h = 45; break;
125 default: throw new IllegalStateException();
128 return new Rectangle(
129 (column - 'A' + 1) * 50 + 25 - w, row * 50 + 25 - h, 2 * w, 2 * h
133 final boolean horizontal;
137 private Viewer(boolean horizontal) {
138 this.horizontal = horizontal;
140 public void movementStarted(Widget widget) {
141 System.err.println("started: " + widget.getBounds());
144 public void movementFinished(Widget widget) {
145 System.err.println("finished: " + widget.getLocation());
148 public Point getOriginalLocation(Widget widget) {
149 orig = new Point(widget.getBounds().x, widget.getBounds().y);
150 System.err.println("orig: " + orig);
154 public void setNewLocation(Widget widget, Point location) {
155 widget.setPreferredBounds(new Rectangle(
156 round(location.x, true) + (horizontal ? 5 : 0),
157 round(location.y, false) + (horizontal ? 0 : 5),
158 horizontal ? 90 : 10,
159 horizontal ? 10 : 90)
163 int round(int p, boolean cmpHori) {
164 // p = horizontal ? orig.x + p: orig.y + p;
165 int onboard = p / 50;
166 System.err.println(cmpHori ? "hori: " + onboard : " vert: " + onboard);
173 int real = 25 + onboard * 50;
174 if (horizontal != cmpHori) {