# HG changeset patch # User Jaroslav Tulach # Date 1283702325 -7200 # Node ID 1273bfb0e2e7e76988022be5442c07e0223fcf99 # Parent ee02205edf13b61953252c9107bb7ef200f634c2 Interactive game suport diff -r ee02205edf13 -r 1273bfb0e2e7 visidor/src/main/java/cz/xelfi/quoridor/visidor/Viewer.java --- a/visidor/src/main/java/cz/xelfi/quoridor/visidor/Viewer.java Sun Sep 05 11:04:26 2010 +0200 +++ b/visidor/src/main/java/cz/xelfi/quoridor/visidor/Viewer.java Sun Sep 05 17:58:45 2010 +0200 @@ -39,6 +39,7 @@ import java.awt.Graphics2D; import java.awt.Rectangle; import java.awt.event.MouseEvent; +import java.awt.event.MouseListener; import java.awt.event.MouseMotionListener; import java.awt.image.BufferedImage; import javax.swing.JFrame; @@ -48,7 +49,7 @@ * * @author Jaroslav Tulach */ -final class Viewer extends JPanel implements MouseMotionListener { +final class Viewer extends JPanel implements MouseMotionListener, MouseListener { private Board board; private int fieldSize; private int xdelta; @@ -62,6 +63,7 @@ private Viewer(Board b) { this.board = b; addMouseMotionListener(this); + addMouseListener(this); } @@ -70,13 +72,6 @@ */ public static void main(String[] args) throws IllegalPositionException { Board b = Board.empty(); - for (int i = 1; i <= 8; i++) { - b = b.apply(Move.fence('A', i, Orientation.values()[i % 2])); - } - for (int i = 1; i <= 8; i++) { - b = b.apply(Move.fence('H', i, Orientation.values()[(i + 1) % 2])); - } - JFrame f = new JFrame(); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.add(new Viewer(b)); @@ -195,13 +190,17 @@ } public void mouseMoved(MouseEvent e) { + redrawCurrentMove(e); + } + + private Move redrawCurrentMove(MouseEvent e) { int x = Math.round(((float)(e.getX() - xdelta)) / fieldSize); int y = Math.round(((float)(e.getY() - ydelta)) / fieldSize); if (x <= 0 || x >= 9) { - return; + return null; } if (y <= 0 || y >= 9) { - return; + return null; } BufferedImage img = new BufferedImage(getWidth(), getHeight(), BufferedImage.TYPE_INT_ARGB); @@ -227,11 +226,11 @@ drawPlayer(d2, x, y, null, false, true); } getGraphics().drawImage(img, 0, 0, null); - return; + return m; } if (!outOfX && !outOfY) { if (lastOrientation == null) { - return; + return null; } } else { if (outOfX) { @@ -241,16 +240,42 @@ } } + Move m; try { - Board newBoard = board.apply(Move.fence((char)('A' + x - 1), 9 - y, lastOrientation)); + m = Move.fence((char)('A' + x - 1), 9 - y, lastOrientation); + Board newBoard = board.apply(m); drawFence(lastOrientation, x, y, d2, true); } catch (IllegalPositionException ex) { // can't place fence drawFence(lastOrientation, x, y, d2, false); + m = null; } - - getGraphics().drawImage(img, 0, 0, null); + return m; + } + + public void mouseClicked(MouseEvent e) { + } + + public void mousePressed(MouseEvent e) { + Move m = redrawCurrentMove(e); + if (m != null) { + try { + board = board.apply(m); + } catch (IllegalPositionException ex) { + // no new move + } + repaint(); + } + } + + public void mouseReleased(MouseEvent e) { + } + + public void mouseEntered(MouseEvent e) { + } + + public void mouseExited(MouseEvent e) { } }