Drawing position of the pawn
authorJaroslav Tulach <jaroslav.tulach@apidesign.org>
Sun, 05 Sep 2010 07:07:35 +0200
changeset 2522769784e86da
parent 251 8b62bffb1a8f
child 253 ee02205edf13
Drawing position of the pawn
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:52:49 2010 +0200
     1.2 +++ b/visidor/src/main/java/cz/xelfi/quoridor/visidor/Viewer.java	Sun Sep 05 07:07:35 2010 +0200
     1.3 @@ -123,22 +123,12 @@
     1.4  
     1.5          int cnt = 0;
     1.6          Color[] colors = { Color.WHITE, Color.BLACK, Color.ORANGE, Color.MAGENTA };
     1.7 -        int diametr = fieldSize - fifth * 4;
     1.8          for (Player p : board.getPlayers()) {
     1.9              int column = p.getColumn();
    1.10              int row = 8 - p.getRow();
    1.11 -            final Rectangle r = new Rectangle(
    1.12 -                column * fieldSize + 2 * fifth,
    1.13 -                row * fieldSize + 2 * fifth,
    1.14 -                diametr,
    1.15 -                diametr
    1.16 -            );
    1.17 -            g.setColor(colors[cnt]);
    1.18 -            g.fillOval(r.x, r.y, r.width, r.height);
    1.19 -            if (p == board.getCurrentPlayer()) {
    1.20 -                g.setColor(Color.lightGray);
    1.21 -                g.fillOval(r.x + r.width / 3, r.y + r.height / 3, r.width / 3, r.height / 3);
    1.22 -            }
    1.23 +            final boolean isCurrent = p == board.getCurrentPlayer();
    1.24 +            final Color currentColor = colors[cnt];
    1.25 +            drawPlayer(g, column, row, currentColor, isCurrent);
    1.26              cnt++;
    1.27          }
    1.28  
    1.29 @@ -155,6 +145,28 @@
    1.30          }
    1.31      }
    1.32  
    1.33 +    private void drawPlayer(Graphics2D g, int column, int row, final Color currentColor, final boolean isCurrent) {
    1.34 +        int fifth = fieldSize / 10;
    1.35 +        int diametr = fieldSize - fifth * 4;
    1.36 +        final Rectangle r = new Rectangle(
    1.37 +            column * fieldSize + 2 * fifth,
    1.38 +            row * fieldSize + 2 * fifth,
    1.39 +            diametr,
    1.40 +            diametr
    1.41 +        );
    1.42 +        if (currentColor == null) {
    1.43 +            g.setColor(Color.BLACK);
    1.44 +            g.drawOval(r.x, r.y, r.width, r.height);
    1.45 +        } else {
    1.46 +            g.setColor(currentColor);
    1.47 +            g.fillOval(r.x, r.y, r.width, r.height);
    1.48 +            if (isCurrent) {
    1.49 +                g.setColor(Color.lightGray);
    1.50 +                g.fillOval(r.x + r.width / 3, r.y + r.height / 3, r.width / 3, r.height / 3);
    1.51 +            }
    1.52 +        }
    1.53 +    }
    1.54 +
    1.55      private void drawFence(Orientation orient, int column, int row, Graphics2D g, boolean fill) throws IllegalStateException {
    1.56          int fifth = fieldSize / 10;
    1.57          int w, h;
    1.58 @@ -188,6 +200,13 @@
    1.59          if (y <= 0 || y >= 9) {
    1.60              return;
    1.61          }
    1.62 +
    1.63 +        BufferedImage img = new BufferedImage(getWidth(), getHeight(), BufferedImage.TYPE_INT_ARGB);
    1.64 +        final Graphics2D d2 = img.createGraphics();
    1.65 +        d2.setBackground(getBackground());
    1.66 +        d2.clearRect(0, 0, getWidth(), getHeight());
    1.67 +        paintComponent(d2);
    1.68 +        
    1.69          
    1.70          int dx = (e.getX() - xdelta) % fieldSize;
    1.71          int dy = (e.getY() - ydelta) % fieldSize;
    1.72 @@ -196,6 +215,10 @@
    1.73          boolean outOfX = dx > fifth && dx < fieldSize - fifth;
    1.74          boolean outOfY = dy > fifth && dy < fieldSize - fifth;
    1.75          if (outOfX && outOfY) {
    1.76 +            x = (e.getX() - xdelta) / fieldSize;
    1.77 +            y = (e.getY() - ydelta) / fieldSize;
    1.78 +            drawPlayer(d2, x, y, null, false);
    1.79 +            getGraphics().drawImage(img, 0, 0, null);
    1.80              return;
    1.81          }
    1.82          if (!outOfX && !outOfY) {
    1.83 @@ -210,12 +233,6 @@
    1.84              }
    1.85          }
    1.86          
    1.87 -        BufferedImage img = new BufferedImage(getWidth(), getHeight(), BufferedImage.TYPE_INT_ARGB);
    1.88 -        final Graphics2D d2 = img.createGraphics();
    1.89 -        d2.setBackground(getBackground());
    1.90 -        d2.clearRect(0, 0, getWidth(), getHeight());
    1.91 -        paintComponent(d2);
    1.92 -        
    1.93          try {
    1.94              Board newBoard = board.apply(Move.fence((char)('A' + x - 1), 9 - y, lastOrientation));
    1.95              drawFence(lastOrientation, x, y, d2, true);