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