visidor/src/main/java/cz/xelfi/quoridor/visidor/Viewer.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Tue, 14 Sep 2010 08:56:13 +0200
changeset 264 d60370059c3c
parent 255 9ecd02d694cd
permissions -rw-r--r--
Changing headers to GPLv3
jtulach@18
     1
/*
jaroslav@264
     2
 * Quoridor server and related libraries
jaroslav@264
     3
 * Copyright (C) 2009-2010 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
jaroslav@178
     4
 *
jaroslav@264
     5
 * This program is free software: you can redistribute it and/or modify
jaroslav@264
     6
 * it under the terms of the GNU General Public License as published by
jaroslav@264
     7
 * the Free Software Foundation, either version 3 of the License.
jaroslav@178
     8
 *
jaroslav@264
     9
 * This program is distributed in the hope that it will be useful,
jaroslav@264
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
jaroslav@264
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
jaroslav@264
    12
 * GNU General Public License for more details.
jaroslav@178
    13
 *
jaroslav@264
    14
 * You should have received a copy of the GNU General Public License
jaroslav@264
    15
 * along with this program. Look for COPYING file in the top folder.
jaroslav@264
    16
 * If not, see http://www.gnu.org/licenses/.
jtulach@18
    17
 */
jtulach@25
    18
package cz.xelfi.quoridor.visidor;
jtulach@18
    19
jtulach@23
    20
import cz.xelfi.quoridor.Board;
jtulach@23
    21
import cz.xelfi.quoridor.Fence;
jtulach@23
    22
import cz.xelfi.quoridor.Fence.Orientation;
jtulach@23
    23
import cz.xelfi.quoridor.IllegalPositionException;
jtulach@23
    24
import cz.xelfi.quoridor.Move;
jtulach@29
    25
import cz.xelfi.quoridor.Player;
jtulach@18
    26
import java.awt.Color;
jaroslav@249
    27
import java.awt.Dimension;
jaroslav@249
    28
import java.awt.EventQueue;
jaroslav@248
    29
import java.awt.Font;
jaroslav@248
    30
import java.awt.Graphics;
jtulach@29
    31
import java.awt.Graphics2D;
jtulach@18
    32
import java.awt.Rectangle;
jaroslav@249
    33
import java.awt.event.MouseEvent;
jaroslav@254
    34
import java.awt.event.MouseListener;
jaroslav@249
    35
import java.awt.event.MouseMotionListener;
jaroslav@250
    36
import java.awt.image.BufferedImage;
jtulach@18
    37
import javax.swing.JFrame;
jtulach@29
    38
import javax.swing.JPanel;
jtulach@18
    39
jtulach@18
    40
/**
jtulach@18
    41
 *
jtulach@18
    42
 * @author Jaroslav Tulach <jtulach@netbeans.org>
jtulach@18
    43
 */
jaroslav@254
    44
