visidor/src/main/java/cz/xelfi/quoridor/visidor/Viewer.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Sun, 05 Sep 2010 07:07:35 +0200
changeset 252 2769784e86da
parent 251 8b62bffb1a8f
child 253 ee02205edf13
permissions -rw-r--r--
Drawing position of the pawn
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@250
    44
import java.awt.image.BufferedImage;
jaroslav@249
    45
import javax.swing.JComponent;
jtulach@18
    46
import javax.swing.JFrame;
jtulach@29
    47
import javax.swing.JPanel;
jtulach@18
    48
jtulach@18
    49
/**
jtulach@18
    50
 *
jtulach@18
    51
 * @author Jaroslav Tulach <jtulach@netbeans.org>
jtulach@18
    52
 */
jaroslav@249
    53
final class Viewer extends JPanel implements MouseMotionListener {
jtulach@29
    54
    private Board board;
jaroslav@249
    55
    private int fieldSize;
jaroslav@249
    56
    private int xdelta;
jaroslav@249
    57
    private int ydelta;
jaroslav@249
    58
    private Orientation lastOrientation;
jtulach@29
    59
jtulach@29
    60
    public Viewer() {
jtulach@29
    61
        this(Board.empty());
jtulach@29
    62
    }
jtulach@29
    63
jtulach@29
    64
    private Viewer(Board b) {
jtulach@29
    65
        this.board = b;
jaroslav@249
    66
        addMouseMotionListener(this);
jtulach@29
    67
    }
jtulach@29
    68
jtulach@18
    69
jtulach@18
    70
    /**
jtulach@18
    71
     * @param args the command line arguments
jtulach@18
    72
     */
jtulach@23
    73
    public static void main(String[] args) throws IllegalPositionException {
jtulach@23
    74
        Board b = Board.empty();
jtulach@23
    75
        for (int i = 1; i <= 8; i++) {
jtulach@23
    76
            b = b.apply(Move.fence('A', i, Orientation.values()[i % 2]));
jtulach@23
    77
        }
jtulach@23
    78
        for (int i = 1; i <= 8; i++) {
jtulach@23
    79
            b = b.apply(Move.fence('H', i, Orientation.values()[(i + 1) % 2]));
jtulach@23
    80
        }
jtulach@29
    81
jtulach@29
    82
        JFrame f = new JFrame();
jtulach@29
    83
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jtulach@29
    84
        f.add(new Viewer(b));
jtulach@29
    85
        f.pack();
jtulach@29
    86
        f.setVisible(true);
jtulach@23
    87
    }
jtulach@23
    88
jaroslav@248
    89
    @Override
jaroslav@249
    90
    public Dimension getPreferredSize() {
jaroslav@249
    91
        return new Dimension(450, 450);
jaroslav@249
    92
    }
jaroslav@249
    93
jaroslav@249
    94
    @Override
jaroslav@248
    95
    protected void paintComponent(Graphics gg) {
jaroslav@249
    96
        assert EventQueue.isDispatchThread();
jaroslav@249
    97
        
jaroslav@249
    98
        fieldSize = Math.min(getSize().width, getSize().height) / 9;
jaroslav@248
    99
        int fifth = fieldSize / 10;
jaroslav@248
   100
        Graphics2D g = (Graphics2D) gg;
jaroslav@248
   101
        
jaroslav@249
   102
        xdelta = (getSize().width - fieldSize * 9) / 2;
jaroslav@249
   103
        ydelta = (getSize().height - fieldSize * 9) / 2;
jaroslav@248
   104
        
jaroslav@249
   105
        g.translate(xdelta, ydelta);
jaroslav@248
   106
jaroslav@248
   107
        g.setColor(Color.lightGray);
jaroslav@248
   108
        for (int i = 0; i < 9; i++) {
jaroslav@248
   109
            for (int j = 0; j < 9; j++) {
jaroslav@248
   110
                final Rectangle r = new Rectangle(i * fieldSize, j * fieldSize, fieldSize, fieldSize);
jaroslav@248
   111
                g.fillRect(r.x + fifth, r.y + fifth, r.width - fifth * 2, r.height - fifth * 2);
jtulach@18
   112
            }
jtulach@18
   113
        }
jtulach@18
   114
jtulach@18
   115
jaroslav@248
   116
        g.setColor(Color.BLACK);
jaroslav@248
   117
        for (Fence f : board.getFences()) {
jaroslav@248
   118
            int column = (f.getColumn() - 'A') + 1;
jaroslav@248
   119
            int row = 9 - f.getRow();
jaroslav@249
   120
            Orientation orient = f.getOrientation();
jaroslav@251
   121
            drawFence(orient, column, row, g, true);
jtulach@23
   122
        }
jtulach@23
   123
jtulach@29
   124
        int cnt = 0;
jaroslav@248
   125
        Color[] colors = { Color.WHITE, Color.BLACK, Color.ORANGE, Color.MAGENTA };
jaroslav@248
   126
        for (Player p : board.getPlayers()) {
jaroslav@248
   127
            int column = p.getColumn();
jaroslav@248
   128
            int row = 8 - p.getRow();
jaroslav@252
   129
            final boolean isCurrent = p == board.getCurrentPlayer();
jaroslav@252
   130
            final Color currentColor = colors[cnt];
jaroslav@252
   131
            drawPlayer(g, column, row, currentColor, isCurrent);
jaroslav@248
   132
            cnt++;
jaroslav@248
   133
        }
jtulach@29
   134
jaroslav@248
   135
        g.setColor(Color.BLACK);
jaroslav@248
   136
        final Font f = new Font("Monospace", Font.BOLD, fifth * 2);
jaroslav@248
   137
        g.setFont(f);
jaroslav@248
   138
        for (int i = 0; i < 8; i++) {
jaroslav@248
   139
            final char ch = (char) ('A' + i);
jaroslav@248
   140
            g.drawString(Character.toString(ch), i * fieldSize + fieldSize - fifth, fifth + f.getSize() - f.getBaselineFor(ch));
jaroslav@248
   141
        }
jaroslav@248
   142
        for (int i = 0; i < 8; i++) {
jaroslav@248
   143
            String s = "" + (8 - i);
jaroslav@248
   144
            g.drawString(s, fifth, i * fieldSize + fieldSize - fifth + f.getSize() - f.getBaselineFor(s.charAt(0)));
jtulach@29
   145
        }
jtulach@18
   146
    }
jtulach@18
   147
jaroslav@252
   148
    private void drawPlayer(Graphics2D g, int column, int row, final Color currentColor, final boolean isCurrent) {
jaroslav@252
   149
        int fifth = fieldSize / 10;
jaroslav@252
   150
        int diametr = fieldSize - fifth * 4;
jaroslav@252
   151
        final Rectangle r = new Rectangle(
jaroslav@252
   152
            column * fieldSize + 2 * fifth,
jaroslav@252
   153
            row * fieldSize + 2 * fifth,
jaroslav@252
   154
            diametr,
jaroslav@252
   155
            diametr
jaroslav@252
   156
        );
jaroslav@252
   157
        if (currentColor == null) {
jaroslav@252
   158
            g.setColor(Color.BLACK);
jaroslav@252
   159
            g.drawOval(r.x, r.y, r.width, r.height);
jaroslav@252
   160
        } else {
jaroslav@252
   161
            g.setColor(currentColor);
jaroslav@252
   162
            g.fillOval(r.x, r.y, r.width, r.height);
jaroslav@252
   163
            if (isCurrent) {
jaroslav@252
   164
                g.setColor(Color.lightGray);
jaroslav@252
   165
                g.fillOval(r.x + r.width / 3, r.y + r.height / 3, r.width / 3, r.height / 3);
jaroslav@252
   166
            }
jaroslav@252
   167
        }
jaroslav@252
   168
    }
jaroslav@252
   169
jaroslav@251
   170
    private void drawFence(Orientation orient, int column, int row, Graphics2D g, boolean fill) throws IllegalStateException {
jaroslav@249
   171
        int fifth = fieldSize / 10;
jaroslav@249
   172
        int w, h;
jaroslav@249
   173
        switch (orient) {
jaroslav@249
   174
            case HORIZONTAL: w = fieldSize - fifth; h = fifth; break;
jaroslav@249
   175
            case VERTICAL: w = fifth; h = fieldSize - fifth; break;
jaroslav@249
   176
            default: throw new IllegalStateException();
jaroslav@249
   177
        }
jaroslav@249
   178
        Rectangle r = new Rectangle(
jaroslav@249
   179
            column * fieldSize - w,
jaroslav@249
   180
            row * fieldSize - h,
jaroslav@249
   181
            2 * w,
jaroslav@249
   182
            2 * h
jaroslav@249
   183
        );
jaroslav@251
   184
        if (fill) {
jaroslav@251
   185
            g.fill(r);
jaroslav@251
   186
        } else {
jaroslav@251
   187
            g.draw(r);
jaroslav@251
   188
        }
jaroslav@249
   189
    }
jaroslav@249
   190
jaroslav@249
   191
    public void mouseDragged(MouseEvent e) {
jaroslav@249
   192
    }
jaroslav@249
   193
jaroslav@249
   194
    public void mouseMoved(MouseEvent e) {
jaroslav@249
   195
        int x = Math.round(((float)(e.getX() - xdelta)) / fieldSize);
jaroslav@249
   196
        int y = Math.round(((float)(e.getY() - ydelta)) / fieldSize);
jaroslav@249
   197
        if (x <= 0 || x >= 9) {
jaroslav@249
   198
            return;
jaroslav@249
   199
        }
jaroslav@249
   200
        if (y <= 0 || y >= 9) {
jaroslav@249
   201
            return;
jaroslav@249
   202
        }
jaroslav@252
   203
jaroslav@252
   204
        BufferedImage img = new BufferedImage(getWidth(), getHeight(), BufferedImage.TYPE_INT_ARGB);
jaroslav@252
   205
        final Graphics2D d2 = img.createGraphics();
jaroslav@252
   206
        d2.setBackground(getBackground());
jaroslav@252
   207
        d2.clearRect(0, 0, getWidth(), getHeight());
jaroslav@252
   208
        paintComponent(d2);
jaroslav@252
   209
        
jaroslav@249
   210
        
jaroslav@249
   211
        int dx = (e.getX() - xdelta) % fieldSize;
jaroslav@249
   212
        int dy = (e.getY() - ydelta) % fieldSize;
jaroslav@249
   213
        
jaroslav@249
   214
        int fifth = fieldSize / 10;
jaroslav@249
   215
        boolean outOfX = dx > fifth && dx < fieldSize - fifth;
jaroslav@249
   216
        boolean outOfY = dy > fifth && dy < fieldSize - fifth;
jaroslav@249
   217
        if (outOfX && outOfY) {
jaroslav@252
   218
            x = (e.getX() - xdelta) / fieldSize;
jaroslav@252
   219
            y = (e.getY() - ydelta) / fieldSize;
jaroslav@252
   220
            drawPlayer(d2, x, y, null, false);
jaroslav@252
   221
            getGraphics().drawImage(img, 0, 0, null);
jaroslav@249
   222
            return;
jaroslav@249
   223
        }
jaroslav@249
   224
        if (!outOfX && !outOfY) {
jaroslav@249
   225
            if (lastOrientation == null) {
jaroslav@249
   226
                return;
jaroslav@249
   227
            }
jaroslav@249
   228
        } else {
jaroslav@249
   229
            if (outOfX) {
jaroslav@249
   230
                lastOrientation = Orientation.HORIZONTAL;
jaroslav@249
   231
            } else {
jaroslav@249
   232
                lastOrientation = Orientation.VERTICAL;
jaroslav@249
   233
            }
jaroslav@249
   234
        }
jaroslav@251
   235
        
jaroslav@251
   236
        try {
jaroslav@251
   237
            Board newBoard = board.apply(Move.fence((char)('A' + x - 1), 9 - y, lastOrientation));
jaroslav@251
   238
            drawFence(lastOrientation, x, y, d2, true);
jaroslav@251
   239
        } catch (IllegalPositionException ex) {
jaroslav@251
   240
            // can't place fence
jaroslav@251
   241
            drawFence(lastOrientation, x, y, d2, false);
jaroslav@251
   242
        }
jaroslav@251
   243
        
jaroslav@250
   244
        
jaroslav@250
   245
        getGraphics().drawImage(img, 0, 0, null);
jaroslav@249
   246
    }
jaroslav@249
   247
jtulach@18
   248
}