visidor/src/main/java/cz/xelfi/quoridor/visidor/Viewer.java
changeset 25 f27d4c4cd464
parent 24 fdd6c390fa5f
child 29 e5e40fd406c1
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/visidor/src/main/java/cz/xelfi/quoridor/visidor/Viewer.java	Mon May 25 09:07:15 2009 +0200
     1.3 @@ -0,0 +1,180 @@
     1.4 +/*
     1.5 + * To change this template, choose Tools | Templates
     1.6 + * and open the template in the editor.
     1.7 + */
     1.8 +
     1.9 +package cz.xelfi.quoridor.visidor;
    1.10 +
    1.11 +import cz.xelfi.quoridor.Board;
    1.12 +import cz.xelfi.quoridor.Fence;
    1.13 +import cz.xelfi.quoridor.Fence.Orientation;
    1.14 +import cz.xelfi.quoridor.IllegalPositionException;
    1.15 +import cz.xelfi.quoridor.Move;
    1.16 +import java.awt.Color;
    1.17 +import java.awt.Point;
    1.18 +import java.awt.Rectangle;
    1.19 +import javax.swing.JFrame;
    1.20 +import org.netbeans.api.visual.action.ActionFactory;
    1.21 +import org.netbeans.api.visual.action.MoveProvider;
    1.22 +import org.netbeans.api.visual.anchor.AnchorFactory;
    1.23 +import org.netbeans.api.visual.border.BorderFactory;
    1.24 +import org.netbeans.api.visual.widget.ConnectionWidget;
    1.25 +import org.netbeans.api.visual.widget.ImageWidget;
    1.26 +import org.netbeans.api.visual.widget.LabelWidget;
    1.27 +import org.netbeans.api.visual.widget.LayerWidget;
    1.28 +import org.netbeans.api.visual.widget.Scene;
    1.29 +import org.netbeans.api.visual.widget.Widget;
    1.30 +
    1.31 +/**
    1.32 + *
    1.33 + * @author Jaroslav Tulach <jtulach@netbeans.org>
    1.34 + */
    1.35 +public class Viewer implements MoveProvider {
    1.36 +
    1.37 +    /**
    1.38 +     * @param args the command line arguments
    1.39 +     */
    1.40 +    public static void main(String[] args) throws IllegalPositionException {
    1.41 +        Board b = Board.empty();
    1.42 +        for (int i = 1; i <= 8; i++) {
    1.43 +            b = b.apply(Move.fence('A', i, Orientation.values()[i % 2]));
    1.44 +        }
    1.45 +        for (int i = 1; i <= 8; i++) {
    1.46 +            b = b.apply(Move.fence('H', i, Orientation.values()[(i + 1) % 2]));
    1.47 +        }
    1.48 +        //b = b.apply(Move.fence('H', 5, Orientation.HORIZONTAL));
    1.49 +        view(b);
    1.50 +    }
    1.51 +
    1.52 +    private static void view(Board b) {
    1.53 +        Scene scene = new Scene();
    1.54 +//        Scene layerBoard = scene;
    1.55 +        final LayerWidget layerLines = new LayerWidget(scene);
    1.56 +        scene.addChild(layerLines);
    1.57 +        final LayerWidget layerBoard = new LayerWidget(scene);
    1.58 +        layerLines.addChild(layerBoard);
    1.59 +        final int WIDTH = 50;
    1.60 +        final int HEIGHT = 50;
    1.61 +//        conn.setSourceAnchor(ahoj);
    1.62 +        Widget[][] fields = new Widget[10][];
    1.63 +        for (int i = 0; i < fields.length; i++) {
    1.64 +            fields[i] = new Widget[10];
    1.65 +            for (int j = 0; j < fields[i].length; j++) {
    1.66 +                LabelWidget w = new LabelWidget(scene);
    1.67 +                w.setPreferredBounds(new Rectangle(i * WIDTH, j * HEIGHT, WIDTH, HEIGHT));
    1.68 +                layerBoard.addChild(w);
    1.69 +                fields[i][j] = w;
    1.70 +            }
    1.71 +        }
    1.72 +        for (int i = 0; i < fields.length; i++) {
    1.73 +            for (int j = 0; j < fields[i].length; j++) {
    1.74 +                if (i > 0) {
    1.75 +                    ConnectionWidget horiz = new ConnectionWidget(scene);
    1.76 +                    horiz.setSourceAnchor(AnchorFactory.createCenterAnchor(fields[i - 1][j]));
    1.77 +                    horiz.setTargetAnchor(AnchorFactory.createCenterAnchor(fields[i][j]));
    1.78 +                    scene.addChild(horiz);
    1.79 +                }
    1.80 +                if (j > 0) {
    1.81 +                    ConnectionWidget vert = new ConnectionWidget(scene);
    1.82 +                    vert.setSourceAnchor(AnchorFactory.createCenterAnchor(fields[i][j - 1]));
    1.83 +                    vert.setTargetAnchor(AnchorFactory.createCenterAnchor(fields[i][j]));
    1.84 +                    scene.addChild(vert);
    1.85 +                }
    1.86 +            }
    1.87 +        }
    1.88 +
    1.89 +        ImageWidget horizontalWall = new ImageWidget(scene);
    1.90 +        horizontalWall.setPreferredBounds(new Rectangle(550, 300, 90, 10));
    1.91 +        horizontalWall.setBackground(Color.BLACK);
    1.92 +        horizontalWall.getActions().addAction(ActionFactory.createMoveAction(null, new Viewer(true)));
    1.93 +        horizontalWall.setBorder(BorderFactory.createLineBorder());
    1.94 +        horizontalWall.setOpaque(true);
    1.95 +        layerBoard.addChild(horizontalWall);
    1.96 +
    1.97 +        ImageWidget verticalWall = new ImageWidget(scene);
    1.98 +        verticalWall.setPreferredBounds(new Rectangle(600, 150, 10, 90));
    1.99 +        verticalWall.setBackground(Color.BLACK);
   1.100 +        verticalWall.getActions().addAction(ActionFactory.createMoveAction(null, new Viewer(false)));
   1.101 +        verticalWall.setBorder(BorderFactory.createLineBorder());
   1.102 +        verticalWall.setOpaque(true);
   1.103 +        layerBoard.addChild(verticalWall);
   1.104 +
   1.105 +        for (Fence f : b.getFences()) {
   1.106 +            Rectangle r = fenceRectangle(f.getColumn(), f.getRow(), f.getOrientation());
   1.107 +            ImageWidget fenceWall = new ImageWidget(scene);
   1.108 +            fenceWall.setPreferredBounds(r);
   1.109 +            fenceWall.setBackground(Color.BLACK);
   1.110 +            fenceWall.setBorder(BorderFactory.createLineBorder());
   1.111 +            fenceWall.setOpaque(true);
   1.112 +            layerBoard.addChild(fenceWall);
   1.113 +        }
   1.114 +
   1.115 +        JFrame f = new JFrame();
   1.116 +        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
   1.117 +        
   1.118 +        f.add(scene.createView());
   1.119 +        f.pack();
   1.120 +        f.setVisible(true);
   1.121 +    }
   1.122 +
   1.123 +    private static Rectangle fenceRectangle(char column, int row, Fence.Orientation o) {
   1.124 +        int w, h;
   1.125 +        switch (o) {
   1.126 +            case HORIZONTAL: w = 45; h = 5; break;
   1.127 +            case VERTICAL: w = 5; h = 45; break;
   1.128 +            default: throw new IllegalStateException();
   1.129 +        }
   1.130 +
   1.131 +        return new Rectangle(
   1.132 +            (column - 'A' + 1) * 50 + 25 - w, row * 50 + 25 - h, 2 * w, 2 * h
   1.133 +        );
   1.134 +    }
   1.135 +
   1.136 +    final boolean horizontal;
   1.137 +
   1.138 +    Point orig;
   1.139 +
   1.140 +    private Viewer(boolean horizontal) {
   1.141 +        this.horizontal = horizontal;
   1.142 +    }
   1.143 +    public void movementStarted(Widget widget) {
   1.144 +        System.err.println("started: " + widget.getBounds());
   1.145 +    }
   1.146 +
   1.147 +    public void movementFinished(Widget widget) {
   1.148 +        System.err.println("finished: " + widget.getLocation());
   1.149 +    }
   1.150 +
   1.151 +    public Point getOriginalLocation(Widget widget) {
   1.152 +        orig = new Point(widget.getBounds().x, widget.getBounds().y);
   1.153 +        System.err.println("orig: " + orig);
   1.154 +        return orig;
   1.155 +    }
   1.156 +
   1.157 +    public void setNewLocation(Widget widget, Point location) {
   1.158 +        widget.setPreferredBounds(new Rectangle(
   1.159 +            round(location.x, true) + (horizontal ? 5 : 0),
   1.160 +            round(location.y, false) + (horizontal ? 0 : 5),
   1.161 +            horizontal ? 90 : 10,
   1.162 +            horizontal ? 10 : 90)
   1.163 +        );
   1.164 +    }
   1.165 +
   1.166 +    int round(int p, boolean cmpHori) {
   1.167 +//        p = horizontal ? orig.x + p: orig.y + p;
   1.168 +        int onboard = p / 50;
   1.169 +        System.err.println(cmpHori ? "hori: " + onboard : "  vert: " + onboard);
   1.170 +        if (onboard < 0) {
   1.171 +            onboard = 0;
   1.172 +        }
   1.173 +        if (onboard >= 9) {
   1.174 +            return p;
   1.175 +        }
   1.176 +        int real = 25 + onboard * 50;
   1.177 +        if (horizontal != cmpHori) {
   1.178 +            return real - 5;
   1.179 +        } else {
   1.180 +            return real;
   1.181 +        }
   1.182 +    }
   1.183 +}