1.1 --- a/visidor/src/main/java/cz/xelfi/quoridor/visidor/Viewer.java Mon May 25 09:07:15 2009 +0200
1.2 +++ b/visidor/src/main/java/cz/xelfi/quoridor/visidor/Viewer.java Sat Jun 06 11:55:18 2009 +0200
1.3 @@ -10,10 +10,13 @@
1.4 import cz.xelfi.quoridor.Fence.Orientation;
1.5 import cz.xelfi.quoridor.IllegalPositionException;
1.6 import cz.xelfi.quoridor.Move;
1.7 +import cz.xelfi.quoridor.Player;
1.8 import java.awt.Color;
1.9 +import java.awt.Graphics2D;
1.10 import java.awt.Point;
1.11 import java.awt.Rectangle;
1.12 import javax.swing.JFrame;
1.13 +import javax.swing.JPanel;
1.14 import org.netbeans.api.visual.action.ActionFactory;
1.15 import org.netbeans.api.visual.action.MoveProvider;
1.16 import org.netbeans.api.visual.anchor.AnchorFactory;
1.17 @@ -24,12 +27,27 @@
1.18 import org.netbeans.api.visual.widget.LayerWidget;
1.19 import org.netbeans.api.visual.widget.Scene;
1.20 import org.netbeans.api.visual.widget.Widget;
1.21 +import org.openide.util.Exceptions;
1.22
1.23 /**
1.24 *
1.25 * @author Jaroslav Tulach <jtulach@netbeans.org>
1.26 */
1.27 -public class Viewer implements MoveProvider {
1.28 +final class Viewer extends JPanel {
1.29 + private Board board;
1.30 + private final Scene scene;
1.31 +
1.32 + public Viewer() {
1.33 + this(Board.empty());
1.34 + }
1.35 +
1.36 + private Viewer(Board b) {
1.37 + this.board = b;
1.38 + this.scene = new Scene();
1.39 + add(scene.createView());
1.40 + view(scene, board);
1.41 + }
1.42 +
1.43
1.44 /**
1.45 * @param args the command line arguments
1.46 @@ -42,26 +60,30 @@
1.47 for (int i = 1; i <= 8; i++) {
1.48 b = b.apply(Move.fence('H', i, Orientation.values()[(i + 1) % 2]));
1.49 }
1.50 - //b = b.apply(Move.fence('H', 5, Orientation.HORIZONTAL));
1.51 - view(b);
1.52 +
1.53 + JFrame f = new JFrame();
1.54 + f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
1.55 + f.add(new Viewer(b));
1.56 + f.pack();
1.57 + f.setVisible(true);
1.58 }
1.59
1.60 - private static void view(Board b) {
1.61 - Scene scene = new Scene();
1.62 + private void view(Scene scene, Board b) {
1.63 + scene.removeChildren();
1.64 // Scene layerBoard = scene;
1.65 final LayerWidget layerLines = new LayerWidget(scene);
1.66 scene.addChild(layerLines);
1.67 final LayerWidget layerBoard = new LayerWidget(scene);
1.68 layerLines.addChild(layerBoard);
1.69 - final int WIDTH = 50;
1.70 - final int HEIGHT = 50;
1.71 + final int WDTH = 50;
1.72 + final int HGHT = 50;
1.73 // conn.setSourceAnchor(ahoj);
1.74 Widget[][] fields = new Widget[10][];
1.75 for (int i = 0; i < fields.length; i++) {
1.76 fields[i] = new Widget[10];
1.77 for (int j = 0; j < fields[i].length; j++) {
1.78 LabelWidget w = new LabelWidget(scene);
1.79 - w.setPreferredBounds(new Rectangle(i * WIDTH, j * HEIGHT, WIDTH, HEIGHT));
1.80 + w.setPreferredBounds(new Rectangle(i * WDTH, j * HGHT, WDTH, HGHT));
1.81 layerBoard.addChild(w);
1.82 fields[i][j] = w;
1.83 }
1.84 @@ -86,7 +108,7 @@
1.85 ImageWidget horizontalWall = new ImageWidget(scene);
1.86 horizontalWall.setPreferredBounds(new Rectangle(550, 300, 90, 10));
1.87 horizontalWall.setBackground(Color.BLACK);
1.88 - horizontalWall.getActions().addAction(ActionFactory.createMoveAction(null, new Viewer(true)));
1.89 + horizontalWall.getActions().addAction(ActionFactory.createMoveAction(null, new MoveControl(true)));
1.90 horizontalWall.setBorder(BorderFactory.createLineBorder());
1.91 horizontalWall.setOpaque(true);
1.92 layerBoard.addChild(horizontalWall);
1.93 @@ -94,7 +116,7 @@
1.94 ImageWidget verticalWall = new ImageWidget(scene);
1.95 verticalWall.setPreferredBounds(new Rectangle(600, 150, 10, 90));
1.96 verticalWall.setBackground(Color.BLACK);
1.97 - verticalWall.getActions().addAction(ActionFactory.createMoveAction(null, new Viewer(false)));
1.98 + verticalWall.getActions().addAction(ActionFactory.createMoveAction(null, new MoveControl(false)));
1.99 verticalWall.setBorder(BorderFactory.createLineBorder());
1.100 verticalWall.setOpaque(true);
1.101 layerBoard.addChild(verticalWall);
1.102 @@ -109,12 +131,24 @@
1.103 layerBoard.addChild(fenceWall);
1.104 }
1.105
1.106 - JFrame f = new JFrame();
1.107 - f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
1.108 -
1.109 - f.add(scene.createView());
1.110 - f.pack();
1.111 - f.setVisible(true);
1.112 + int cnt = 0;
1.113 + for (Player p : b.getPlayers()) {
1.114 + LabelWidget lw = new LabelWidget(scene);
1.115 + lw.setLabel("" + p.getFences());
1.116 + if (cnt == 0) {
1.117 + lw.setPreferredBounds(new Rectangle(550, 200, 50, 50));
1.118 + } else {
1.119 + lw.setPreferredBounds(new Rectangle(550, 450, 50, 50));
1.120 + }
1.121 + layerBoard.addChild(lw);
1.122 +
1.123 + PlayerWidget pw = new PlayerWidget(scene);
1.124 + System.err.println("p: " + p);
1.125 + pw.setPreferredBounds(new Rectangle(p.getColumn() * 50, p.getRow() * 50, 50, 50));
1.126 + layerBoard.addChild(pw);
1.127 +
1.128 + cnt++;
1.129 + }
1.130 }
1.131
1.132 private static Rectangle fenceRectangle(char column, int row, Fence.Orientation o) {
1.133 @@ -130,51 +164,92 @@
1.134 );
1.135 }
1.136
1.137 - final boolean horizontal;
1.138 + private class MoveControl implements MoveProvider {
1.139 + final boolean horizontal;
1.140
1.141 - Point orig;
1.142 + Point orig;
1.143
1.144 - private Viewer(boolean horizontal) {
1.145 - this.horizontal = horizontal;
1.146 - }
1.147 - public void movementStarted(Widget widget) {
1.148 - System.err.println("started: " + widget.getBounds());
1.149 + private MoveControl(boolean horizontal) {
1.150 + this.horizontal = horizontal;
1.151 + }
1.152 + public void movementStarted(Widget widget) {
1.153 + System.err.println("started: " + widget.getBounds());
1.154 + }
1.155 +
1.156 + public void movementFinished(Widget widget) {
1.157 + int column = widget.getBounds().x / 50 - 1;
1.158 + if (column < 0) {
1.159 + column = 0;
1.160 + }
1.161 + if (column >= 9) {
1.162 + column = 8;
1.163 + }
1.164 + int row = widget.getBounds().y / 50 - 1;
1.165 + if (row < 0) {
1.166 + row = 0;
1.167 + }
1.168 + if (row >= 8) {
1.169 + row = 7;
1.170 + }
1.171 +
1.172 + Orientation o = widget.getBounds().width > widget.getBounds().height ? Orientation.HORIZONTAL : Orientation.VERTICAL;
1.173 + try {
1.174 + board = board.apply(Move.fence((char) ('A' + column), 1 + row, o));
1.175 + } catch (IllegalPositionException ex) {
1.176 + Exceptions.printStackTrace(ex);
1.177 + }
1.178 +
1.179 + view(scene, board);
1.180 + }
1.181 +
1.182 + public Point getOriginalLocation(Widget widget) {
1.183 + orig = new Point(widget.getBounds().x, widget.getBounds().y);
1.184 + System.err.println("orig: " + orig);
1.185 + return orig;
1.186 + }
1.187 +
1.188 + public void setNewLocation(Widget widget, Point location) {
1.189 + widget.setPreferredBounds(new Rectangle(
1.190 + round(location.x, true) + (horizontal ? 5 : 0),
1.191 + round(location.y, false) + (horizontal ? 0 : 5),
1.192 + horizontal ? 90 : 10,
1.193 + horizontal ? 10 : 90)
1.194 + );
1.195 + }
1.196 +
1.197 + int round(int p, boolean cmpHori) {
1.198 + // p = horizontal ? orig.x + p: orig.y + p;
1.199 + int onboard = p / 50;
1.200 + System.err.println(cmpHori ? "hori: " + onboard : " vert: " + onboard);
1.201 + if (onboard < 0) {
1.202 + onboard = 0;
1.203 + }
1.204 + if (onboard >= 9) {
1.205 + return p;
1.206 + }
1.207 + int real = 25 + onboard * 50;
1.208 + if (horizontal != cmpHori) {
1.209 + return real - 5;
1.210 + } else {
1.211 + return real;
1.212 + }
1.213 + }
1.214 }
1.215
1.216 - public void movementFinished(Widget widget) {
1.217 - System.err.println("finished: " + widget.getLocation());
1.218 - }
1.219 + private static final class PlayerWidget extends Widget {
1.220 + public PlayerWidget(Scene s) {
1.221 + super(s);
1.222 + }
1.223
1.224 - public Point getOriginalLocation(Widget widget) {
1.225 - orig = new Point(widget.getBounds().x, widget.getBounds().y);
1.226 - System.err.println("orig: " + orig);
1.227 - return orig;
1.228 - }
1.229 + @Override
1.230 + protected Rectangle calculateClientArea() {
1.231 + return new Rectangle(0, 0, 50, 50);
1.232 + }
1.233
1.234 - public void setNewLocation(Widget widget, Point location) {
1.235 - widget.setPreferredBounds(new Rectangle(
1.236 - round(location.x, true) + (horizontal ? 5 : 0),
1.237 - round(location.y, false) + (horizontal ? 0 : 5),
1.238 - horizontal ? 90 : 10,
1.239 - horizontal ? 10 : 90)
1.240 - );
1.241 - }
1.242 -
1.243 - int round(int p, boolean cmpHori) {
1.244 -// p = horizontal ? orig.x + p: orig.y + p;
1.245 - int onboard = p / 50;
1.246 - System.err.println(cmpHori ? "hori: " + onboard : " vert: " + onboard);
1.247 - if (onboard < 0) {
1.248 - onboard = 0;
1.249 - }
1.250 - if (onboard >= 9) {
1.251 - return p;
1.252 - }
1.253 - int real = 25 + onboard * 50;
1.254 - if (horizontal != cmpHori) {
1.255 - return real - 5;
1.256 - } else {
1.257 - return real;
1.258 + @Override
1.259 + protected void paintWidget() {
1.260 + Graphics2D g = getGraphics();
1.261 + g.drawOval(0, 0, 50, 50);
1.262 }
1.263 }
1.264 }