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