Fences can be dropped to the board
authorJaroslav Tulach <jtulach@netbeans.org>
Sat, 06 Jun 2009 11:55:18 +0200
changeset 29e5e40fd406c1
parent 28 77f6d8bf0d6c
child 30 f25846453907
Fences can be dropped to the board
visidor/nbactions.xml
visidor/src/main/java/cz/xelfi/quoridor/visidor/Viewer.java
visidor/src/test/java/cz/xelfi/quoridor/visidor/ViewerTest.java
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/visidor/nbactions.xml	Sat Jun 06 11:55:18 2009 +0200
     1.3 @@ -0,0 +1,14 @@
     1.4 +<?xml version="1.0" encoding="UTF-8"?>
     1.5 +<actions>
     1.6 +        <action>
     1.7 +            <actionName>run</actionName>
     1.8 +            <goals>
     1.9 +                <goal>process-classes</goal>
    1.10 +                <goal>org.codehaus.mojo:exec-maven-plugin:1.1.1:exec</goal>
    1.11 +            </goals>
    1.12 +            <properties>
    1.13 +                <exec.args>-classpath %classpath cz.xelfi.quoridor.visidor.Viewer</exec.args>
    1.14 +                <exec.executable>java</exec.executable>
    1.15 +            </properties>
    1.16 +        </action>
    1.17 +    </actions>
     2.1 --- a/visidor/src/main/java/cz/xelfi/quoridor/visidor/Viewer.java	Sat May 30 14:54:31 2009 +0200
     2.2 +++ b/visidor/src/main/java/cz/xelfi/quoridor/visidor/Viewer.java	Sat Jun 06 11:55:18 2009 +0200
     2.3 @@ -10,10 +10,13 @@
     2.4  import cz.xelfi.quoridor.Fence.Orientation;
     2.5  import cz.xelfi.quoridor.IllegalPositionException;
     2.6  import cz.xelfi.quoridor.Move;
     2.7 +import cz.xelfi.quoridor.Player;
     2.8  import java.awt.Color;
     2.9 +import java.awt.Graphics2D;
    2.10  import java.awt.Point;
    2.11  import java.awt.Rectangle;
    2.12  import javax.swing.JFrame;
    2.13 +import javax.swing.JPanel;
    2.14  import org.netbeans.api.visual.action.ActionFactory;
    2.15  import org.netbeans.api.visual.action.MoveProvider;
    2.16  import org.netbeans.api.visual.anchor.AnchorFactory;
    2.17 @@ -24,12 +27,27 @@
    2.18  import org.netbeans.api.visual.widget.LayerWidget;
    2.19  import org.netbeans.api.visual.widget.Scene;
    2.20  import org.netbeans.api.visual.widget.Widget;
    2.21 +import org.openide.util.Exceptions;
    2.22  
    2.23  /**
    2.24   *
    2.25   * @author Jaroslav Tulach <jtulach@netbeans.org>
    2.26   */
    2.27 -public class Viewer implements MoveProvider {
    2.28 +final class Viewer extends JPanel {
    2.29 +    private Board board;
    2.30 +    private final Scene scene;
    2.31 +
    2.32 +    public Viewer() {
    2.33 +        this(Board.empty());
    2.34 +    }
    2.35 +
    2.36 +    private Viewer(Board b) {
    2.37 +        this.board = b;
    2.38 +        this.scene = new Scene();
    2.39 +        add(scene.createView());
    2.40 +        view(scene, board);
    2.41 +    }
    2.42 +
    2.43  
    2.44      /**
    2.45       * @param args the command line arguments
    2.46 @@ -42,26 +60,30 @@
    2.47          for (int i = 1; i <= 8; i++) {
    2.48              b = b.apply(Move.fence('H', i, Orientation.values()[(i + 1) % 2]));
    2.49          }
    2.50 -        //b = b.apply(Move.fence('H', 5, Orientation.HORIZONTAL));
    2.51 -        view(b);
    2.52 +
    2.53 +        JFrame f = new JFrame();
    2.54 +        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    2.55 +        f.add(new Viewer(b));
    2.56 +        f.pack();
    2.57 +        f.setVisible(true);
    2.58      }
    2.59  
    2.60 -    private static void view(Board b) {
    2.61 -        Scene scene = new Scene();
    2.62 +    private void view(Scene scene, Board b) {
    2.63 +        scene.removeChildren();
    2.64  //        Scene layerBoard = scene;
    2.65          final LayerWidget layerLines = new LayerWidget(scene);
    2.66          scene.addChild(layerLines);
    2.67          final LayerWidget layerBoard = new LayerWidget(scene);
    2.68          layerLines.addChild(layerBoard);
    2.69 -        final int WIDTH = 50;
    2.70 -        final int HEIGHT = 50;
    2.71 +        final int WDTH = 50;
    2.72 +        final int HGHT = 50;
    2.73  //        conn.setSourceAnchor(ahoj);
    2.74          Widget[][] fields = new Widget[10][];
    2.75          for (int i = 0; i < fields.length; i++) {
    2.76              fields[i] = new Widget[10];
    2.77              for (int j = 0; j < fields[i].length; j++) {
    2.78                  LabelWidget w = new LabelWidget(scene);
    2.79 -                w.setPreferredBounds(new Rectangle(i * WIDTH, j * HEIGHT, WIDTH, HEIGHT));
    2.80 +                w.setPreferredBounds(new Rectangle(i * WDTH, j * HGHT, WDTH, HGHT));
    2.81                  layerBoard.addChild(w);
    2.82                  fields[i][j] = w;
    2.83              }
    2.84 @@ -86,7 +108,7 @@
    2.85          ImageWidget horizontalWall = new ImageWidget(scene);
    2.86          horizontalWall.setPreferredBounds(new Rectangle(550, 300, 90, 10));
    2.87          horizontalWall.setBackground(Color.BLACK);
    2.88 -        horizontalWall.getActions().addAction(ActionFactory.createMoveAction(null, new Viewer(true)));
    2.89 +        horizontalWall.getActions().addAction(ActionFactory.createMoveAction(null, new MoveControl(true)));
    2.90          horizontalWall.setBorder(BorderFactory.createLineBorder());
    2.91          horizontalWall.setOpaque(true);
    2.92          layerBoard.addChild(horizontalWall);
    2.93 @@ -94,7 +116,7 @@
    2.94          ImageWidget verticalWall = new ImageWidget(scene);
    2.95          verticalWall.setPreferredBounds(new Rectangle(600, 150, 10, 90));
    2.96          verticalWall.setBackground(Color.BLACK);
    2.97 -        verticalWall.getActions().addAction(ActionFactory.createMoveAction(null, new Viewer(false)));
    2.98 +        verticalWall.getActions().addAction(ActionFactory.createMoveAction(null, new MoveControl(false)));
    2.99          verticalWall.setBorder(BorderFactory.createLineBorder());
   2.100          verticalWall.setOpaque(true);
   2.101          layerBoard.addChild(verticalWall);
   2.102 @@ -109,12 +131,24 @@
   2.103              layerBoard.addChild(fenceWall);
   2.104          }
   2.105  
   2.106 -        JFrame f = new JFrame();
   2.107 -        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
   2.108 -        
   2.109 -        f.add(scene.createView());
   2.110 -        f.pack();
   2.111 -        f.setVisible(true);
   2.112 +        int cnt = 0;
   2.113 +        for (Player p : b.getPlayers()) {
   2.114 +            LabelWidget lw = new LabelWidget(scene);
   2.115 +            lw.setLabel("" + p.getFences());
   2.116 +            if (cnt == 0) {
   2.117 +                lw.setPreferredBounds(new Rectangle(550, 200, 50, 50));
   2.118 +            } else {
   2.119 +                lw.setPreferredBounds(new Rectangle(550, 450, 50, 50));
   2.120 +            }
   2.121 +            layerBoard.addChild(lw);
   2.122 +
   2.123 +            PlayerWidget pw = new PlayerWidget(scene);
   2.124 +            System.err.println("p: " + p);
   2.125 +            pw.setPreferredBounds(new Rectangle(p.getColumn() * 50, p.getRow() * 50, 50, 50));
   2.126 +            layerBoard.addChild(pw);
   2.127 +
   2.128 +            cnt++;
   2.129 +        }
   2.130      }
   2.131  
   2.132      private static Rectangle fenceRectangle(char column, int row, Fence.Orientation o) {
   2.133 @@ -130,51 +164,92 @@
   2.134          );
   2.135      }
   2.136  
   2.137 -    final boolean horizontal;
   2.138 +    private class MoveControl implements MoveProvider {
   2.139 +        final boolean horizontal;
   2.140  
   2.141 -    Point orig;
   2.142 +        Point orig;
   2.143  
   2.144 -    private Viewer(boolean horizontal) {
   2.145 -        this.horizontal = horizontal;
   2.146 -    }
   2.147 -    public void movementStarted(Widget widget) {
   2.148 -        System.err.println("started: " + widget.getBounds());
   2.149 +        private MoveControl(boolean horizontal) {
   2.150 +            this.horizontal = horizontal;
   2.151 +        }
   2.152 +        public void movementStarted(Widget widget) {
   2.153 +            System.err.println("started: " + widget.getBounds());
   2.154 +        }
   2.155 +
   2.156 +        public void movementFinished(Widget widget) {
   2.157 +            int column = widget.getBounds().x / 50 - 1;
   2.158 +            if (column < 0) {
   2.159 +                column = 0;
   2.160 +            }
   2.161 +            if (column >= 9) {
   2.162 +                column = 8;
   2.163 +            }
   2.164 +            int row = widget.getBounds().y / 50 - 1;
   2.165 +            if (row < 0) {
   2.166 +                row = 0;
   2.167 +            }
   2.168 +            if (row >= 8) {
   2.169 +                row = 7;
   2.170 +            }
   2.171 +
   2.172 +            Orientation o = widget.getBounds().width > widget.getBounds().height ? Orientation.HORIZONTAL : Orientation.VERTICAL;
   2.173 +            try {
   2.174 +                board = board.apply(Move.fence((char) ('A' + column), 1 + row, o));
   2.175 +            } catch (IllegalPositionException ex) {
   2.176 +                Exceptions.printStackTrace(ex);
   2.177 +            }
   2.178 +
   2.179 +            view(scene, board);
   2.180 +        }
   2.181 +
   2.182 +        public Point getOriginalLocation(Widget widget) {
   2.183 +            orig = new Point(widget.getBounds().x, widget.getBounds().y);
   2.184 +            System.err.println("orig: " + orig);
   2.185 +            return orig;
   2.186 +        }
   2.187 +
   2.188 +        public void setNewLocation(Widget widget, Point location) {
   2.189 +            widget.setPreferredBounds(new Rectangle(
   2.190 +                round(location.x, true) + (horizontal ? 5 : 0),
   2.191 +                round(location.y, false) + (horizontal ? 0 : 5),
   2.192 +                horizontal ? 90 : 10,
   2.193 +                horizontal ? 10 : 90)
   2.194 +            );
   2.195 +        }
   2.196 +
   2.197 +        int round(int p, boolean cmpHori) {
   2.198 +    //        p = horizontal ? orig.x + p: orig.y + p;
   2.199 +            int onboard = p / 50;
   2.200 +            System.err.println(cmpHori ? "hori: " + onboard : "  vert: " + onboard);
   2.201 +            if (onboard < 0) {
   2.202 +                onboard = 0;
   2.203 +            }
   2.204 +            if (onboard >= 9) {
   2.205 +                return p;
   2.206 +            }
   2.207 +            int real = 25 + onboard * 50;
   2.208 +            if (horizontal != cmpHori) {
   2.209 +                return real - 5;
   2.210 +            } else {
   2.211 +                return real;
   2.212 +            }
   2.213 +        }
   2.214      }
   2.215  
   2.216 -    public void movementFinished(Widget widget) {
   2.217 -        System.err.println("finished: " + widget.getLocation());
   2.218 -    }
   2.219 +    private static final class PlayerWidget extends Widget {
   2.220 +        public PlayerWidget(Scene s) {
   2.221 +            super(s);
   2.222 +        }
   2.223  
   2.224 -    public Point getOriginalLocation(Widget widget) {
   2.225 -        orig = new Point(widget.getBounds().x, widget.getBounds().y);
   2.226 -        System.err.println("orig: " + orig);
   2.227 -        return orig;
   2.228 -    }
   2.229 +        @Override
   2.230 +        protected Rectangle calculateClientArea() {
   2.231 +            return new Rectangle(0, 0, 50, 50);
   2.232 +        }
   2.233  
   2.234 -    public void setNewLocation(Widget widget, Point location) {
   2.235 -        widget.setPreferredBounds(new Rectangle(
   2.236 -            round(location.x, true) + (horizontal ? 5 : 0),
   2.237 -            round(location.y, false) + (horizontal ? 0 : 5),
   2.238 -            horizontal ? 90 : 10,
   2.239 -            horizontal ? 10 : 90)
   2.240 -        );
   2.241 -    }
   2.242 -
   2.243 -    int round(int p, boolean cmpHori) {
   2.244 -//        p = horizontal ? orig.x + p: orig.y + p;
   2.245 -        int onboard = p / 50;
   2.246 -        System.err.println(cmpHori ? "hori: " + onboard : "  vert: " + onboard);
   2.247 -        if (onboard < 0) {
   2.248 -            onboard = 0;
   2.249 -        }
   2.250 -        if (onboard >= 9) {
   2.251 -            return p;
   2.252 -        }
   2.253 -        int real = 25 + onboard * 50;
   2.254 -        if (horizontal != cmpHori) {
   2.255 -            return real - 5;
   2.256 -        } else {
   2.257 -            return real;
   2.258 +        @Override
   2.259 +        protected void paintWidget() {
   2.260 +            Graphics2D g = getGraphics();
   2.261 +            g.drawOval(0, 0, 50, 50);
   2.262          }
   2.263      }
   2.264  }
     3.1 --- a/visidor/src/test/java/cz/xelfi/quoridor/visidor/ViewerTest.java	Sat May 30 14:54:31 2009 +0200
     3.2 +++ b/visidor/src/test/java/cz/xelfi/quoridor/visidor/ViewerTest.java	Sat Jun 06 11:55:18 2009 +0200
     3.3 @@ -1,4 +1,4 @@
     3.4 -package org.apidesign.quoridor.visidor;
     3.5 +package cz.xelfi.quoridor.visidor;
     3.6  
     3.7  import junit.framework.Test;
     3.8  import junit.framework.TestCase;
     3.9 @@ -7,32 +7,13 @@
    3.10  /**
    3.11   * Unit test for simple App.
    3.12   */
    3.13 -public class AppTest 
    3.14 -    extends TestCase
    3.15 -{
    3.16 -    /**
    3.17 -     * Create the test case
    3.18 -     *
    3.19 -     * @param testName name of the test case
    3.20 -     */
    3.21 -    public AppTest( String testName )
    3.22 -    {
    3.23 -        super( testName );
    3.24 +public class ViewerTest extends TestCase {
    3.25 +
    3.26 +    public ViewerTest(String testName) {
    3.27 +        super(testName);
    3.28      }
    3.29  
    3.30 -    /**
    3.31 -     * @return the suite of tests being tested
    3.32 -     */
    3.33 -    public static Test suite()
    3.34 -    {
    3.35 -        return new TestSuite( AppTest.class );
    3.36 -    }
    3.37 -
    3.38 -    /**
    3.39 -     * Rigourous Test :-)
    3.40 -     */
    3.41 -    public void testApp()
    3.42 -    {
    3.43 -        assertTrue( true );
    3.44 +    public static Test suite() {
    3.45 +        return new TestSuite(ViewerTest.class);
    3.46      }
    3.47  }