visidor/src/main/java/cz/xelfi/quoridor/visidor/Viewer.java
author Jaroslav Tulach <jtulach@netbeans.org>
Mon, 25 May 2009 09:07:15 +0200
changeset 25 f27d4c4cd464
parent 24 visidor/src/visidor/Viewer.java@fdd6c390fa5f
child 29 e5e40fd406c1
permissions -rw-r--r--
Converting the visidor to maven project
     1 /*
     2  * To change this template, choose Tools | Templates
     3  * and open the template in the editor.
     4  */
     5 
     6 package cz.xelfi.quoridor.visidor;
     7 
     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;
    27 
    28 /**
    29  *
    30  * @author Jaroslav Tulach <jtulach@netbeans.org>
    31  */
    32 public class Viewer implements MoveProvider {
    33 
    34     /**
    35      * @param args the command line arguments
    36      */
    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]));
    41         }
    42         for (int i = 1; i <= 8; i++) {
    43             b = b.apply(Move.fence('H', i, Orientation.values()[(i + 1) % 2]));
    44         }
    45         //b = b.apply(Move.fence('H', 5, Orientation.HORIZONTAL));
    46         view(b);
    47     }
    48 
    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);
    56         final int WIDTH = 50;
    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);
    66                 fields[i][j] = w;
    67             }
    68         }
    69         for (int i = 0; i < fields.length; i++) {
    70             for (int j = 0; j < fields[i].length; j++) {
    71                 if (i > 0) {
    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);
    76                 }
    77                 if (j > 0) {
    78                     ConnectionWidget vert = new ConnectionWidget(scene);
    79                     vert.setSourceAnchor(AnchorFactory.createCenterAnchor(fields[i][j - 1]));
    80                     vert.setTargetAnchor(AnchorFactory.createCenterAnchor(fields[i][j]));
    81                     scene.addChild(vert);
    82                 }
    83             }
    84         }
    85 
    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);
    93 
    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);
   101 
   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);
   110         }
   111 
   112         JFrame f = new JFrame();
   113         f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
   114         
   115         f.add(scene.createView());
   116         f.pack();
   117         f.setVisible(true);
   118     }
   119 
   120     private static Rectangle fenceRectangle(char column, int row, Fence.Orientation o) {
   121         int w, h;
   122         switch (o) {
   123             case HORIZONTAL: w = 45; h = 5; break;
   124             case VERTICAL: w = 5; h = 45; break;
   125             default: throw new IllegalStateException();
   126         }
   127 
   128         return new Rectangle(
   129             (column - 'A' + 1) * 50 + 25 - w, row * 50 + 25 - h, 2 * w, 2 * h
   130         );
   131     }
   132 
   133     final boolean horizontal;
   134 
   135     Point orig;
   136 
   137     private Viewer(boolean horizontal) {
   138         this.horizontal = horizontal;
   139     }
   140     public void movementStarted(Widget widget) {
   141         System.err.println("started: " + widget.getBounds());
   142     }
   143 
   144     public void movementFinished(Widget widget) {
   145         System.err.println("finished: " + widget.getLocation());
   146     }
   147 
   148     public Point getOriginalLocation(Widget widget) {
   149         orig = new Point(widget.getBounds().x, widget.getBounds().y);
   150         System.err.println("orig: " + orig);
   151         return orig;
   152     }
   153 
   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)
   160         );
   161     }
   162 
   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);
   167         if (onboard < 0) {
   168             onboard = 0;
   169         }
   170         if (onboard >= 9) {
   171             return p;
   172         }
   173         int real = 25 + onboard * 50;
   174         if (horizontal != cmpHori) {
   175             return real - 5;
   176         } else {
   177             return real;
   178         }
   179     }
   180 }