visidor/src/main/java/cz/xelfi/quoridor/visidor/Viewer.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Sun, 05 Sep 2010 22:34:43 +0200
changeset 255 9ecd02d694cd
parent 254 1273bfb0e2e7
child 264 d60370059c3c
permissions -rw-r--r--
Official API for BoardPane with listener to react to user gestures.
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;
jtulach@18
    40
import java.awt.Rectangle;
jaroslav@249
    41
import java.awt.event.MouseEvent;
jaroslav@254
    42
import java.awt.event.MouseListener;
jaroslav@249
    43
import java.awt.event.MouseMotionListener;
jaroslav@250
    44
import java.awt.image.BufferedImage;
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@254
    52
final class Viewer extends JPanel implements MouseMotionListener, MouseListener {
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;
jaroslav@255
    58
    private BoardPane pane;
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;
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@29
    73
        JFrame f = new JFrame();
jtulach@29
    74
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jaroslav@255
    75
        final BoardPane bp = new BoardPane();
jaroslav@255
    76
        class ApplyMove implements BoardListener {
jaroslav@255
    77
            public void boardChanged(BoardEvent ev) {
jaroslav@255
    78
                Move m = ev.getMove();
jaroslav@255
    79
                if (m != null) {
jaroslav@255
    80
                    try {
jaroslav@255
    81
                        bp.setBoard(bp.getBoard().apply(m));
jaroslav@255
    82
                    } catch (IllegalPositionException ex) {
jaroslav@255
    83
                        // no new move
jaroslav@255
    84
                    }
jaroslav@255
    85
                }
jaroslav@255
    86
            }
jaroslav@255
    87
        }
jaroslav@255
    88
        bp.addBoardListener(new ApplyMove());
jaroslav@255
    89
        bp.setEditable(true);
jaroslav@255
    90
        f.add(bp);
jtulach@29
    91
        f.pack();
jtulach@29
    92
        f.setVisible(true);
jtulach@23
    93
    }
jtulach@23
    94
jaroslav@248
    95
    @Override
jaroslav@249
    96
    public Dimension getPreferredSize() {
jaroslav@249
    97
        return new Dimension(450, 450);
jaroslav@249
    98
    }
jaroslav@249
    99
jaroslav@249
   100
    @Override
jaroslav@248
   101
    protected void paintComponent(Graphics gg) {
jaroslav@249
   102
        assert EventQueue.isDispatchThread();
jaroslav@249
   103
        
jaroslav@249
   104
        fieldSize = Math.min(getSize().width, getSize().height) / 9;
jaroslav@248
   105
        int fifth = fieldSize / 10;
jaroslav@248
   106
        Graphics2D g = (Graphics2D) gg;
jaroslav@248
   107
        
jaroslav@249
   108
        xdelta = (getSize().width - fieldSize * 9) / 2;
jaroslav@249
   109
        ydelta = (getSize().height - fieldSize * 9) / 2;
jaroslav@248
   110
        
jaroslav@249
   111
        g.translate(xdelta, ydelta);
jaroslav@248
   112
jaroslav@248
   113
        g.setColor(Color.lightGray);
jaroslav@248
   114
        for (int i = 0; i < 9; i++) {
jaroslav@248
   115
            for (int j = 0; j < 9; j++) {
jaroslav@248
   116
                final Rectangle r = new Rectangle(i * fieldSize, j * fieldSize, fieldSize, fieldSize);
jaroslav@248
   117
                g.fillRect(r.x + fifth, r.y + fifth, r.width - fifth * 2, r.height - fifth * 2);
jtulach@18
   118
            }
jtulach@18
   119
        }
jtulach@18
   120
jtulach@18
   121
jaroslav@248
   122
        g.setColor(Color.BLACK);
jaroslav@248
   123
        for (Fence f : board.getFences()) {
jaroslav@248
   124
            int column = (f.getColumn() - 'A') + 1;
jaroslav@248
   125
            int row = 9 - f.getRow();
jaroslav@249
   126
            Orientation orient = f.getOrientation();
jaroslav@251
   127
            drawFence(orient, column, row, g, true);
jtulach@23
   128
        }
jtulach@23
   129
jtulach@29
   130
        int cnt = 0;
jaroslav@248
   131
        Color[] colors = { Color.WHITE, Color.BLACK, Color.ORANGE, Color.MAGENTA };
jaroslav@248
   132
        for (Player p : board.getPlayers()) {
jaroslav@248
   133
            int column = p.getColumn();
jaroslav@248
   134
            int row = 8 - p.getRow();
jaroslav@252
   135
            final boolean isCurrent = p == board.getCurrentPlayer();
jaroslav@252
   136
            final Color currentColor = colors[cnt];
jaroslav@253
   137
            drawPlayer(g, column, row, currentColor, isCurrent, false);
jaroslav@248
   138
            cnt++;
jaroslav@248
   139
        }
jtulach@29
   140
jaroslav@248
   141
        g.setColor(Color.BLACK);
jaroslav@248
   142
        final Font f = new Font("Monospace", Font.BOLD, fifth * 2);
jaroslav@248
   143
        g.setFont(f);
jaroslav@248
   144
        for (int i = 0; i < 8; i++) {
jaroslav@248
   145
            final char ch = (char) ('A' + i);
jaroslav@248
   146
            g.drawString(Character.toString(ch), i * fieldSize + fieldSize - fifth, fifth + f.getSize() - f.getBaselineFor(ch));
jaroslav@248
   147
        }
jaroslav@248
   148
        for (int i = 0; i < 8; i++) {
jaroslav@248
   149
            String s = "" + (8 - i);
jaroslav@248
   150
            g.drawString(s, fifth, i * fieldSize + fieldSize - fifth + f.getSize() - f.getBaselineFor(s.charAt(0)));
jtulach@29
   151
        }
jtulach@18
   152
    }
