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