# HG changeset patch # User Jaroslav Tulach # Date 1283638052 -7200 # Node ID 47d62a3afe639b581a98e613b085f2b2de9cc1cf # Parent 9bbf2502188607dee5882ef941bcd43bba2ad997 Preview of new fence position diff -r 9bbf25021886 -r 47d62a3afe63 visidor/src/main/java/cz/xelfi/quoridor/visidor/Viewer.java --- a/visidor/src/main/java/cz/xelfi/quoridor/visidor/Viewer.java Sat Sep 04 21:58:56 2010 +0200 +++ b/visidor/src/main/java/cz/xelfi/quoridor/visidor/Viewer.java Sun Sep 05 00:07:32 2010 +0200 @@ -32,10 +32,16 @@ import cz.xelfi.quoridor.Move; import cz.xelfi.quoridor.Player; import java.awt.Color; +import java.awt.Dimension; +import java.awt.EventQueue; import java.awt.Font; import java.awt.Graphics; import java.awt.Graphics2D; +import java.awt.GridLayout; import java.awt.Rectangle; +import java.awt.event.MouseEvent; +import java.awt.event.MouseMotionListener; +import javax.swing.JComponent; import javax.swing.JFrame; import javax.swing.JPanel; @@ -43,8 +49,12 @@ * * @author Jaroslav Tulach */ -final class Viewer extends JPanel { +final class Viewer extends JPanel implements MouseMotionListener { private Board board; + private int fieldSize; + private int xdelta; + private int ydelta; + private Orientation lastOrientation; public Viewer() { this(Board.empty()); @@ -52,6 +62,7 @@ private Viewer(Board b) { this.board = b; + addMouseMotionListener(this); } @@ -75,15 +86,22 @@ } @Override + public Dimension getPreferredSize() { + return new Dimension(450, 450); + } + + @Override protected void paintComponent(Graphics gg) { - int fieldSize = Math.min(getSize().width, getSize().height) / 9; + assert EventQueue.isDispatchThread(); + + fieldSize = Math.min(getSize().width, getSize().height) / 9; int fifth = fieldSize / 10; Graphics2D g = (Graphics2D) gg; - int xdelta = getSize().width - fieldSize * 9; - int ydelta = getSize().height - fieldSize * 9; + xdelta = (getSize().width - fieldSize * 9) / 2; + ydelta = (getSize().height - fieldSize * 9) / 2; - g.translate(xdelta / 2, ydelta / 2); + g.translate(xdelta, ydelta); g.setColor(Color.lightGray); for (int i = 0; i < 9; i++) { @@ -96,21 +114,10 @@ g.setColor(Color.BLACK); for (Fence f : board.getFences()) { - int w, h; - switch (f.getOrientation()) { - case HORIZONTAL: w = fieldSize - fifth; h = fifth; break; - case VERTICAL: w = fifth; h = fieldSize - fifth; break; - default: throw new IllegalStateException(); - } int column = (f.getColumn() - 'A') + 1; int row = 9 - f.getRow(); - Rectangle r = new Rectangle( - column * fieldSize - w, - row * fieldSize - h, - 2 * w, - 2 * h - ); - g.fill(r); + Orientation orient = f.getOrientation(); + drawFence(orient, column, row, g); } int cnt = 0; @@ -147,4 +154,63 @@ } } + private void drawFence(Orientation orient, int column, int row, Graphics2D g) throws IllegalStateException { + int fifth = fieldSize / 10; + int w, h; + switch (orient) { + case HORIZONTAL: w = fieldSize - fifth; h = fifth; break; + case VERTICAL: w = fifth; h = fieldSize - fifth; break; + default: throw new IllegalStateException(); + } + Rectangle r = new Rectangle( + column * fieldSize - w, + row * fieldSize - h, + 2 * w, + 2 * h + ); + g.fill(r); + } + + public void mouseDragged(MouseEvent e) { + } + + public void mouseMoved(MouseEvent e) { + final Graphics2D d2 = (Graphics2D) getGraphics(); + + 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) { + return; + } + if (y <= 0 || y >= 9) { + return; + } + + int dx = (e.getX() - xdelta) % fieldSize; + int dy = (e.getY() - ydelta) % fieldSize; + + int fifth = fieldSize / 10; + boolean outOfX = dx > fifth && dx < fieldSize - fifth; + boolean outOfY = dy > fifth && dy < fieldSize - fifth; + if (outOfX && outOfY) { + return; + } + if (!outOfX && !outOfY) { + if (lastOrientation == null) { + return; + } + } else { + if (outOfX) { + lastOrientation = Orientation.HORIZONTAL; + } else { + lastOrientation = Orientation.VERTICAL; + } + } + d2.translate(xdelta, ydelta); + drawFence(lastOrientation, x, y, d2); + } + }