# HG changeset patch # User Jaroslav Tulach # Date 1283630336 -7200 # Node ID 9bbf2502188607dee5882ef941bcd43bba2ad997 # Parent 57bd8b561a2e039eac7f8044a33d785ea065856c Using Java2D to display the board diff -r 57bd8b561a2e -r 9bbf25021886 visidor/pom.xml --- a/visidor/pom.xml Sat Sep 04 19:08:55 2010 +0200 +++ b/visidor/pom.xml Sat Sep 04 21:58:56 2010 +0200 @@ -37,11 +37,6 @@ quoridor ${quoridorVersion} - - org.netbeans.api - org-netbeans-api-visual - RELEASE65 - diff -r 57bd8b561a2e -r 9bbf25021886 visidor/src/main/java/cz/xelfi/quoridor/visidor/Viewer.java --- a/visidor/src/main/java/cz/xelfi/quoridor/visidor/Viewer.java Sat Sep 04 19:08:55 2010 +0200 +++ b/visidor/src/main/java/cz/xelfi/quoridor/visidor/Viewer.java Sat Sep 04 21:58:56 2010 +0200 @@ -32,22 +32,12 @@ import cz.xelfi.quoridor.Move; import cz.xelfi.quoridor.Player; import java.awt.Color; +import java.awt.Font; +import java.awt.Graphics; import java.awt.Graphics2D; -import java.awt.Point; import java.awt.Rectangle; import javax.swing.JFrame; import javax.swing.JPanel; -import org.netbeans.api.visual.action.ActionFactory; -import org.netbeans.api.visual.action.MoveProvider; -import org.netbeans.api.visual.anchor.AnchorFactory; -import org.netbeans.api.visual.border.BorderFactory; -import org.netbeans.api.visual.widget.ConnectionWidget; -import org.netbeans.api.visual.widget.ImageWidget; -import org.netbeans.api.visual.widget.LabelWidget; -import org.netbeans.api.visual.widget.LayerWidget; -import org.netbeans.api.visual.widget.Scene; -import org.netbeans.api.visual.widget.Widget; -import org.openide.util.Exceptions; /** * @@ -55,7 +45,6 @@ */ final class Viewer extends JPanel { private Board board; - private final Scene scene; public Viewer() { this(Board.empty()); @@ -63,9 +52,6 @@ private Viewer(Board b) { this.board = b; - this.scene = new Scene(); - add(scene.createView()); - view(scene, board); } @@ -88,211 +74,77 @@ f.setVisible(true); } - private void view(Scene scene, Board b) { - scene.removeChildren(); -// Scene layerBoard = scene; - final LayerWidget layerLines = new LayerWidget(scene); - scene.addChild(layerLines); - final LayerWidget layerBoard = new LayerWidget(scene); - layerLines.addChild(layerBoard); - final int WDTH = 50; - final int HGHT = 50; -// conn.setSourceAnchor(ahoj); - Widget[][] fields = new Widget[10][]; - for (int i = 0; i < fields.length; i++) { - fields[i] = new Widget[10]; - for (int j = 0; j < fields[i].length; j++) { - LabelWidget w = new LabelWidget(scene); - w.setPreferredBounds(new Rectangle(i * WDTH, j * HGHT, WDTH, HGHT)); - layerBoard.addChild(w); - fields[i][j] = w; - } - } - for (int i = 0; i < fields.length; i++) { - for (int j = 0; j < fields[i].length; j++) { - if (i > 0) { - ConnectionWidget horiz = new ConnectionWidget(scene); - horiz.setSourceAnchor(AnchorFactory.createCenterAnchor(fields[i - 1][j])); - horiz.setTargetAnchor(AnchorFactory.createCenterAnchor(fields[i][j])); - scene.addChild(horiz); - } - if (j > 0) { - ConnectionWidget vert = new ConnectionWidget(scene); - vert.setSourceAnchor(AnchorFactory.createCenterAnchor(fields[i][j - 1])); - vert.setTargetAnchor(AnchorFactory.createCenterAnchor(fields[i][j])); - scene.addChild(vert); - } + @Override + protected void paintComponent(Graphics gg) { + int 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; + + g.translate(xdelta / 2, ydelta / 2); + + g.setColor(Color.lightGray); + for (int i = 0; i < 9; i++) { + for (int j = 0; j < 9; j++) { + final Rectangle r = new Rectangle(i * fieldSize, j * fieldSize, fieldSize, fieldSize); + g.fillRect(r.x + fifth, r.y + fifth, r.width - fifth * 2, r.height - fifth * 2); } } - ImageWidget horizontalWall = new ImageWidget(scene); - horizontalWall.setPreferredBounds(new Rectangle(550, 300, 90, 10)); - horizontalWall.setBackground(Color.BLACK); - horizontalWall.getActions().addAction(ActionFactory.createMoveAction(null, new MoveControl(true))); - horizontalWall.setBorder(BorderFactory.createLineBorder()); - horizontalWall.setOpaque(true); - layerBoard.addChild(horizontalWall); - ImageWidget verticalWall = new ImageWidget(scene); - verticalWall.setPreferredBounds(new Rectangle(600, 150, 10, 90)); - verticalWall.setBackground(Color.BLACK); - verticalWall.getActions().addAction(ActionFactory.createMoveAction(null, new MoveControl(false))); - verticalWall.setBorder(BorderFactory.createLineBorder()); - verticalWall.setOpaque(true); - layerBoard.addChild(verticalWall); - - for (Fence f : b.getFences()) { - Rectangle r = fenceRectangle(f.getColumn(), f.getRow(), f.getOrientation()); - ImageWidget fenceWall = new ImageWidget(scene); - fenceWall.setPreferredBounds(r); - fenceWall.setBackground(Color.BLACK); - fenceWall.setBorder(BorderFactory.createLineBorder()); - fenceWall.setOpaque(true); - layerBoard.addChild(fenceWall); + 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); } int cnt = 0; - for (Player p : b.getPlayers()) { - LabelWidget lw = new LabelWidget(scene); - lw.setLabel("" + p.getFences()); - if (cnt == 0) { - lw.setPreferredBounds(new Rectangle(550, 200, 50, 50)); - } else { - lw.setPreferredBounds(new Rectangle(550, 450, 50, 50)); + 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); } - layerBoard.addChild(lw); + cnt++; + } - PlayerWidget pw = new PlayerWidget(scene); - System.err.println("p: " + p); - pw.setPreferredBounds(new Rectangle(p.getColumn() * 50 + 25, p.getRow() * 50 + 25, 50, 50)); - layerBoard.addChild(pw); - - cnt++; + g.setColor(Color.BLACK); + final Font f = new Font("Monospace", Font.BOLD, fifth * 2); + g.setFont(f); + for (int i = 0; i < 8; i++) { + final char ch = (char) ('A' + i); + g.drawString(Character.toString(ch), i * fieldSize + fieldSize - fifth, fifth + f.getSize() - f.getBaselineFor(ch)); + } + for (int i = 0; i < 8; i++) { + String s = "" + (8 - i); + g.drawString(s, fifth, i * fieldSize + fieldSize - fifth + f.getSize() - f.getBaselineFor(s.charAt(0))); } } - private static Rectangle fenceRectangle(char column, int row, Fence.Orientation o) { - int w, h; - switch (o) { - case HORIZONTAL: w = 45; h = 5; break; - case VERTICAL: w = 5; h = 45; break; - default: throw new IllegalStateException(); - } - - return new Rectangle( - (column - 'A' + 1) * 50 + 25 - w, row * 50 + 25 - h, 2 * w, 2 * h - ); - } - - private class MoveControl implements MoveProvider { - final boolean horizontal; - - Point orig; - - private MoveControl(boolean horizontal) { - this.horizontal = horizontal; - } - public void movementStarted(Widget widget) { - System.err.println("started: " + widget.getBounds()); - } - - public void movementFinished(Widget widget) { - try { - final Rectangle bounds = widget.getBounds(); - final Move m = createMoveForDrop( bounds); - System.err.println("finish: " + m); - board = board.apply(m); - } catch (IllegalPositionException ex) { - Exceptions.printStackTrace(ex); - } - - view(scene, board); - } - - public Point getOriginalLocation(Widget widget) { - final Rectangle b = widget.getBounds(); - orig = new Point(b.x, b.y); - try { - System.err.println(" where: " + createMoveForDrop(b)); - } catch (IllegalPositionException ex) { - Exceptions.printStackTrace(ex); - } - return orig; - } - - public void setNewLocation(Widget widget, Point location) { - final Rectangle b = new Rectangle( - round(location.x, true) + (horizontal ? 5 : 0), - round(location.y, false) + (horizontal ? 0 : 5), - horizontal ? 90 : 10, - horizontal ? 10 : 90 - ); - widget.setPreferredBounds(b); - try { - System.err.println("move: " + createMoveForDrop(b)); - } catch (IllegalPositionException ex) { - System.err.println("no move"); - } - } - - Move createMoveForDrop(final Rectangle bounds) throws IllegalPositionException { - int column = bounds.x / 50; - int row = bounds.y / 50; - Orientation o = bounds.width > bounds.height ? Orientation.HORIZONTAL : Orientation.VERTICAL; - switch (o) { - case HORIZONTAL: row--; break; - case VERTICAL: column--; break; - default: assert false; - } - if (column < 0) { - column = 0; - } - if (column >= 9) { - column = 8; - } - if (row < 0) { - row = 0; - } - if (row > 8) { - row = 8; - } - Move m = Move.fence((char) ('A' + column), 1 + row, o); - return m; - } - - int round(int p, boolean cmpHori) { - // p = horizontal ? orig.x + p: orig.y + p; - int onboard = p / 50; - if (onboard < 0) { - onboard = 0; - } - if (onboard >= 9) { - return p; - } - int real = 25 + onboard * 50; - if (horizontal != cmpHori) { - return real - 5; - } else { - return real; - } - } - } - - private static final class PlayerWidget extends Widget { - public PlayerWidget(Scene s) { - super(s); - } - - @Override - protected Rectangle calculateClientArea() { - return getBounds(); - } - - @Override - protected void paintWidget() { - Graphics2D g = getGraphics(); - Rectangle b = getBounds(); - g.drawOval(b.x + 5, b.y + 5, b.width - 10, b.height - 10); - } - } }