visidor/src/main/java/cz/xelfi/quoridor/visidor/Viewer.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Sun, 05 Sep 2010 00:07:32 +0200
changeset 249 47d62a3afe63
parent 248 9bbf25021886
child 250 eb6053abdd00
permissions -rw-r--r--
Preview of new fence position
jtulach@18
     1
/*
jaroslav@178
     2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
jaroslav@178
     3
 *
jaroslav@178
     4
 * The contents of this file are subject to the terms of either the GNU
jaroslav@178
     5
 * General Public License Version 2 only ("GPL") or the Common
jaroslav@178
     6
 * Development and Distribution License("CDDL") (collectively, the
jaroslav@178
     7
 * "License"). You may not use this file except in compliance with the
jaroslav@178
     8
 * License. You can obtain a copy of the License at
jaroslav@178
     9
 * http://www.netbeans.org/cddl-gplv2.html
jaroslav@178
    10
 * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
jaroslav@178
    11
 * specific language governing permissions and limitations under the
jaroslav@178
    12
 * License.  When distributing the software, include this License Header
jaroslav@178
    13
 * Notice in each file and include the License file at
jaroslav@178
    14
 * nbbuild/licenses/CDDL-GPL-2-CP.  Sun designates this
jaroslav@178
    15
 * particular file as subject to the "Classpath" exception as provided
jaroslav@178
    16
 * by Sun in the GPL Version 2 section of the License file that
jaroslav@178
    17
 * accompanied this code. If applicable, add the following below the
jaroslav@178
    18
 * License Header, with the fields enclosed by brackets [] replaced by
jaroslav@178
    19
 * your own identifying information:
jaroslav@178
    20
 * "Portions Copyrighted [year] [name of copyright owner]"
jaroslav@178
    21
 *
jaroslav@178
    22
 * Contributor(s):
jaroslav@178
    23
 *
jaroslav@178
    24
 * Portions Copyrighted 2009 Jaroslav Tulach
jtulach@18
    25
 */
jtulach@25
    26
package cz.xelfi.quoridor.visidor;
jtulach@18
    27
jtulach@23
    28
import cz.xelfi.quoridor.Board;
jtulach@23
    29
import cz.xelfi.quoridor.Fence;
jtulach@23
    30
import cz.xelfi.quoridor.Fence.Orientation;
jtulach@23
    31
import cz.xelfi.quoridor.IllegalPositionException;
jtulach@23
    32
import cz.xelfi.quoridor.Move;
jtulach@29
    33
import cz.xelfi.quoridor.Player;
jtulach@18
    34
import java.awt.Color;
jaroslav@249
    35
import java.awt.Dimension;
jaroslav@249
    36
import java.awt.EventQueue;
jaroslav@248
    37
import java.awt.Font;
jaroslav@248
    38
import java.awt.Graphics;
jtulach@29
    39
import java.awt.Graphics2D;
jaroslav@249
    40
import java.awt.GridLayout;
jtulach@18
    41
import java.awt.Rectangle;
jaroslav@249
    42
import java.awt.event.MouseEvent;
jaroslav@249
    43
import java.awt.event.MouseMotionListener;
jaroslav@249
    44
import javax.swing.JComponent;
jtulach@18
    45
import javax.swing.JFrame;
jtulach@29
    46
import javax.swing.JPanel;
jtulach@18
    47
jtulach@18
    48
/**
jtulach@18
    49
 *
jtulach@18
    50
 * @author Jaroslav Tulach <jtulach@netbeans.org>
jtulach@18
    51
 */
jaroslav@249
    52
