1.1 --- a/visidor/src/main/java/cz/xelfi/quoridor/visidor/Viewer.java Thu Jan 07 22:34:17 2010 +0100
1.2 +++ b/visidor/src/main/java/cz/xelfi/quoridor/visidor/Viewer.java Sat Sep 04 21:58:56 2010 +0200
1.3 @@ -32,22 +32,12 @@
1.4 import cz.xelfi.quoridor.Move;
1.5 import cz.xelfi.quoridor.Player;
1.6 import java.awt.Color;
1.7 +import java.awt.Font;
1.8 +import java.awt.Graphics;
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 -import org.netbeans.api.visual.border.BorderFactory;
1.18 -import org.netbeans.api.visual.widget.ConnectionWidget;
1.19 -import org.netbeans.api.visual.widget.ImageWidget;
1.20 -import org.netbeans.api.visual.widget.LabelWidget;
1.21 -import org.netbeans.api.visual.widget.LayerWidget;
1.22 -import org.netbeans.api.visual.widget.Scene;
1.23 -import org.netbeans.api.visual.widget.Widget;
1.24 -import org.openide.util.Exceptions;
1.25
1.26 /**
1.27 *
1.28 @@ -55,7 +45,6 @@
1.29 */
1.30 final class Viewer extends JPanel {
1.31 private Board board;
1.32 - private final Scene scene;
1.33
1.34 public Viewer() {
1.35 this(Board.empty());
1.36 @@ -63,9 +52,6 @@
1.37
1.38 private Viewer(Board b) {
1.39 this.board = b;
1.40 - this.scene = new Scene();
1.41 - add(scene.createView());
1.42 - view(scene, board);
1.43 }
1.44
1.45
1.46 @@ -88,211 +74,77 @@
1.47 f.setVisible(true);
1.48 }
1.49
1.50 - private void view(Scene scene, Board b) {
1.51 - scene.removeChildren();
1.52 -// Scene layerBoard = scene;
1.53 - final LayerWidget layerLines = new LayerWidget(scene);
1.54 - scene.addChild(layerLines);
1.55 - final LayerWidget layerBoard = new LayerWidget(scene);
1.56 - layerLines.addChild(layerBoard);
1.57 - final int WDTH = 50;
1.58 - final int HGHT = 50;
1.59 -// conn.setSourceAnchor(ahoj);
1.60 - Widget[][] fields = new Widget[10][];
1.61 - for (int i = 0; i < fields.length; i++) {
1.62 - fields[i] = new Widget[10];
1.63 - for (int j = 0; j < fields[i].length; j++) {
1.64 - LabelWidget w = new LabelWidget(scene);
1.65 - w.setPreferredBounds(new Rectangle(i * WDTH, j * HGHT, WDTH, HGHT));
1.66 - layerBoard.addChild(w);
1.67 - fields[i][j] = w;
1.68 - }
1.69 - }
1.70 - for (int i = 0; i < fields.length; i++) {
1.71 - for (int j = 0; j < fields[i].length; j++) {
1.72 - if (i > 0) {
1.73 - ConnectionWidget horiz = new ConnectionWidget(scene);
1.74 - horiz.setSourceAnchor(AnchorFactory.createCenterAnchor(fields[i - 1][j]));
1.75 - horiz.setTargetAnchor(AnchorFactory.createCenterAnchor(fields[i][j]));
1.76 - scene.addChild(horiz);
1.77 - }
1.78 - if (j > 0) {
1.79 - ConnectionWidget vert = new ConnectionWidget(scene);
1.80 - vert.setSourceAnchor(AnchorFactory.createCenterAnchor(fields[i][j - 1]));
1.81 - vert.setTargetAnchor(AnchorFactory.createCenterAnchor(fields[i][j]));
1.82 - scene.addChild(vert);
1.83 - }
1.84 + @Override
1.85 + protected void paintComponent(Graphics gg) {
1.86 + int fieldSize = Math.min(getSize().width, getSize().height) / 9;
1.87 + int fifth = fieldSize / 10;
1.88 + Graphics2D g = (Graphics2D) gg;
1.89 +
1.90 + int xdelta = getSize().width - fieldSize * 9;
1.91 + int ydelta = getSize().height - fieldSize * 9;
1.92 +
1.93 + g.translate(xdelta / 2, ydelta / 2);
1.94 +
1.95 + g.setColor(Color.lightGray);
1.96 + for (int i = 0; i < 9; i++) {
1.97 + for (int j = 0; j < 9; j++) {
1.98 + final Rectangle r = new Rectangle(i * fieldSize, j * fieldSize, fieldSize, fieldSize);
1.99 + g.fillRect(r.x + fifth, r.y + fifth, r.width - fifth * 2, r.height - fifth * 2);
1.100 }
1.101 }
1.102
1.103 - ImageWidget horizontalWall = new ImageWidget(scene);
1.104 - horizontalWall.setPreferredBounds(new Rectangle(550, 300, 90, 10));
1.105 - horizontalWall.setBackground(Color.BLACK);
1.106 - horizontalWall.getActions().addAction(ActionFactory.createMoveAction(null, new MoveControl(true)));
1.107 - horizontalWall.setBorder(BorderFactory.createLineBorder());
1.108 - horizontalWall.setOpaque(true);
1.109 - layerBoard.addChild(horizontalWall);
1.110
1.111 - ImageWidget verticalWall = new ImageWidget(scene);
1.112 - verticalWall.setPreferredBounds(new Rectangle(600, 150, 10, 90));
1.113 - verticalWall.setBackground(Color.BLACK);
1.114 - verticalWall.getActions().addAction(ActionFactory.createMoveAction(null, new MoveControl(false)));
1.115 - verticalWall.setBorder(BorderFactory.createLineBorder());
1.116 - verticalWall.setOpaque(true);
1.117 - layerBoard.addChild(verticalWall);
1.118 -
1.119 - for (Fence f : b.getFences()) {
1.120 - Rectangle r = fenceRectangle(f.getColumn(), f.getRow(), f.getOrientation());
1.121 - ImageWidget fenceWall = new ImageWidget(scene);
1.122 - fenceWall.setPreferredBounds(r);
1.123 - fenceWall.setBackground(Color.BLACK);
1.124 - fenceWall.setBorder(BorderFactory.createLineBorder());
1.125 - fenceWall.setOpaque(true);
1.126 - layerBoard.addChild(fenceWall);
1.127 + g.setColor(Color.BLACK);
1.128 + for (Fence f : board.getFences()) {
1.129 + int w, h;
1.130 + switch (f.getOrientation()) {
1.131 + case HORIZONTAL: w = fieldSize - fifth; h = fifth; break;
1.132 + case VERTICAL: w = fifth; h = fieldSize - fifth; break;
1.133 + default: throw new IllegalStateException();
1.134 + }
1.135 + int column = (f.getColumn() - 'A') + 1;
1.136 + int row = 9 - f.getRow();
1.137 + Rectangle r = new Rectangle(
1.138 + column * fieldSize - w,
1.139 + row * fieldSize - h,
1.140 + 2 * w,
1.141 + 2 * h
1.142 + );
1.143 + g.fill(r);
1.144 }
1.145
1.146 int cnt = 0;
1.147 - for (Player p : b.getPlayers()) {
1.148 - LabelWidget lw = new LabelWidget(scene);
1.149 - lw.setLabel("" + p.getFences());
1.150 - if (cnt == 0) {
1.151 - lw.setPreferredBounds(new Rectangle(550, 200, 50, 50));
1.152 - } else {
1.153 - lw.setPreferredBounds(new Rectangle(550, 450, 50, 50));
1.154 + Color[] colors = { Color.WHITE, Color.BLACK, Color.ORANGE, Color.MAGENTA };
1.155 + int diametr = fieldSize - fifth * 4;
1.156 + for (Player p : board.getPlayers()) {
1.157 + int column = p.getColumn();
1.158 + int row = 8 - p.getRow();
1.159 + final Rectangle r = new Rectangle(
1.160 + column * fieldSize + 2 * fifth,
1.161 + row * fieldSize + 2 * fifth,
1.162 + diametr,
1.163 + diametr
1.164 + );
1.165 + g.setColor(colors[cnt]);
1.166 + g.fillOval(r.x, r.y, r.width, r.height);
1.167 + if (p == board.getCurrentPlayer()) {
1.168 + g.setColor(Color.lightGray);
1.169 + g.fillOval(r.x + r.width / 3, r.y + r.height / 3, r.width / 3, r.height / 3);
1.170 }
1.171 - layerBoard.addChild(lw);
1.172 + cnt++;
1.173 + }
1.174
1.175 - PlayerWidget pw = new PlayerWidget(scene);
1.176 - System.err.println("p: " + p);
1.177 - pw.setPreferredBounds(new Rectangle(p.getColumn() * 50 + 25, p.getRow() * 50 + 25, 50, 50));
1.178 - layerBoard.addChild(pw);
1.179 -
1.180 - cnt++;
1.181 + g.setColor(Color.BLACK);
1.182 + final Font f = new Font("Monospace", Font.BOLD, fifth * 2);
1.183 + g.setFont(f);
1.184 + for (int i = 0; i < 8; i++) {
1.185 + final char ch = (char) ('A' + i);
1.186 + g.drawString(Character.toString(ch), i * fieldSize + fieldSize - fifth, fifth + f.getSize() - f.getBaselineFor(ch));
1.187 + }
1.188 + for (int i = 0; i < 8; i++) {
1.189 + String s = "" + (8 - i);
1.190 + g.drawString(s, fifth, i * fieldSize + fieldSize - fifth + f.getSize() - f.getBaselineFor(s.charAt(0)));
1.191 }
1.192 }
1.193
1.194 - private static Rectangle fenceRectangle(char column, int row, Fence.Orientation o) {
1.195 - int w, h;
1.196 - switch (o) {
1.197 - case HORIZONTAL: w = 45; h = 5; break;
1.198 - case VERTICAL: w = 5; h = 45; break;
1.199 - default: throw new IllegalStateException();
1.200 - }
1.201 -
1.202 - return new Rectangle(
1.203 - (column - 'A' + 1) * 50 + 25 - w, row * 50 + 25 - h, 2 * w, 2 * h
1.204 - );
1.205 - }
1.206 -
1.207 - private class MoveControl implements MoveProvider {
1.208 - final boolean horizontal;
1.209 -
1.210 - Point orig;
1.211 -
1.212 - private MoveControl(boolean horizontal) {
1.213 - this.horizontal = horizontal;
1.214 - }
1.215 - public void movementStarted(Widget widget) {
1.216 - System.err.println("started: " + widget.getBounds());
1.217 - }
1.218 -
1.219 - public void movementFinished(Widget widget) {
1.220 - try {
1.221 - final Rectangle bounds = widget.getBounds();
1.222 - final Move m = createMoveForDrop( bounds);
1.223 - System.err.println("finish: " + m);
1.224 - board = board.apply(m);
1.225 - } catch (IllegalPositionException ex) {
1.226 - Exceptions.printStackTrace(ex);
1.227 - }
1.228 -
1.229 - view(scene, board);
1.230 - }
1.231 -
1.232 - public Point getOriginalLocation(Widget widget) {
1.233 - final Rectangle b = widget.getBounds();
1.234 - orig = new Point(b.x, b.y);
1.235 - try {
1.236 - System.err.println(" where: " + createMoveForDrop(b));
1.237 - } catch (IllegalPositionException ex) {
1.238 - Exceptions.printStackTrace(ex);
1.239 - }
1.240 - return orig;
1.241 - }
1.242 -
1.243 - public void setNewLocation(Widget widget, Point location) {
1.244 - final Rectangle b = new Rectangle(
1.245 - round(location.x, true) + (horizontal ? 5 : 0),
1.246 - round(location.y, false) + (horizontal ? 0 : 5),
1.247 - horizontal ? 90 : 10,
1.248 - horizontal ? 10 : 90
1.249 - );
1.250 - widget.setPreferredBounds(b);
1.251 - try {
1.252 - System.err.println("move: " + createMoveForDrop(b));
1.253 - } catch (IllegalPositionException ex) {
1.254 - System.err.println("no move");
1.255 - }
1.256 - }
1.257 -
1.258 - Move createMoveForDrop(final Rectangle bounds) throws IllegalPositionException {
1.259 - int column = bounds.x / 50;
1.260 - int row = bounds.y / 50;
1.261 - Orientation o = bounds.width > bounds.height ? Orientation.HORIZONTAL : Orientation.VERTICAL;
1.262 - switch (o) {
1.263 - case HORIZONTAL: row--; break;
1.264 - case VERTICAL: column--; break;
1.265 - default: assert false;
1.266 - }
1.267 - if (column < 0) {
1.268 - column = 0;
1.269 - }
1.270 - if (column >= 9) {
1.271 - column = 8;
1.272 - }
1.273 - if (row < 0) {
1.274 - row = 0;
1.275 - }
1.276 - if (row > 8) {
1.277 - row = 8;
1.278 - }
1.279 - Move m = Move.fence((char) ('A' + column), 1 + row, o);
1.280 - return m;
1.281 - }
1.282 -
1.283 - int round(int p, boolean cmpHori) {
1.284 - // p = horizontal ? orig.x + p: orig.y + p;
1.285 - int onboard = p / 50;
1.286 - if (onboard < 0) {
1.287 - onboard = 0;
1.288 - }
1.289 - if (onboard >= 9) {
1.290 - return p;
1.291 - }
1.292 - int real = 25 + onboard * 50;
1.293 - if (horizontal != cmpHori) {
1.294 - return real - 5;
1.295 - } else {
1.296 - return real;
1.297 - }
1.298 - }
1.299 - }
1.300 -
1.301 - private static final class PlayerWidget extends Widget {
1.302 - public PlayerWidget(Scene s) {
1.303 - super(s);
1.304 - }
1.305 -
1.306 - @Override
1.307 - protected Rectangle calculateClientArea() {
1.308 - return getBounds();
1.309 - }
1.310 -
1.311 - @Override
1.312 - protected void paintWidget() {
1.313 - Graphics2D g = getGraphics();
1.314 - Rectangle b = getBounds();
1.315 - g.drawOval(b.x + 5, b.y + 5, b.width - 10, b.height - 10);
1.316 - }
1.317 - }
1.318 }