# HG changeset patch # User Jaroslav Tulach # Date 1283662369 -7200 # Node ID 8b62bffb1a8f50669551022b397b91c17d3177d7 # Parent eb6053abdd000697c745f044fe1d1177144f2345 Verify whether the fence can be placed at given position diff -r eb6053abdd00 -r 8b62bffb1a8f 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:43:06 2010 +0200 +++ b/visidor/src/main/java/cz/xelfi/quoridor/visidor/Viewer.java Sun Sep 05 06:52:49 2010 +0200 @@ -118,7 +118,7 @@ int column = (f.getColumn() - 'A') + 1; int row = 9 - f.getRow(); Orientation orient = f.getOrientation(); - drawFence(orient, column, row, g); + drawFence(orient, column, row, g, true); } int cnt = 0; @@ -155,7 +155,7 @@ } } - private void drawFence(Orientation orient, int column, int row, Graphics2D g) throws IllegalStateException { + private void drawFence(Orientation orient, int column, int row, Graphics2D g, boolean fill) throws IllegalStateException { int fifth = fieldSize / 10; int w, h; switch (orient) { @@ -169,20 +169,17 @@ 2 * w, 2 * h ); - g.fill(r); + if (fill) { + g.fill(r); + } else { + g.draw(r); + } } public void mouseDragged(MouseEvent e) { } public void mouseMoved(MouseEvent e) { - 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 x = Math.round(((float)(e.getX() - xdelta)) / fieldSize); int y = Math.round(((float)(e.getY() - ydelta)) / fieldSize); if (x <= 0 || x >= 9) { @@ -212,7 +209,21 @@ lastOrientation = Orientation.VERTICAL; } } - drawFence(lastOrientation, x, y, d2); + + 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); + } catch (IllegalPositionException ex) { + // can't place fence + drawFence(lastOrientation, x, y, d2, false); + } + getGraphics().drawImage(img, 0, 0, null); }