visidor/src/visidor/Viewer.java
author Jaroslav Tulach <jtulach@netbeans.org>
Sun, 24 May 2009 16:27:38 +0200
changeset 23 d4d8dc9d052b
parent 19 ae8603970d9a
child 24 fdd6c390fa5f
permissions -rw-r--r--
Ability to paint fences
jtulach@18
     1
/*
jtulach@18
     2
 * To change this template, choose Tools | Templates
jtulach@18
     3
 * and open the template in the editor.
jtulach@18
     4
 */
jtulach@18
     5
jtulach@18
     6
package visidor;
jtulach@18
     7
jtulach@23
     8
import cz.xelfi.quoridor.Board;
jtulach@23
     9
import cz.xelfi.quoridor.Fence;
jtulach@23
    10
import cz.xelfi.quoridor.Fence.Orientation;
jtulach@23
    11
import cz.xelfi.quoridor.IllegalPositionException;
jtulach@23
    12
import cz.xelfi.quoridor.Move;
jtulach@18
    13
import java.awt.Color;
jtulach@18
    14
import java.awt.Point;
jtulach@18
    15
import java.awt.Rectangle;
jtulach@18
    16
import javax.swing.JFrame;
jtulach@18
    17
import org.netbeans.api.visual.action.ActionFactory;
jtulach@18
    18
import org.netbeans.api.visual.action.MoveProvider;
jtulach@18
    19
import org.netbeans.api.visual.anchor.AnchorFactory;
jtulach@18
    20
import org.netbeans.api.visual.border.BorderFactory;
jtulach@18
    21
import org.netbeans.api.visual.widget.ConnectionWidget;
jtulach@18
    22
import org.netbeans.api.visual.widget.ImageWidget;
jtulach@18
    23
import org.netbeans.api.visual.widget.LabelWidget;
jtulach@18
    24
import org.netbeans.api.visual.widget.LayerWidget;
jtulach@18
    25
import org.netbeans.api.visual.widget.Scene;
jtulach@18
    26
import org.netbeans.api.visual.widget.Widget;
jtulach@18
    27
jtulach@18
    28
/**
jtulach@18
    29
 *
jtulach@18
    30
 * @author Jaroslav Tulach <jtulach@netbeans.org>
jtulach@18
    31
 */
jtulach@18
    32