jtulach@18
   153
jaroslav@253
   154
    private void drawPlayer(Graphics2D g, int column, int row, final Color currentColor, final boolean isCurrent, boolean allowedMove) {
jaroslav@252
   155
        int fifth = fieldSize / 10;
jaroslav@252
   156
        int diametr = fieldSize - fifth * 4;
jaroslav@252
   157
        final Rectangle r = new Rectangle(
jaroslav@252
   158
            column * fieldSize + 2 * fifth,
jaroslav@252
   159
            row * fieldSize + 2 * fifth,
jaroslav@252
   160
            diametr,
jaroslav@252
   161
            diametr
jaroslav@252
   162
        );
jaroslav@252
   163
        if (currentColor == null) {
jaroslav@253
   164
            if (allowedMove) {
jaroslav@253
   165
                g.setColor(Color.BLACK);
jaroslav@253
   166
                g.drawOval(r.x, r.y, r.width, r.height);
jaroslav@253
   167
            } else {
jaroslav@253
   168
                g.drawLine(r.x, r.y, r.x + r.width, r.y + r.height);
jaroslav@253
   169
                g.drawLine(r.x, r.y + r.height, r.x + r.width, r.y);
jaroslav@253
   170
            }
jaroslav@252
   171
        } else {
jaroslav@252
   172
            g.setColor(currentColor);
jaroslav@252
   173
            g.fillOval(r.x, r.y, r.width, r.height);
jaroslav@252
   174
            if (isCurrent) {
jaroslav@252
   175
                g.setColor(Color.lightGray);
jaroslav@252
   176
                g.fillOval(r.x + r.width / 3, r.y + r.height / 3, r.width / 3, r.height / 3);
jaroslav@252
   177
            }
jaroslav@252
   178
        }
jaroslav@252
   179
    }
jaroslav@253
   180
    
jaroslav@251
   181
    private void drawFence(Orientation orient, int column, int row, Graphics2D g, boolean fill) throws IllegalStateException {
jaroslav@249
   182
        int fifth = fieldSize / 10;
jaroslav@249
   183
        int w, h;
jaroslav@249
   184
        switch (orient) {
jaroslav@249
   185
            case HORIZONTAL: w = fieldSize - fifth; h = fifth; break;
jaroslav@249
   186
            case VERTICAL: w = fifth; h = fieldSize - fifth; break;
jaroslav@249
   187
            default: throw new IllegalStateException();
jaroslav@249
   188
        }
jaroslav@249
   189
        Rectangle r = new Rectangle(
jaroslav@249
   190
            column * fieldSize - w,
jaroslav@249
   191
            row * fieldSize - h,
jaroslav@249
   192
            2 * w,
jaroslav@249
   193
            2 * h
jaroslav@249
   194
        );
jaroslav@251
   195
        if (fill) {
jaroslav@251
   196
            g.fill(r);
jaroslav@251
   197
        } else {
jaroslav@251
   198
            g.draw(r);
jaroslav@251
   199
        }
jaroslav@249
   200
    }
jaroslav@249
   201
jaroslav@249
   202
    public void mouseDragged(MouseEvent e) {
jaroslav@249
   203
    }
jaroslav@249
   204
jaroslav@249
   205
    public void mouseMoved(MouseEvent e) {
jaroslav@254
   206
        redrawCurrentMove(e);
jaroslav@254
   207
    }
jaroslav@254
   208
    
