1.1 --- a/visidor/src/main/java/cz/xelfi/quoridor/visidor/Viewer.java Sun Sep 05 11:04:26 2010 +0200
1.2 +++ b/visidor/src/main/java/cz/xelfi/quoridor/visidor/Viewer.java Sun Sep 05 17:58:45 2010 +0200
1.3 @@ -39,6 +39,7 @@
1.4 import java.awt.Graphics2D;
1.5 import java.awt.Rectangle;
1.6 import java.awt.event.MouseEvent;
1.7 +import java.awt.event.MouseListener;
1.8 import java.awt.event.MouseMotionListener;
1.9 import java.awt.image.BufferedImage;
1.10 import javax.swing.JFrame;
1.11 @@ -48,7 +49,7 @@
1.12 *
1.13 * @author Jaroslav Tulach <jtulach@netbeans.org>
1.14 */
1.15 -final class Viewer extends JPanel implements MouseMotionListener {
1.16 +final class Viewer extends JPanel implements MouseMotionListener, MouseListener {
1.17 private Board board;
1.18 private int fieldSize;
1.19 private int xdelta;
1.20 @@ -62,6 +63,7 @@
1.21 private Viewer(Board b) {
1.22 this.board = b;
1.23 addMouseMotionListener(this);
1.24 + addMouseListener(this);
1.25 }
1.26
1.27
1.28 @@ -70,13 +72,6 @@
1.29 */
1.30 public static void main(String[] args) throws IllegalPositionException {
1.31 Board b = Board.empty();
1.32 - for (int i = 1; i <= 8; i++) {
1.33 - b = b.apply(Move.fence('A', i, Orientation.values()[i % 2]));
1.34 - }
1.35 - for (int i = 1; i <= 8; i++) {
1.36 - b = b.apply(Move.fence('H', i, Orientation.values()[(i + 1) % 2]));
1.37 - }
1.38 -
1.39 JFrame f = new JFrame();
1.40 f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
1.41 f.add(new Viewer(b));
1.42 @@ -195,13 +190,17 @@
1.43 }
1.44
1.45 public void mouseMoved(MouseEvent e) {
1.46 + redrawCurrentMove(e);
1.47 + }
1.48 +
1.49 + private Move redrawCurrentMove(MouseEvent e) {
1.50 int x = Math.round(((float)(e.getX() - xdelta)) / fieldSize);
1.51 int y = Math.round(((float)(e.getY() - ydelta)) / fieldSize);
1.52 if (x <= 0 || x >= 9) {
1.53 - return;
1.54 + return null;
1.55 }
1.56 if (y <= 0 || y >= 9) {
1.57 - return;
1.58 + return null;
1.59 }
1.60
1.61 BufferedImage img = new BufferedImage(getWidth(), getHeight(), BufferedImage.TYPE_INT_ARGB);
1.62 @@ -227,11 +226,11 @@
1.63 drawPlayer(d2, x, y, null, false, true);
1.64 }
1.65 getGraphics().drawImage(img, 0, 0, null);
1.66 - return;
1.67 + return m;
1.68 }
1.69 if (!outOfX && !outOfY) {
1.70 if (lastOrientation == null) {
1.71 - return;
1.72 + return null;
1.73 }
1.74 } else {
1.75 if (outOfX) {
1.76 @@ -241,16 +240,42 @@
1.77 }
1.78 }
1.79
1.80 + Move m;
1.81 try {
1.82 - Board newBoard = board.apply(Move.fence((char)('A' + x - 1), 9 - y, lastOrientation));
1.83 + m = Move.fence((char)('A' + x - 1), 9 - y, lastOrientation);
1.84 + Board newBoard = board.apply(m);
1.85 drawFence(lastOrientation, x, y, d2, true);
1.86 } catch (IllegalPositionException ex) {
1.87 // can't place fence
1.88 drawFence(lastOrientation, x, y, d2, false);
1.89 + m = null;
1.90 }
1.91 -
1.92 -
1.93 getGraphics().drawImage(img, 0, 0, null);
1.94 + return m;
1.95 + }
1.96 +
1.97 + public void mouseClicked(MouseEvent e) {
1.98 + }
1.99 +
1.100 + public void mousePressed(MouseEvent e) {
1.101 + Move m = redrawCurrentMove(e);
1.102 + if (m != null) {
1.103 + try {
1.104 + board = board.apply(m);
1.105 + } catch (IllegalPositionException ex) {
1.106 + // no new move
1.107 + }
1.108 + repaint();
1.109 + }
1.110 + }
1.111 +
1.112 + public void mouseReleased(MouseEvent e) {
1.113 + }
1.114 +
1.115 + public void mouseEntered(MouseEvent e) {
1.116 + }
1.117 +
1.118 + public void mouseExited(MouseEvent e) {
1.119 }
1.120
1.121 }