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