final class Viewer extends JPanel implements MouseMotionListener {
jtulach@29
    53
    private Board board;
jaroslav@249
    54
    private int fieldSize;
jaroslav@249
    55
    private int xdelta;
jaroslav@249
    56
    private int ydelta;
jaroslav@249
    57
    private Orientation lastOrientation;
jtulach@29
    58
jtulach@29
    59
    public Viewer() {
jtulach@29
    60
        this(Board.empty());
jtulach@29
    61
    }
jtulach@29
    62
jtulach@29
    63
    private Viewer(Board b) {
jtulach@29
    64
        this.board = b;
jaroslav@249
    65
        addMouseMotionListener(this);
jtulach@29
    66
    }
jtulach@29
    67
jtulach@18
    68
jtulach@18
    69
    /**
jtulach@18
    70
     * @param args the command line arguments
jtulach@18
    71
     */
jtulach@23
    72
    public static void main(String[] args) throws IllegalPositionException {
jtulach@23
    73
        Board b = Board.empty();
jtulach@23
    74
        for (int i = 1; i <= 8; i++) {
jtulach@23
    75
            b = b.apply(Move.fence('A', i, Orientation.values()[i % 2]));
jtulach@23
    76
        }
jtulach@23
    77
        for (int i = 1; i <= 8; i++) {
jtulach@23
    78
            b = b.apply(Move.fence('H', i, Orientation.values()[(i + 1) % 2]));
jtulach@23
    79
        }
jtulach@29
    80
jtulach@29
    81
        JFrame f = new JFrame();
jtulach@29
    82
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jtulach@29
    83
        f.add(new Viewer(b));
jtulach@29
    84
        f.pack();
jtulach@29
    85
        f.setVisible(true);
jtulach@23
    86
    }
jtulach@23
    87
jaroslav@248
    88
    @Override
jaroslav@249
    89
    public Dimension getPreferredSize() {
jaroslav@249
    90
        return new Dimension(450, 450);
jaroslav@249
    91
    }
jaroslav@249
    92
jaroslav@249
    93
    @Override
jaroslav@248
    94
    protected void paintComponent(Graphics gg) {
jaroslav@249
    95
        assert EventQueue.isDispatchThread();
jaroslav@249
    96
        
jaroslav@249
    97
        fieldSize = Math.min(getSize().width, getSize().height) / 9;
jaroslav@248
    98
        int fifth = fieldSize / 10;
jaroslav@248
    99
        Graphics2D g = (Graphics2D) gg;
jaroslav@248
   100
        
jaroslav@249
   101
        xdelta = (getSize().width - fieldSize * 9) / 2;
jaroslav@249
   102
        ydelta = (getSize().height - fieldSize * 9) / 2;
jaroslav@248
   103
        
jaroslav@249
   104
        g.translate(xdelta, ydelta);
jaroslav@248
   105
jaroslav@248
   106
        g.setColor(Color.lightGray);
jaroslav@248
   107
        for (int i = 0; i < 9; i++) {
jaroslav@248
   108
            for (int j = 0; j < 9; j++) {
jaroslav@248
   109
                final Rectangle r = new Rectangle(i * fieldSize, j * fieldSize, fieldSize, fieldSize);
jaroslav@248
   110
                g.fillRect(r.x + fifth, r.y + fifth, r.width - fifth * 2, r.height - fifth * 2);
jtulach@18
   111
            }
jtulach@18
   112
        }
jtulach@18
   113
jtulach@18
   114
jaroslav@248
   115
        g.setColor(Color.BLACK);
jaroslav@248
   116
        for (Fence f : board.getFences()) {
jaroslav@248
   117
            int column = (f.getColumn() - 'A') + 1;
jaroslav@248
   118
            int row = 9 - f.getRow();
jaroslav@249
   119
            Orientation orient = f.getOrientation();
jaroslav@249
   120
            drawFence(orient, column, row, g);
jtulach@23
   121
        }
jtulach@23
   122
jtulach@29
   123
        int cnt = 0;
jaroslav@248
   124
        Color[] colors = { Color.WHITE, Color.BLACK, Color.ORANGE, Color.MAGENTA };
jaroslav@248
   125
        int diametr = fieldSize - fifth * 4;
jaroslav@248
   126
        for (Player p : board.getPlayers()) {
jaroslav@248
   127
            int column = p.getColumn();
jaroslav@248
   128
            int row = 8 - p.getRow();
jaroslav@248
   129
            final Rectangle r = new Rectangle(
jaroslav@248
   130
                column * fieldSize + 2 * fifth,
jaroslav@248
   131
                row * fieldSize + 2 * fifth,
jaroslav@248
   132
                diametr,
jaroslav@248
   133
                diametr
jaroslav@248
   134
            );
jaroslav@248
   135
            g.setColor(colors[cnt]);
jaroslav@248
   136
            g.fillOval(r.x, r.y, r.width, r.height);
jaroslav@248
   137
            if (p == board.getCurrentPlayer()) {
jaroslav@248
   138
                g.setColor(Color.lightGray);
jaroslav@248
   139
                g.fillOval(r.x + r.width / 3, r.y + r.height / 3, r.width / 3, r.height / 3);
jtulach@29
   140
            }
jaroslav@248
   141
            cnt++;
jaroslav@248
   142
        }
jtulach@29
   143
jaroslav@248
   144
        g.setColor(Color.BLACK);
jaroslav@248
   145
        final Font f = new Font("Monospace", Font.BOLD, fifth * 2);
jaroslav@248
   146
        g.setFont(f);
jaroslav@248
   147
        for (int i = 0; i < 8; i++) {
jaroslav@248
   148
            final char ch = (char) ('A' + i);
jaroslav@248
   149
            g.drawString(Character.toString(ch), i * fieldSize + fieldSize - fifth, fifth + f.getSize() - f.getBaselineFor(ch));
jaroslav@248
   150
        }
jaroslav@248
   151
        for (int i = 0; i < 8; i++) {
jaroslav@248
   152
            String s = "" + (8 - i);
jaroslav@248
   153
            g.drawString(s, fifth, i * fieldSize + fieldSize - fifth + f.getSize() - f.getBaselineFor(s.charAt(0)));
jtulach@29
   154
        }
jtulach@18
   155
    }
jtulach@18
   156
jaroslav@249
   157
    private void drawFence(Orientation orient, int column, int row, Graphics2D g) throws IllegalStateException {
jaroslav@249
   158
        int fifth = fieldSize / 10;
jaroslav@249
   159
        int w, h;
jaroslav@249
   160
        switch (orient) {
jaroslav@249
   161
            case HORIZONTAL: w = fieldSize - fifth; h = fifth; break;
jaroslav@249
   162
            case VERTICAL: w = fifth; h = fieldSize - fifth; break;
jaroslav@249
   163
            default: throw new IllegalStateException();
jaroslav@249
   164
        }
jaroslav@249
   165
        Rectangle r = new Rectangle(
jaroslav@249
   166
            column * fieldSize - w,
jaroslav@249
   167
            row * fieldSize - h,
jaroslav@249
   168
            2 * w,
jaroslav@249
   169
            2 * h
jaroslav@249
   170
        );
jaroslav@249
   171
        g.fill(r);
jaroslav@249
   172
    }
jaroslav@249
   173
jaroslav@249
   174
    public void mouseDragged(MouseEvent e) {
jaroslav@249
   175
    }
jaroslav@249
   176
jaroslav@249
   177
    public void mouseMoved(MouseEvent e) {
jaroslav@249
   178
        final Graphics2D d2 = (Graphics2D) getGraphics();
jaroslav@249
   179
        
jaroslav@249
   180
        d2.clearRect(0, 0, getWidth(), getHeight());
jaroslav@249
   181
        paintComponent(d2);
jaroslav@249
   182
        
jaroslav@249
   183
        int x = Math.round(((float)(e.getX() - xdelta)) / fieldSize);
jaroslav@249
   184
        int y = Math.round(((float)(e.getY() - ydelta)) / fieldSize);
jaroslav@249
   185
        if (x <= 0 || x >= 9) {
jaroslav@249
   186
            return;
jaroslav@249
   187
        }
jaroslav@249
   188
        if (y <= 0 || y >= 9) {
jaroslav@249
   189
            return;
jaroslav@249
   190
        }
jaroslav@249
   191
        
jaroslav@249
   192
        int dx = (e.getX() - xdelta) % fieldSize;
jaroslav@249
   193
        int dy = (e.getY() - ydelta) % fieldSize;
jaroslav@249
   194
        
jaroslav@249
   195
        int fifth = fieldSize / 10;
jaroslav@249
   196
        boolean outOfX = dx > fifth && dx < fieldSize - fifth;
jaroslav@249
   197
        boolean outOfY = dy > fifth && dy < fieldSize - fifth;
jaroslav@249
   198
        if (outOfX && outOfY) {
jaroslav@249
   199
            return;
jaroslav@249
   200
        }
jaroslav@249
   201
        if (!outOfX && !outOfY) {
jaroslav@249
   202
            if (lastOrientation == null) {
jaroslav@249
   203
                return;
jaroslav@249
   204
            }
jaroslav@249
   205
        } else {
jaroslav@249
   206
            if (outOfX) {
jaroslav@249
   207
                lastOrientation = Orientation.HORIZONTAL;
jaroslav@249
   208
            } else {
jaroslav@249
   209
                lastOrientation = Orientation.VERTICAL;
jaroslav@249
   210
            }
jaroslav@249
   211
        }
jaroslav@249
   212
        d2.translate(xdelta, ydelta);
jaroslav@249
   213
        drawFence(lastOrientation, x, y, d2);
jaroslav@249
   214
    }
jaroslav@249
   215
jtulach@18
   216
}