visidor/src/main/java/cz/xelfi/quoridor/visidor/Viewer.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Tue, 14 Sep 2010 08:56:13 +0200
changeset 264 d60370059c3c
parent 255 9ecd02d694cd
permissions -rw-r--r--
Changing headers to GPLv3
     1 /*
     2  * Quoridor server and related libraries
     3  * Copyright (C) 2009-2010 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
     4  *
     5  * This program is free software: you can redistribute it and/or modify
     6  * it under the terms of the GNU General Public License as published by
     7  * the Free Software Foundation, either version 3 of the License.
     8  *
     9  * This program is distributed in the hope that it will be useful,
    10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    12  * GNU General Public License for more details.
    13  *
    14  * You should have received a copy of the GNU General Public License
    15  * along with this program. Look for COPYING file in the top folder.
    16  * If not, see http://www.gnu.org/licenses/.
    17  */
    18 package cz.xelfi.quoridor.visidor;
    19 
    20 import cz.xelfi.quoridor.Board;
    21 import cz.xelfi.quoridor.Fence;
    22 import cz.xelfi.quoridor.Fence.Orientation;
    23 import cz.xelfi.quoridor.IllegalPositionException;
    24 import cz.xelfi.quoridor.Move;
    25 import cz.xelfi.quoridor.Player;
    26 import java.awt.Color;
    27 import java.awt.Dimension;
    28 import java.awt.EventQueue;
    29 import java.awt.Font;
    30 import java.awt.Graphics;
    31 import java.awt.Graphics2D;
    32 import java.awt.Rectangle;
    33 import java.awt.event.MouseEvent;
    34 import java.awt.event.MouseListener;
    35 import java.awt.event.MouseMotionListener;
    36 import java.awt.image.BufferedImage;
    37 import javax.swing.JFrame;
    38 import javax.swing.JPanel;
    39 
    40 /**
    41  *
    42  * @author Jaroslav Tulach <jtulach@netbeans.org>
    43  */
    44 final class Viewer extends JPanel implements MouseMotionListener, MouseListener {
    45     private Board board;
    46     private int fieldSize;
    47     private int xdelta;
    48     private int ydelta;
    49     private Orientation lastOrientation;
    50     private BoardPane pane;
    51 
    52     public Viewer() {
    53         this(Board.empty());
    54     }
    55 
    56     private Viewer(Board b) {
    57         this.board = b;
    58     }
    59 
    60 
    61     /**
    62      * @param args the command line arguments
    63      */
    64     public static void main(String[] args) throws IllegalPositionException {
    65         JFrame f = new JFrame();
    66         f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    67         final BoardPane bp = new BoardPane();
    68         class ApplyMove implements BoardListener {
    69             public void boardChanged(BoardEvent ev) {
    70                 Move m = ev.getMove();
    71                 if (m != null) {
    72                     try {
    73                         bp.setBoard(bp.getBoard().apply(m));
    74                     } catch (IllegalPositionException ex) {
    75                         // no new move
    76                     }
    77                 }
    78             }
    79         }
    80         bp.addBoardListener(new ApplyMove());
    81         bp.setEditable(true);
    82         f.add(bp);
    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         redrawCurrentMove(e);
   199     }
   200     
   201     private Move redrawCurrentMove(MouseEvent e) {
   202         int x = Math.round(((float)(e.getX() - xdelta)) / fieldSize);
   203         int y = Math.round(((float)(e.getY() - ydelta)) / fieldSize);
   204         if (x <= 0 || x >= 9) {
   205             return null;
   206         }
   207         if (y <= 0 || y >= 9) {
   208             return null;
   209         }
   210 
   211         BufferedImage img = new BufferedImage(getWidth(), getHeight(), BufferedImage.TYPE_INT_ARGB);
   212         final Graphics2D d2 = img.createGraphics();
   213         d2.setBackground(getBackground());
   214         d2.clearRect(0, 0, getWidth(), getHeight());
   215         paintComponent(d2);
   216         
   217         
   218         int dx = (e.getX() - xdelta) % fieldSize;
   219         int dy = (e.getY() - ydelta) % fieldSize;
   220         
   221         int fifth = fieldSize / 10;
   222         boolean outOfX = dx > fifth && dx < fieldSize - fifth;
   223         boolean outOfY = dy > fifth && dy < fieldSize - fifth;
   224         if (outOfX && outOfY) {
   225             x = (e.getX() - xdelta) / fieldSize;
   226             y = (e.getY() - ydelta) / fieldSize;
   227             Move m = board.findMove(board.getCurrentPlayer(), x, 8 - y);
   228             if (m == null) {
   229                 drawPlayer(d2, x, y, null, false, false);
   230             } else {
   231                 drawPlayer(d2, x, y, null, false, true);
   232             }
   233             getGraphics().drawImage(img, 0, 0, null);
   234             return m;
   235         }
   236         if (!outOfX && !outOfY) {
   237             if (lastOrientation == null) {
   238                 return null;
   239             }
   240         } else {
   241             if (outOfX) {
   242                 lastOrientation = Orientation.HORIZONTAL;
   243             } else {
   244                 lastOrientation = Orientation.VERTICAL;
   245             }
   246         }
   247         
   248         Move m;
   249         try {
   250             m = Move.fence((char)('A' + x - 1), 9 - y, lastOrientation);
   251             Board newBoard = board.apply(m);
   252             drawFence(lastOrientation, x, y, d2, true);
   253         } catch (IllegalPositionException ex) {
   254             // can't place fence
   255             drawFence(lastOrientation, x, y, d2, false);
   256             m = null;
   257         }
   258         getGraphics().drawImage(img, 0, 0, null);
   259         return m;
   260     }
   261 
   262     public void mouseClicked(MouseEvent e) {
   263     }
   264 
   265     public void mousePressed(MouseEvent e) {
   266         Move m = redrawCurrentMove(e);
   267         BoardPane bp = pane;
   268         if (bp != null) {
   269             bp.moveHappened(m);
   270         }
   271     }
   272 
   273     public void mouseReleased(MouseEvent e) {
   274     }
   275 
   276     public void mouseEntered(MouseEvent e) {
   277     }
   278 
   279     public void mouseExited(MouseEvent e) {
   280     }
   281 
   282     final void setBoard(Board b) {
   283         board = b;
   284         if (isShowing()) {
   285             repaint();
   286         }
   287     }
   288 
   289     final Board getBoard() {
   290         return board;
   291     }
   292 
   293     final void enableListeners(boolean editable) {
   294         if (editable) {
   295             addMouseMotionListener(this);
   296             addMouseListener(this);
   297         } else {
   298             removeMouseListener(this);
   299             removeMouseMotionListener(this);
   300         }
   301     }
   302 
   303     final void moveListener(BoardPane pane) {
   304         this.pane = pane;
   305     }
   306 
   307 }