Preview of new fence position
authorJaroslav Tulach <jaroslav.tulach@apidesign.org>
Sun, 05 Sep 2010 00:07:32 +0200
changeset 24947d62a3afe63
parent 248 9bbf25021886
child 250 eb6053abdd00
Preview of new fence position
visidor/src/main/java/cz/xelfi/quoridor/visidor/Viewer.java
     1.1 --- a/visidor/src/main/java/cz/xelfi/quoridor/visidor/Viewer.java	Sat Sep 04 21:58:56 2010 +0200
     1.2 +++ b/visidor/src/main/java/cz/xelfi/quoridor/visidor/Viewer.java	Sun Sep 05 00:07:32 2010 +0200
     1.3 @@ -32,10 +32,16 @@
     1.4  import cz.xelfi.quoridor.Move;
     1.5  import cz.xelfi.quoridor.Player;
     1.6  import java.awt.Color;
     1.7 +import java.awt.Dimension;
     1.8 +import java.awt.EventQueue;
     1.9  import java.awt.Font;
    1.10  import java.awt.Graphics;
    1.11  import java.awt.Graphics2D;
    1.12 +import java.awt.GridLayout;
    1.13  import java.awt.Rectangle;
    1.14 +import java.awt.event.MouseEvent;
    1.15 +import java.awt.event.MouseMotionListener;
    1.16 +import javax.swing.JComponent;
    1.17  import javax.swing.JFrame;
    1.18  import javax.swing.JPanel;
    1.19  
    1.20 @@ -43,8 +49,12 @@
    1.21   *
    1.22   * @author Jaroslav Tulach <jtulach@netbeans.org>
    1.23   */
    1.24 -final class Viewer extends JPanel {
    1.25 +final class Viewer extends JPanel implements MouseMotionListener {
    1.26      private Board board;
    1.27 +    private int fieldSize;
    1.28 +    private int xdelta;
    1.29 +    private int ydelta;
    1.30 +    private Orientation lastOrientation;
    1.31  
    1.32      public Viewer() {
    1.33          this(Board.empty());
    1.34 @@ -52,6 +62,7 @@
    1.35  
    1.36      private Viewer(Board b) {
    1.37          this.board = b;
    1.38 +        addMouseMotionListener(this);
    1.39      }
    1.40  
    1.41  
    1.42 @@ -75,15 +86,22 @@
    1.43      }
    1.44  
    1.45      @Override
    1.46 +    public Dimension getPreferredSize() {
    1.47 +        return new Dimension(450, 450);
    1.48 +    }
    1.49 +
    1.50 +    @Override
    1.51      protected void paintComponent(Graphics gg) {
    1.52 -        int fieldSize = Math.min(getSize().width, getSize().height) / 9;
    1.53 +        assert EventQueue.isDispatchThread();
    1.54 +        
    1.55 +        fieldSize = Math.min(getSize().width, getSize().height) / 9;
    1.56          int fifth = fieldSize / 10;
    1.57          Graphics2D g = (Graphics2D) gg;
    1.58          
    1.59 -        int xdelta = getSize().width - fieldSize * 9;
    1.60 -        int ydelta = getSize().height - fieldSize * 9;
    1.61 +        xdelta = (getSize().width - fieldSize * 9) / 2;
    1.62 +        ydelta = (getSize().height - fieldSize * 9) / 2;
    1.63          
    1.64 -        g.translate(xdelta / 2, ydelta / 2);
    1.65 +        g.translate(xdelta, ydelta);
    1.66  
    1.67          g.setColor(Color.lightGray);
    1.68          for (int i = 0; i < 9; i++) {
    1.69 @@ -96,21 +114,10 @@
    1.70  
    1.71          g.setColor(Color.BLACK);
    1.72          for (Fence f : board.getFences()) {
    1.73 -            int w, h;
    1.74 -            switch (f.getOrientation()) {
    1.75 -                case HORIZONTAL: w = fieldSize - fifth; h = fifth; break;
    1.76 -                case VERTICAL: w = fifth; h = fieldSize - fifth; break;
    1.77 -                default: throw new IllegalStateException();
    1.78 -            }
    1.79              int column = (f.getColumn() - 'A') + 1;
    1.80              int row = 9 - f.getRow();
    1.81 -            Rectangle r = new Rectangle(
    1.82 -                column * fieldSize - w,
    1.83 -                row * fieldSize - h,
    1.84 -                2 * w,
    1.85 -                2 * h
    1.86 -            );
    1.87 -            g.fill(r);
    1.88 +            Orientation orient = f.getOrientation();
    1.89 +            drawFence(orient, column, row, g);
    1.90          }
    1.91  
    1.92          int cnt = 0;
    1.93 @@ -147,4 +154,63 @@
    1.94          }
    1.95      }
    1.96  
    1.97 +    private void drawFence(Orientation orient, int column, int row, Graphics2D g) throws IllegalStateException {
    1.98 +        int fifth = fieldSize / 10;
    1.99 +        int w, h;
   1.100 +        switch (orient) {
   1.101 +            case HORIZONTAL: w = fieldSize - fifth; h = fifth; break;
   1.102 +            case VERTICAL: w = fifth; h = fieldSize - fifth; break;
   1.103 +            default: throw new IllegalStateException();
   1.104 +        }
   1.105 +        Rectangle r = new Rectangle(
   1.106 +            column * fieldSize - w,
   1.107 +            row * fieldSize - h,
   1.108 +            2 * w,
   1.109 +            2 * h
   1.110 +        );
   1.111 +        g.fill(r);
   1.112 +    }
   1.113 +
   1.114 +    public void mouseDragged(MouseEvent e) {
   1.115 +    }
   1.116 +
   1.117 +    public void mouseMoved(MouseEvent e) {
   1.118 +        final Graphics2D d2 = (Graphics2D) getGraphics();
   1.119 +        
   1.120 +        d2.clearRect(0, 0, getWidth(), getHeight());
   1.121 +        paintComponent(d2);
   1.122 +        
   1.123 +        int x = Math.round(((float)(e.getX() - xdelta)) / fieldSize);
   1.124 +        int y = Math.round(((float)(e.getY() - ydelta)) / fieldSize);
   1.125 +        if (x <= 0 || x >= 9) {
   1.126 +            return;
   1.127 +        }
   1.128 +        if (y <= 0 || y >= 9) {
   1.129 +            return;
   1.130 +        }
   1.131 +        
   1.132 +        int dx = (e.getX() - xdelta) % fieldSize;
   1.133 +        int dy = (e.getY() - ydelta) % fieldSize;
   1.134 +        
   1.135 +        int fifth = fieldSize / 10;
   1.136 +        boolean outOfX = dx > fifth && dx < fieldSize - fifth;
   1.137 +        boolean outOfY = dy > fifth && dy < fieldSize - fifth;
   1.138 +        if (outOfX && outOfY) {
   1.139 +            return;
   1.140 +        }
   1.141 +        if (!outOfX && !outOfY) {
   1.142 +            if (lastOrientation == null) {
   1.143 +                return;
   1.144 +            }
   1.145 +        } else {
   1.146 +            if (outOfX) {
   1.147 +                lastOrientation = Orientation.HORIZONTAL;
   1.148 +            } else {
   1.149 +                lastOrientation = Orientation.VERTICAL;
   1.150 +            }
   1.151 +        }
   1.152 +        d2.translate(xdelta, ydelta);
   1.153 +        drawFence(lastOrientation, x, y, d2);
   1.154 +    }
   1.155 +
   1.156  }