Verify whether the fence can be placed at given position
authorJaroslav Tulach <jaroslav.tulach@apidesign.org>
Sun, 05 Sep 2010 06:52:49 +0200
changeset 2518b62bffb1a8f
parent 250 eb6053abdd00
child 252 2769784e86da
Verify whether the fence can be placed at given position
visidor/src/main/java/cz/xelfi/quoridor/visidor/Viewer.java
     1.1 --- a/visidor/src/main/java/cz/xelfi/quoridor/visidor/Viewer.java	Sun Sep 05 06:43:06 2010 +0200
     1.2 +++ b/visidor/src/main/java/cz/xelfi/quoridor/visidor/Viewer.java	Sun Sep 05 06:52:49 2010 +0200
     1.3 @@ -118,7 +118,7 @@
     1.4              int column = (f.getColumn() - 'A') + 1;
     1.5              int row = 9 - f.getRow();
     1.6              Orientation orient = f.getOrientation();
     1.7 -            drawFence(orient, column, row, g);
     1.8 +            drawFence(orient, column, row, g, true);
     1.9          }
    1.10  
    1.11          int cnt = 0;
    1.12 @@ -155,7 +155,7 @@
    1.13          }
    1.14      }
    1.15  
    1.16 -    private void drawFence(Orientation orient, int column, int row, Graphics2D g) throws IllegalStateException {
    1.17 +    private void drawFence(Orientation orient, int column, int row, Graphics2D g, boolean fill) throws IllegalStateException {
    1.18          int fifth = fieldSize / 10;
    1.19          int w, h;
    1.20          switch (orient) {
    1.21 @@ -169,20 +169,17 @@
    1.22              2 * w,
    1.23              2 * h
    1.24          );
    1.25 -        g.fill(r);
    1.26 +        if (fill) {
    1.27 +            g.fill(r);
    1.28 +        } else {
    1.29 +            g.draw(r);
    1.30 +        }
    1.31      }
    1.32  
    1.33      public void mouseDragged(MouseEvent e) {
    1.34      }
    1.35  
    1.36      public void mouseMoved(MouseEvent e) {
    1.37 -        BufferedImage img = new BufferedImage(getWidth(), getHeight(), BufferedImage.TYPE_INT_ARGB);
    1.38 -        final Graphics2D d2 = img.createGraphics();
    1.39 -        
    1.40 -        d2.setBackground(getBackground());
    1.41 -        d2.clearRect(0, 0, getWidth(), getHeight());
    1.42 -        paintComponent(d2);
    1.43 -        
    1.44          int x = Math.round(((float)(e.getX() - xdelta)) / fieldSize);
    1.45          int y = Math.round(((float)(e.getY() - ydelta)) / fieldSize);
    1.46          if (x <= 0 || x >= 9) {
    1.47 @@ -212,7 +209,21 @@
    1.48                  lastOrientation = Orientation.VERTICAL;
    1.49              }
    1.50          }
    1.51 -        drawFence(lastOrientation, x, y, d2);
    1.52 +        
    1.53 +        BufferedImage img = new BufferedImage(getWidth(), getHeight(), BufferedImage.TYPE_INT_ARGB);
    1.54 +        final Graphics2D d2 = img.createGraphics();
    1.55 +        d2.setBackground(getBackground());
    1.56 +        d2.clearRect(0, 0, getWidth(), getHeight());
    1.57 +        paintComponent(d2);
    1.58 +        
    1.59 +        try {
    1.60 +            Board newBoard = board.apply(Move.fence((char)('A' + x - 1), 9 - y, lastOrientation));
    1.61 +            drawFence(lastOrientation, x, y, d2, true);
    1.62 +        } catch (IllegalPositionException ex) {
    1.63 +            // can't place fence
    1.64 +            drawFence(lastOrientation, x, y, d2, false);
    1.65 +        }
    1.66 +        
    1.67          
    1.68          getGraphics().drawImage(img, 0, 0, null);
    1.69      }