public class Viewer implements MoveProvider {
jtulach@18
    33
jtulach@18
    34
    /**
jtulach@18
    35
     * @param args the command line arguments
jtulach@18
    36
     */
jtulach@23
    37
    public static void main(String[] args) throws IllegalPositionException {
jtulach@23
    38
        Board b = Board.empty();
jtulach@23
    39
        for (int i = 1; i <= 8; i++) {
jtulach@23
    40
            b = b.apply(Move.fence('A', i, Orientation.values()[i % 2]));
jtulach@23
    41
        }
jtulach@23
    42
        for (int i = 1; i <= 8; i++) {
jtulach@23
    43
            b = b.apply(Move.fence('H', i, Orientation.values()[(i + 1) % 2]));
jtulach@23
    44
        }
jtulach@23
    45
        //b = b.apply(Move.fence('H', 5, Orientation.HORIZONTAL));
jtulach@23
    46
        view(b);
jtulach@23
    47
    }
jtulach@23
    48
jtulach@23
    49
    private static void view(Board b) {
jtulach@18
    50
        Scene scene = new Scene();
jtulach@18
    51
//        Scene layerBoard = scene;
jtulach@18
    52
        final LayerWidget layerLines = new LayerWidget(scene);
jtulach@18
    53
        scene.addChild(layerLines);
jtulach@18
    54
        final LayerWidget layerBoard = new LayerWidget(scene);
jtulach@18
    55
        layerLines.addChild(layerBoard);
jtulach@18
    56
        final int WIDTH = 50;
jtulach@18
    57
        final int HEIGHT = 50;
jtulach@18
    58
//        conn.setSourceAnchor(ahoj);
jtulach@18
    59
        Widget[][] fields = new Widget[10][];
jtulach@18
    60
        for (int i = 0; i < fields.length; i++) {
jtulach@18
    61
            fields[i] = new Widget[10];
jtulach@18
    62
            for (int j = 0; j < fields[i].length; j++) {
jtulach@18
    63
                LabelWidget w = new LabelWidget(scene);
jtulach@18
    64
                w.setPreferredBounds(new Rectangle(i * WIDTH, j * HEIGHT, WIDTH, HEIGHT));
jtulach@18
    65
                layerBoard.addChild(w);
jtulach@18
    66
                fields[i][j] = w;
jtulach@18
    67
            }
jtulach@18
    68
        }
jtulach@18
    69
        for (int i = 0; i < fields.length; i++) {
jtulach@18
    70
            for (int j = 0; j < fields[i].length; j++) {
jtulach@18
    71
                if (i > 0) {
jtulach@18
    72
                    ConnectionWidget horiz = new ConnectionWidget(scene);
jtulach@18
    73
                    horiz.setSourceAnchor(AnchorFactory.createCenterAnchor(fields[i - 1][j]));
jtulach@18
    74
                    horiz.setTargetAnchor(AnchorFactory.createCenterAnchor(fields[i][j]));
jtulach@18
    75
                    scene.addChild(horiz);
jtulach@18
    76
                }
jtulach@18
    77
                if (j > 0) {
jtulach@18
    78
                    ConnectionWidget vert = new ConnectionWidget(scene);
jtulach@18
    79
                    vert.setSourceAnchor(AnchorFactory.createCenterAnchor(fields[i][j - 1]));
jtulach@18
    80
                    vert.setTargetAnchor(AnchorFactory.createCenterAnchor(fields[i][j]));
jtulach@18
    81
                    scene.addChild(vert);
jtulach@18
    82
                }
jtulach@18
    83
            }
jtulach@18
    84
        }
jtulach@18
    85
jtulach@18
    86
        ImageWidget horizontalWall = new ImageWidget(scene);
jtulach@19
    87
        horizontalWall.setPreferredBounds(new Rectangle(550, 300, 90, 10));
jtulach@18
    88
        horizontalWall.setBackground(Color.BLACK);
jtulach@18
    89
        horizontalWall.getActions().addAction(ActionFactory.createMoveAction(null, new Viewer(true)));
jtulach@18
    90
        horizontalWall.setForeground(Color.BLACK);
jtulach@18
    91
        horizontalWall.setBorder(BorderFactory.createLineBorder());
jtulach@18
    92
        layerBoard.addChild(horizontalWall);
jtulach@18
    93
jtulach@18
    94
        ImageWidget verticalWall = new ImageWidget(scene);
jtulach@19
    95
        verticalWall.setPreferredBounds(new Rectangle(600, 150, 10, 90));
jtulach@18
    96
        verticalWall.setBackground(Color.BLACK);
jtulach@18
    97
        verticalWall.getActions().addAction(ActionFactory.createMoveAction(null, new Viewer(false)));
jtulach@18
    98
        verticalWall.setBorder(BorderFactory.createLineBorder());
jtulach@18
    99
        layerBoard.addChild(verticalWall);
jtulach@18
   100
jtulach@23
   101
        for (Fence f : b.getFences()) {
jtulach@23
   102
            Rectangle r = fenceRectangle(f.getColumn(), f.getRow(), f.getOrientation());
jtulach@23
   103
            ImageWidget fenceWall = new ImageWidget(scene);
jtulach@23
   104
            fenceWall.setPreferredBounds(r);
jtulach@23
   105
            fenceWall.setBackground(Color.BLACK);
jtulach@23
   106
            fenceWall.setBorder(BorderFactory.createLineBorder());
jtulach@23
   107
            layerBoard.addChild(fenceWall);
jtulach@23
   108
        }
jtulach@23
   109
jtulach@18
   110
        JFrame f = new JFrame();
jtulach@18
   111
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jtulach@18
   112
        
jtulach@18
   113
        f.add(scene.createView());
jtulach@18
   114
        f.pack();
jtulach@18
   115
        f.setVisible(true);
jtulach@18
   116
    }
jtulach@18
   117
jtulach@23
   118
    private static Rectangle fenceRectangle(char column, int row, Fence.Orientation o) {
jtulach@23
   119
        int w, h;
jtulach@23
   120
        switch (o) {
jtulach@23
   121
            case HORIZONTAL: w = 45; h = 5; break;
jtulach@23
   122
            case VERTICAL: w = 5; h = 45; break;
jtulach@23
   123
            default: throw new IllegalStateException();
jtulach@23
   124
        }
jtulach@23
   125
jtulach@23
   126
        return new Rectangle(
jtulach@23
   127
            (column - 'A' + 1) * 50 + 25 - w, row * 50 + 25 - h, 2 * w, 2 * h
jtulach@23
   128
        );
jtulach@23
   129
    }
jtulach@23
   130
jtulach@18
   131
    final boolean horizontal;
jtulach@18
   132
jtulach@18
   133
    Point orig;
jtulach@18
   134
jtulach@18
   135
    private Viewer(boolean horizontal) {
jtulach@18
   136
        this.horizontal = horizontal;
jtulach@18
   137
    }
jtulach@18
   138
    public void movementStarted(Widget widget) {
jtulach@18
   139
        System.err.println("started: " + widget.getBounds());
jtulach@18
   140
    }
jtulach@18
   141
jtulach@18
   142
    public void movementFinished(Widget widget) {
jtulach@18
   143
        System.err.println("finished: " + widget.getLocation());
jtulach@18
   144
    }
jtulach@18
   145
jtulach@18
   146
    public Point getOriginalLocation(Widget widget) {
jtulach@18
   147
        orig = new Point(widget.getBounds().x, widget.getBounds().y);
jtulach@18
   148
        System.err.println("orig: " + orig);
jtulach@18
   149
        return orig;
jtulach@18
   150
    }
jtulach@18
   151
jtulach@18
   152
    public void setNewLocation(Widget widget, Point location) {
jtulach@18
   153
        widget.setPreferredBounds(new Rectangle(
jtulach@19
   154
            round(location.x, true) + (horizontal ? 5 : 0),
jtulach@19
   155
            round(location.y, false) + (horizontal ? 0 : 5),
jtulach@19
   156
            horizontal ? 90 : 10,
jtulach@19
   157
            horizontal ? 10 : 90)
jtulach@18
   158
        );
jtulach@18
   159
    }
jtulach@18
   160
jtulach@18
   161
    int round(int p, boolean cmpHori) {
jtulach@18
   162
//        p = horizontal ? orig.x + p: orig.y + p;
jtulach@18
   163
        int onboard = p / 50;
jtulach@18
   164
        System.err.println(cmpHori ? "hori: " + onboard : "  vert: " + onboard);
jtulach@18
   165
        if (onboard < 0) {
jtulach@18
   166
            onboard = 0;
jtulach@18
   167
        }
jtulach@18
   168
        if (onboard >= 9) {
jtulach@18
   169
            return p;
jtulach@18
   170
        }
jtulach@18
   171
        int real = 25 + onboard * 50;
jtulach@18
   172
        if (horizontal != cmpHori) {
jtulach@18
   173
            return real - 5;
jtulach@18
   174
        } else {
jtulach@18
   175
            return real;
jtulach@18
   176
        }
jtulach@18
   177
    }
jtulach@18
   178
}