package grid; import javafx.animation.AnimationTimer; import javafx.application.Application; import javafx.beans.property.DoubleProperty; import javafx.beans.property.SimpleDoubleProperty; import javafx.scene.Group; import javafx.scene.Scene; import javafx.scene.paint.Color; import javafx.scene.shape.Line; import javafx.scene.shape.Rectangle; import javafx.stage.Stage; /** User: rbair Date: 5/29/13 Time: 12:52 PM */ public class GridApp extends Application { @Override public void start(Stage primaryStage) throws Exception { final Grid grid = new Grid(); AnimationTimer timer = new AnimationTimer() { @Override public void handle(long now) { grid.update(); } }; timer.start(); Scene scene = new Scene(grid); primaryStage.setScene(scene); primaryStage.show(); } public static void main(String[] args) { launch(args); } } class Cell { Grid grid; int row, col; DoubleProperty x = new SimpleDoubleProperty(); DoubleProperty y = new SimpleDoubleProperty(); double xv, yv, cx, cy; Cell left, right, bottom, top; Cell(Grid grid, int row, int col) { this.row = row; this.col = col; this.grid = grid; x.set(col * grid.gSize); y.set(row * grid.gSize); cx = x.get(); cy = y.get(); } // called after the whole grid.cells sequence has been initialized/filled void initAdjacencies() { left = (col > 0) ? grid.cells[row * grid.width + col-1] : null; right = (col < grid.width-1) ? grid.cells[row * grid.width + col+1] : null; top = (row > 0) ? grid.cells[(row-1) * grid.width + col] : null; bottom = (row < grid.height-1) ? grid.cells[(row+1) * grid.width + col] : null; } } class Grid extends Group { double xmouse, ymouse; double k1 = 0.01; double k2 = 0.20000000000000001; double damp = 0.72999999999999998; int drag = -1; boolean dragging; final int width = 80; final int height = 80; final double gSize = 15; Cell[] cells = new Cell[height * width]; { for (int row=0; row { int row = (int)(e.getSceneY() / gSize); int col = (int)(e.getSceneX() / gSize); if (row >= 0 && row < height && col >=0 && col < width) { xmouse = e.getSceneX(); ymouse = e.getSceneY(); drag = row * width + col; } dragging = true; }); r.setOnMouseDragged((e) -> { xmouse = e.getSceneX(); ymouse = e.getSceneY(); }); r.setOnMouseReleased((e) -> { dragging = false; drag = -1; }); Group g = new Group(); for (Cell cell : cells) { Group gg = new Group(); if (cell.right != null) { Line line = new Line(); line.setSmooth(false); line.setStroke(Color.WHITE); line.startXProperty().bind(cell.x); line.startYProperty().bind(cell.y); line.endXProperty().bind(cell.right.x); line.endYProperty().bind(cell.right.y); gg.getChildren().add(line); } if (cell.bottom != null) { Line line = new Line(); line.setSmooth(false); line.setStroke(Color.WHITE); line.startXProperty().bind(cell.x); line.startYProperty().bind(cell.y); line.endXProperty().bind(cell.bottom.x); line.endYProperty().bind(cell.bottom.y); gg.getChildren().add(line); } g.getChildren().add(gg); } getChildren().addAll(r, g); }}); }}); /* // uncomment to use rectangles instead of lines [if (cell.right == null) null else Rectangle { smooth: false; stroke: null;//Color.GREEN; fill: Color.GREEN x: bind cell.x; y: bind cell.y; width: bind (cell.right.x-cell.x); height: 1//bind (cell.y - cell.right.y); }, if (cell.bottom == null) null else Rectangle { smooth: false; fill: Color.RED; stroke: null x: bind cell.x; y: bind cell.y; width: 1//bind cell.bottom.x; height: bind (cell.bottom.y-cell.y); }] */ } }