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