jaroslav@254
   209
    private Move redrawCurrentMove(MouseEvent e) {
jaroslav@249
   210
        int x = Math.round(((float)(e.getX() - xdelta)) / fieldSize);
jaroslav@249
   211
        int y = Math.round(((float)(e.getY() - ydelta)) / fieldSize);
jaroslav@249
   212
        if (x <= 0 || x >= 9) {
jaroslav@254
   213
            return null;
jaroslav@249
   214
        }
jaroslav@249
   215
        if (y <= 0 || y >= 9) {
jaroslav@254
   216
            return null;
jaroslav@249
   217
        }
jaroslav@252
   218
jaroslav@252
   219
        BufferedImage img = new BufferedImage(getWidth(), getHeight(), BufferedImage.TYPE_INT_ARGB);
jaroslav@252
   220
        final Graphics2D d2 = img.createGraphics();
jaroslav@252
   221
        d2.setBackground(getBackground());
jaroslav@252
   222
        d2.clearRect(0, 0, getWidth(), getHeight());
jaroslav@252
   223
        paintComponent(d2);
jaroslav@252
   224
        
jaroslav@249
   225
        
jaroslav@249
   226
        int dx = (e.getX() - xdelta) % fieldSize;
jaroslav@249
   227
        int dy = (e.getY() - ydelta) % fieldSize;
jaroslav@249
   228
        
jaroslav@249
   229
        int fifth = fieldSize / 10;
jaroslav@249
   230
        boolean outOfX = dx > fifth && dx < fieldSize - fifth;
jaroslav@249
   231
        boolean outOfY = dy > fifth && dy < fieldSize - fifth;
jaroslav@249
   232
        if (outOfX && outOfY) {
jaroslav@252
   233
            x = (e.getX() - xdelta) / fieldSize;
jaroslav@252
   234
            y = (e.getY() - ydelta) / fieldSize;
jaroslav@253
   235
            Move m = board.findMove(board.getCurrentPlayer(), x, 8 - y);
jaroslav@253
   236
            if (m == null) {
jaroslav@253
   237
                drawPlayer(d2, x, y, null, false, false);
jaroslav@253
   238
            } else {
jaroslav@253
   239
                drawPlayer(d2, x, y, null, false, true);
jaroslav@253
   240
            }
jaroslav@252
   241
            getGraphics().drawImage(img, 0, 0, null);
jaroslav@254
   242
            return m;
jaroslav@249
   243
        }
jaroslav@249
   244
        if (!outOfX && !outOfY) {
jaroslav@249
   245
            if (lastOrientation == null) {
jaroslav@254
   246
                return null;
jaroslav@249
   247
            }
jaroslav@249
   248
        } else {
jaroslav@249
   249
            if (outOfX) {
jaroslav@249
   250
                lastOrientation = Orientation.HORIZONTAL;
jaroslav@249
   251
            } else {
jaroslav@249
   252
                lastOrientation = Orientation.VERTICAL;
jaroslav@249
   253
            }
jaroslav@249
   254
        }
jaroslav@251
   255
        
jaroslav@254
   256
        Move m;
jaroslav@251
   257
        try {
jaroslav@254
   258
            m = Move.fence((char)('A' + x - 1), 9 - y, lastOrientation);
jaroslav@254
   259
            Board newBoard = board.apply(m);
jaroslav@251
   260
            drawFence(lastOrientation, x, y, d2, true);
jaroslav@251
   261
        } catch (IllegalPositionException ex) {
jaroslav@251
   262
            // can't place fence
jaroslav@251
   263
            drawFence(lastOrientation, x, y, d2, false);
jaroslav@254
   264
            m = null;
jaroslav@251
   265
        }
jaroslav@250
   266
        getGraphics().drawImage(img, 0, 0, null);
jaroslav@254
   267
        return m;
jaroslav@254
   268
    }
jaroslav@254
   269
jaroslav@254
   270
    public void mouseClicked(MouseEvent e) {
jaroslav@254
   271
    }
jaroslav@254
   272
jaroslav@254
   273
    public void mousePressed(MouseEvent e) {
jaroslav@254
   274
        Move m = redrawCurrentMove(e);
jaroslav@255
   275
        BoardPane bp = pane;
jaroslav@255
   276
        if (bp != null) {
jaroslav@255
   277
            bp.moveHappened(m);
jaroslav@254
   278
        }
jaroslav@254
   279
    }
jaroslav@254
   280
jaroslav@254
   281
    public void mouseReleased(MouseEvent e) {
jaroslav@254
   282
    }
jaroslav@254
   283
jaroslav@254
   284
    public void mouseEntered(MouseEvent e) {
jaroslav@254
   285
    }
jaroslav@254
   286
jaroslav@254
   287
    public void mouseExited(MouseEvent e) {
jaroslav@249
   288
    }
jaroslav@249
   289
jaroslav@255
   290
    final void setBoard(Board b) {
jaroslav@255
   291
        board = b;
jaroslav@255
   292
        if (isShowing()) {
jaroslav@255
   293
            repaint();
jaroslav@255
   294
        }
jaroslav@255
   295
    }
jaroslav@255
   296
jaroslav@255
   297
    final Board getBoard() {
jaroslav@255
   298
        return board;
jaroslav@255
   299
    }
jaroslav@255
   300
jaroslav@255
   301
    final void enableListeners(boolean editable) {
jaroslav@255
   302
        if (editable) {
jaroslav@255
   303
            addMouseMotionListener(this);
jaroslav@255
   304
            addMouseListener(this);
jaroslav@255
   305
        } else {
jaroslav@255
   306
            removeMouseListener(this);
jaroslav@255
   307
            removeMouseMotionListener(this);
jaroslav@255
   308
        }
jaroslav@255
   309
    }
jaroslav@255
   310
jaroslav@255
   311
    final void moveListener(BoardPane pane) {
jaroslav@255
   312
        this.pane = pane;
jaroslav@255
   313
    }
jaroslav@255
   314
jtulach@18
   315
}