final class Viewer extends JPanel implements MouseMotionListener, MouseListener {
jtulach@29
    45
    private Board board;
jaroslav@249
    46
    private int fieldSize;
jaroslav@249
    47
    private int xdelta;
jaroslav@249
    48
    private int ydelta;
jaroslav@249
    49
    private Orientation lastOrientation;
jaroslav@255
    50
    private BoardPane pane;
jtulach@29
    51
jtulach@29
    52
    public Viewer() {
jtulach@29
    53
        this(Board.empty());
jtulach@29
    54
    }
jtulach@29
    55
jtulach@29
    56
    private Viewer(Board b) {
jtulach@29
    57
        this.board = b;
jtulach@29
    58
    }
jtulach@29
    59
jtulach@18
    60
jtulach@18
    61
    /**
jtulach@18
    62
     * @param args the command line arguments
jtulach@18
    63
     */
jtulach@23
    64
    public static void main(String[] args) throws IllegalPositionException {
jtulach@29
    65
        JFrame f = new JFrame();
jtulach@29
    66
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jaroslav@255
    67
        final BoardPane bp = new BoardPane();
jaroslav@255
    68
        class ApplyMove implements BoardListener {
jaroslav@255
    69
            public void boardChanged(BoardEvent ev) {
jaroslav@255
    70
                Move m = ev.getMove();
jaroslav@255
    71
                if (m != null) {
jaroslav@255
    72
                    try {
jaroslav@255
    73
                        bp.setBoard(bp.getBoard().apply(m));
jaroslav@255
    74
                    } catch (IllegalPositionException ex) {
jaroslav@255
    75
                        // no new move
jaroslav@255
    76
                    }
jaroslav@255
    77
                }
jaroslav@255
    78
            }
jaroslav@255
    79
        }
jaroslav@255
    80
        bp.addBoardListener(new ApplyMove());
jaroslav@255
    81
        bp.setEditable(true);
jaroslav@255
    82
        f.add(bp);
jtulach@29
    83
        f.pack();
jtulach@29
    84
        f.setVisible(true);
jtulach@23
    85
    }
jtulach@23
    86
jaroslav@248
    87
    @Override
jaroslav@249
    88
    public Dimension getPreferredSize() {
jaroslav@249
    89
        return new Dimension(450, 450);
jaroslav@249
    90
    }
jaroslav@249
    91
jaroslav@249
    92
    @Override
jaroslav@248
    93
    protected void paintComponent(Graphics gg) {
jaroslav@249
    94
        assert EventQueue.isDispatchThread();
jaroslav@249
    95
        
jaroslav@249
    96
        fieldSize = Math.min(getSize().width, getSize().height) / 9;
jaroslav@248
    97
        int fifth = fieldSize / 10;
jaroslav@248
    98
        Graphics2D g = (Graphics2D) gg;
jaroslav@248
    99
        
jaroslav@249
   100
        xdelta = (getSize().width - fieldSize * 9) / 2;
jaroslav@249
   101
        ydelta = (getSize().height - fieldSize * 9) / 2;
jaroslav@248
   102
        
jaroslav@249
   103
        g.translate(xdelta, ydelta);
jaroslav@248
   104
jaroslav@248
   105
        g.setColor(Color.lightGray);
jaroslav@248
   106
        for (int i = 0; i < 9; i++) {
jaroslav@248
   107
            for (int j = 0; j < 9; j++) {
jaroslav@248
   108
                final Rectangle r = new Rectangle(i * fieldSize, j * fieldSize, fieldSize, fieldSize);
jaroslav@248
   109
                g.fillRect(r.x + fifth, r.y + fifth, r.width - fifth * 2, r.height - fifth * 2);
jtulach@18
   110
            }
jtulach@18
   111
        }
jtulach@18
   112
jtulach@18
   113
jaroslav@248
   114
        g.setColor(Color.BLACK);
jaroslav@248
   115
        for (Fence f : board.getFences()) {
jaroslav@248
   116
            int column = (f.getColumn() - 'A') + 1;
jaroslav@248
   117
            int row = 9 - f.getRow();
jaroslav@249
   118
            Orientation orient = f.getOrientation();
jaroslav@251
   119
            drawFence(orient, column, row, g, true);
jtulach@23
   120
        }
jtulach@23
   121
jtulach@29
   122
        int cnt = 0;
jaroslav@248
   123
        Color[] colors = { Color.WHITE, Color.BLACK, Color.ORANGE, Color.MAGENTA };
jaroslav@248
   124
        for (Player p : board.getPlayers()) {
jaroslav@248
   125
            int column = p.getColumn();
jaroslav@248
   126
            int row = 8 - p.getRow();
jaroslav@252
   127
            final boolean isCurrent = p == board.getCurrentPlayer();
jaroslav@252
   128
            final Color currentColor = colors[cnt];
jaroslav@253
   129
            drawPlayer(g, column, row, currentColor, isCurrent, false);
jaroslav@248
   130
            cnt++;
jaroslav@248
   131
        }
jtulach@29
   132
jaroslav@248
   133
        g.setColor(Color.BLACK);
jaroslav@248
   134
        final Font f = new Font("Monospace", Font.BOLD, fifth * 2);
jaroslav@248
   135
        g.setFont(f);
jaroslav@248
   136
        for (int i = 0; i < 8; i++) {
jaroslav@248
   137
            final char ch = (char) ('A' + i);
jaroslav@248
   138
            g.drawString(Character.toString(ch), i * fieldSize + fieldSize - fifth, fifth + f.getSize() - f.getBaselineFor(ch));
jaroslav@248
   139
        }
jaroslav@248
   140
        for (int i = 0; i < 8; i++) {
jaroslav@248
   141
            String s = "" + (8 - i);
jaroslav@248
   142
            g.drawString(s, fifth, i * fieldSize + fieldSize - fifth + f.getSize() - f.getBaselineFor(s.charAt(0)));
jtulach@29
   143
        }
jtulach@18
   144
    }
jtulach@18
   145
jaroslav@253
   146
    private void drawPlayer(Graphics2D g, int column, int row, final Color currentColor, final boolean isCurrent, boolean allowedMove) {
jaroslav@252
   147
        int fifth = fieldSize / 10;
jaroslav@252
   148
        int diametr = fieldSize - fifth * 4;
jaroslav@252
   149
        final Rectangle r = new Rectangle(
jaroslav@252
   150
            column * fieldSize + 2 * fifth,
jaroslav@252
   151
            row * fieldSize + 2 * fifth,
jaroslav@252
   152
            diametr,
jaroslav@252
   153
            diametr
jaroslav@252
   154
        );
jaroslav@252
   155
        if (currentColor == null) {
jaroslav@253
   156
            if (allowedMove) {
jaroslav@253
   157
                g.setColor(Color.BLACK);
jaroslav@253
   158
                g.drawOval(r.x, r.y, r.width, r.height);
jaroslav@253
   159
            } else {
jaroslav@253
   160
                g.drawLine(r.x, r.y, r.x + r.width, r.y + r.height);
jaroslav@253
   161
                g.drawLine(r.x, r.y + r.height, r.x + r.width, r.y);
jaroslav@253
   162
            }
jaroslav@252
   163
        } else {
jaroslav@252
   164
            g.setColor(currentColor);
jaroslav@252
   165
            g.fillOval(r.x, r.y, r.width, r.height);
jaroslav@252
   166
            if (isCurrent) {
jaroslav@252
   167
                g.setColor(Color.lightGray);
jaroslav@252
   168
                g.fillOval(r.x + r.width / 3, r.y + r.height / 3, r.width / 3, r.height / 3);
jaroslav@252
   169
            }
jaroslav@252
   170
        }
jaroslav@252
   171
    }
jaroslav@253
   172
    
jaroslav@251
   173
    private void drawFence(Orientation orient, int column, int row, Graphics2D g, boolean fill) throws IllegalStateException {
jaroslav@249
   174
        int fifth = fieldSize / 10;
jaroslav@249
   175
        int w, h;
jaroslav@249
   176
        switch (orient) {
jaroslav@249
   177
            case HORIZONTAL: w = fieldSize - fifth; h = fifth; break;
jaroslav@249
   178
            case VERTICAL: w = fifth; h = fieldSize - fifth; break;
jaroslav@249
   179
            default: throw new IllegalStateException();
jaroslav@249
   180
        }
jaroslav@249
   181
        Rectangle r = new Rectangle(
jaroslav@249
   182
            column * fieldSize - w,
jaroslav@249
   183
            row * fieldSize - h,
jaroslav@249
   184
            2 * w,
jaroslav@249
   185
            2 * h
jaroslav@249
   186
        );
jaroslav@251
   187
        if (fill) {
jaroslav@251
   188
            g.fill(r);
jaroslav@251
   189
        } else {
jaroslav@251
   190
            g.draw(r);
jaroslav@251
   191
        }
jaroslav@249
   192
    }
jaroslav@249
   193
jaroslav@249
   194
    public void mouseDragged(MouseEvent e) {
jaroslav@249
   195
    }
jaroslav@249
   196
jaroslav@249
   197
    public void mouseMoved(MouseEvent e) {
jaroslav@254
   198
        redrawCurrentMove(e);
jaroslav@254
   199
    }
jaroslav@254
   200
    
jaroslav@254
   201
    private Move redrawCurrentMove(MouseEvent e) {
jaroslav@249
   202
        int x = Math.round(((float)(e.getX() - xdelta)) / fieldSize);
jaroslav@249
   203
        int y = Math.round(((float)(e.getY() - ydelta)) / fieldSize);
jaroslav@249
   204
        if (x <= 0 || x >= 9) {
jaroslav@254
   205
            return null;
jaroslav@249
   206
        }
jaroslav@249
   207
        if (y <= 0 || y >= 9) {
jaroslav@254
   208
            return null;
jaroslav@249
   209
        }
jaroslav@252
   210
jaroslav@252
   211
        BufferedImage img = new BufferedImage(getWidth(), getHeight(), BufferedImage.TYPE_INT_ARGB);
jaroslav@252
   212
        final Graphics2D d2 = img.createGraphics();
jaroslav@252
   213
        d2.setBackground(getBackground());
jaroslav@252
   214
        d2.clearRect(0, 0, getWidth(), getHeight());
jaroslav@252
   215
        paintComponent(d2);
jaroslav@252
   216
        
jaroslav@249
   217
        
jaroslav@249
   218
        int dx = (e.getX() - xdelta) % fieldSize;
jaroslav@249
   219
        int dy = (e.getY() - ydelta) % fieldSize;
jaroslav@249
   220
        
jaroslav@249
   221
        int fifth = fieldSize / 10;
jaroslav@249
   222
        boolean outOfX = dx > fifth && dx < fieldSize - fifth;
jaroslav@249
   223
        boolean outOfY = dy > fifth && dy < fieldSize - fifth;
jaroslav@249
   224
        if (outOfX && outOfY) {
jaroslav@252
   225
            x = (e.getX() - xdelta) / fieldSize;
jaroslav@252
   226
            y = (e.getY() - ydelta) / fieldSize;
jaroslav@253
   227
            Move m = board.findMove(board.getCurrentPlayer(), x, 8 - y);
jaroslav@253
   228
            if (m == null) {
jaroslav@253
   229
                drawPlayer(d2, x, y, null, false, false);
jaroslav@253
   230
            } else {
jaroslav@253
   231
                drawPlayer(d2, x, y, null, false, true);
jaroslav@253
   232
            }
jaroslav@252
   233
            getGraphics().drawImage(img, 0, 0, null);
jaroslav@254
   234
            return m;
jaroslav@249
   235
        }
jaroslav@249
   236
        if (!outOfX && !outOfY) {
jaroslav@249
   237
            if (lastOrientation == null) {
jaroslav@254
   238
                return null;
jaroslav@249
   239
            }
jaroslav@249
   240
        } else {
jaroslav@249
   241
            if (outOfX) {
jaroslav@249
   242
                lastOrientation = Orientation.HORIZONTAL;
jaroslav@249
   243
            } else {
jaroslav@249
   244
                lastOrientation = Orientation.VERTICAL;
jaroslav@249
   245
            }
jaroslav@249
   246
        }
jaroslav@251
   247
        
jaroslav@254
   248
        Move m;
jaroslav@251
   249
        try {
jaroslav@254
   250
            m = Move.fence((char)('A' + x - 1), 9 - y, lastOrientation);
jaroslav@254
   251
            Board newBoard = board.apply(m);
jaroslav@251
   252
            drawFence(lastOrientation, x, y, d2, true);
jaroslav@251
   253
        } catch (IllegalPositionException ex) {
jaroslav@251
   254
            // can't place fence
jaroslav@251
   255
            drawFence(lastOrientation, x, y, d2, false);
jaroslav@254
   256
            m = null;
jaroslav@251
   257
        }
jaroslav@250
   258
        getGraphics().drawImage(img, 0, 0, null);
jaroslav@254
   259
        return m;
jaroslav@254
   260
    }
jaroslav@254
   261
jaroslav@254
   262
    public void mouseClicked(MouseEvent e) {
jaroslav@254
   263
    }
jaroslav@254
   264
jaroslav@254
   265
    public void mousePressed(MouseEvent e) {
jaroslav@254
   266
        Move m = redrawCurrentMove(e);
jaroslav@255
   267
        BoardPane bp = pane;
jaroslav@255
   268
        if (bp != null) {
jaroslav@255
   269
            bp.moveHappened(m);
jaroslav@254
   270
        }
jaroslav@254
   271
    }
jaroslav@254
   272
jaroslav@254
   273
    public void mouseReleased(MouseEvent e) {
jaroslav@254
   274
    }
jaroslav@254
   275
jaroslav@254
   276
    public void mouseEntered(MouseEvent e) {
jaroslav@254
   277
    }
jaroslav@254
   278
jaroslav@254
   279
    public void mouseExited(MouseEvent e) {
jaroslav@249
   280
    }
jaroslav@249
   281
jaroslav@255
   282
    final void setBoard(Board b) {
jaroslav@255
   283
        board = b;
jaroslav@255
   284
        if (isShowing()) {
jaroslav@255
   285
            repaint();
jaroslav@255
   286
        }
jaroslav@255
   287
    }
jaroslav@255
   288
jaroslav@255
   289
    final Board getBoard() {
jaroslav@255
   290
        return board;
jaroslav@255
   291
    }
jaroslav@255
   292
jaroslav@255
   293
    final void enableListeners(boolean editable) {
jaroslav@255
   294
        if (editable) {
jaroslav@255
   295
            addMouseMotionListener(this);
jaroslav@255
   296
            addMouseListener(this);
jaroslav@255
   297
        } else {
jaroslav@255
   298
            removeMouseListener(this);
jaroslav@255
   299
            removeMouseMotionListener(this);
jaroslav@255
   300
        }
jaroslav@255
   301
    }
jaroslav@255
   302
jaroslav@255
   303
    final void moveListener(BoardPane pane) {
jaroslav@255
   304
        this.pane = pane;
jaroslav@255
   305
    }
jaroslav@255
   306
jtulach@18
   307
}