# HG changeset patch # User Jaroslav Tulach # Date 1283663255 -7200 # Node ID 2769784e86dae1cdc53fe3fc8f24262a12831b9c # Parent 8b62bffb1a8f50669551022b397b91c17d3177d7 Drawing position of the pawn diff -r 8b62bffb1a8f -r 2769784e86da visidor/src/main/java/cz/xelfi/quoridor/visidor/Viewer.java --- a/visidor/src/main/java/cz/xelfi/quoridor/visidor/Viewer.java Sun Sep 05 06:52:49 2010 +0200 +++ b/visidor/src/main/java/cz/xelfi/quoridor/visidor/Viewer.java Sun Sep 05 07:07:35 2010 +0200 @@ -123,22 +123,12 @@ int cnt = 0; Color[] colors = { Color.WHITE, Color.BLACK, Color.ORANGE, Color.MAGENTA }; - int diametr = fieldSize - fifth * 4; for (Player p : board.getPlayers()) { int column = p.getColumn(); int row = 8 - p.getRow(); - final Rectangle r = new Rectangle( - column * fieldSize + 2 * fifth, - row * fieldSize + 2 * fifth, - diametr, - diametr - ); - g.setColor(colors[cnt]); - g.fillOval(r.x, r.y, r.width, r.height); - if (p == board.getCurrentPlayer()) { - g.setColor(Color.lightGray); - g.fillOval(r.x + r.width / 3, r.y + r.height / 3, r.width / 3, r.height / 3); - } + final boolean isCurrent = p == board.getCurrentPlayer(); + final Color currentColor = colors[cnt]; + drawPlayer(g, column, row, currentColor, isCurrent); cnt++; } @@ -155,6 +145,28 @@ } } + private void drawPlayer(Graphics2D g, int column, int row, final Color currentColor, final boolean isCurrent) { + int fifth = fieldSize / 10; + int diametr = fieldSize - fifth * 4; + final Rectangle r = new Rectangle( + column * fieldSize + 2 * fifth, + row * fieldSize + 2 * fifth, + diametr, + diametr + ); + if (currentColor == null) { + g.setColor(Color.BLACK); + g.drawOval(r.x, r.y, r.width, r.height); + } else { + g.setColor(currentColor); + g.fillOval(r.x, r.y, r.width, r.height); + if (isCurrent) { + g.setColor(Color.lightGray); + g.fillOval(r.x + r.width / 3, r.y + r.height / 3, r.width / 3, r.height / 3); + } + } + } + private void drawFence(Orientation orient, int column, int row, Graphics2D g, boolean fill) throws IllegalStateException { int fifth = fieldSize / 10; int w, h; @@ -188,6 +200,13 @@ if (y <= 0 || y >= 9) { return; } + + BufferedImage img = new BufferedImage(getWidth(), getHeight(), BufferedImage.TYPE_INT_ARGB); + final Graphics2D d2 = img.createGraphics(); + d2.setBackground(getBackground()); + d2.clearRect(0, 0, getWidth(), getHeight()); + paintComponent(d2); + int dx = (e.getX() - xdelta) % fieldSize; int dy = (e.getY() - ydelta) % fieldSize; @@ -196,6 +215,10 @@ boolean outOfX = dx > fifth && dx < fieldSize - fifth; boolean outOfY = dy > fifth && dy < fieldSize - fifth; if (outOfX && outOfY) { + x = (e.getX() - xdelta) / fieldSize; + y = (e.getY() - ydelta) / fieldSize; + drawPlayer(d2, x, y, null, false); + getGraphics().drawImage(img, 0, 0, null); return; } if (!outOfX && !outOfY) { @@ -210,12 +233,6 @@ } } - BufferedImage img = new BufferedImage(getWidth(), getHeight(), BufferedImage.TYPE_INT_ARGB); - final Graphics2D d2 = img.createGraphics(); - d2.setBackground(getBackground()); - d2.clearRect(0, 0, getWidth(), getHeight()); - paintComponent(d2); - try { Board newBoard = board.apply(Move.fence((char)('A' + x - 1), 9 - y, lastOrientation)); drawFence(lastOrientation, x, y, d2, true);