Beispiel #1
0
 public void reset() {
   for (int i = 0; i < grid.getWidth(); i++) {
     for (int j = 0; j < grid.getHeight(); j++) {
       if (cellViews[i][j] != null) {
         while (cellViews[i][j].getState() != 0) cellViews[i][j].incrementState();
       }
     }
   }
 }
Beispiel #2
0
  public void zoomIn() {
    int width = grid.getWidth();
    int height = grid.getHeight();
    grid.cropLeft(width / 4);
    grid.cropRight(width / 4);
    grid.cropTop(height / 4);
    grid.cropBottom(height / 4);
    cellSizeProperty.set(cellSizeProperty.get() * 2);

    timelineRate = timeline.getRate();
    init();
    runner.setGrid(grid);
  }
Beispiel #3
0
  public void zoomOut() {
    int width = grid.getWidth();
    int height = grid.getHeight();
    grid.padLeft(width / 2);
    grid.padRight(width / 2);
    grid.padTop(height / 2);
    grid.padBottom(height / 2);
    cellSizeProperty.set(cellSizeProperty.get() / 2);

    timelineRate = timeline.getRate();
    init();
    runner.setGrid(grid);
  }
Beispiel #4
0
 private int addCentralCellViews(int cellsAdded) {
   //		System.out.println("Central");
   for (int i = 0; i < grid.getWidth(); i++) {
     for (int j = 0; j < grid.getHeight(); j++) {
       if (cellViews[i + xOffset][j + yOffset] == null) {
         CellView cellView = new CellView(grid.getCell(i, j));
         cellViews[i + xOffset][j + yOffset] = cellView;
         cellView.widthProperty().bind(cellSizeProperty);
         cellView.heightProperty().bind(cellSizeProperty);
         cellView.addEventHandler(MouseEvent.ANY, mouseEventHandler);
         add(cellView, i, j);
         cellsAdded++;
       }
       if (cellsAdded >= centralCellsToMake) {
         //					System.out.println("central dong");
         i = grid.getWidth();
         j = grid.getHeight();
       }
     }
   }
   return cellsAdded;
 }
Beispiel #5
0
 private int addLiveCellViews() {
   //		System.out.println("Live");
   int cellsAdded = 0;
   for (int i = 0; i < grid.getWidth(); i++) {
     for (int j = 0; j < grid.getHeight(); j++) {
       if (grid.getCell(i, j).getState() != 0) {
         CellView cellView = new CellView(grid.getCell(i, j));
         cellViews[i + xOffset][j + yOffset] = cellView;
         cellView.widthProperty().bind(cellSizeProperty);
         cellView.heightProperty().bind(cellSizeProperty);
         cellView.addEventHandler(MouseEvent.ANY, mouseEventHandler);
         add(cellView, i, j);
         cellsAdded++;
       }
     }
   }
   return cellsAdded;
 }
Beispiel #6
0
  public void init() {
    setOffsets();
    Stage primaryStage = (Stage) getScene().getWindow();
    mouseEventHandler =
        new EventHandler<MouseEvent>() {
          @Override
          public void handle(MouseEvent event) {
            CellView source = (CellView) event.getSource();
            if (event.getEventType().equals(MouseEvent.MOUSE_PRESSED)) {
              if (event.isControlDown()) {
                int x =
                    (int)
                        Math.floor(
                            (event.getScreenX() - primaryStage.getX() - dxProperty.get())
                                / cellSizeProperty.get());
                int y =
                    (int)
                        Math.floor(
                            (event.getScreenY() - primaryStage.getY() - dyProperty.get())
                                / cellSizeProperty.get());
                for (int i = 0; i < pattern.length; i++) {
                  for (int j = 0; j < pattern[0].length; j++) {
                    try {
                      if (cellViews[x + j][y + i].getState() != pattern[i][j])
                        cellViews[x + j][y + i].incrementState();
                    } catch (ArrayIndexOutOfBoundsException e) {
                    }
                  }
                }
              } else {
                source.incrementState();
                currentState = source.getState();
              }
            } else if (event.getEventType().equals(MouseEvent.MOUSE_DRAGGED)) {
              int x =
                  (int)
                      Math.floor(
                          (event.getScreenX() - primaryStage.getX() - dxProperty.get())
                              / cellSizeProperty.get());
              int y =
                  (int)
                      Math.floor(
                          (event.getScreenY() - primaryStage.getY() - dyProperty.get())
                              / cellSizeProperty.get());
              if (cellViews[x][y].getState() != currentState) cellViews[x][y].incrementState();
            }
          }
        };

    getChildren().clear();
    for (int i = 0; i < grid.getWidth(); i++) {
      for (int j = 0; j < grid.getHeight(); j++) {
        CellView cellView = null;
        if ((i <= 1 || j <= 1) || (grid.getCell(i, j).getState() != 0)) {
          cellView = new CellView(grid.getCell(i, j));
          cellView.widthProperty().bind(cellSizeProperty);
          cellView.heightProperty().bind(cellSizeProperty);
          cellView.addEventHandler(MouseEvent.ANY, mouseEventHandler);
          add(cellView, i, j);
        }
        cellViews[i + xOffset][j + yOffset] = cellView;
        totalCells--;
      }
    }
    timeline = new Timeline(new KeyFrame(new Duration(100), ((event) -> updateGrid())));
    timeline.setCycleCount(Timeline.INDEFINITE);

    getScene()
        .setOnKeyPressed(
            (keyEvent) -> {
              if (keyEvent.getCode().equals(KeyCode.SPACE)) {
                if (playing) timeline.pause();
                else timeline.play();
                playing = !playing;
              } else if (keyEvent.getCode().equals(KeyCode.MINUS)) {
                if (keyEvent.isControlDown() && cellSizeProperty.get() > 2) zoomOut();
                else if (playing && timeline.getRate() > 0.03125)
                  timeline.setRate(timeline.getRate() / 2);
              } else if (keyEvent.getCode().equals(KeyCode.EQUALS)) {
                if (keyEvent.isControlDown() && cellSizeProperty.get() < 64) zoomIn();
                else if (playing && timeline.getRate() < 16)
                  timeline.setRate(timeline.getRate() * 2);
              }
            });
    timeline.setRate(timelineRate);

    if (playing) timeline.play();
    ((Stage) getScene().getWindow()).sizeToScene();
  }
Beispiel #7
0
 public void setOffsets() {
   xOffset = (totalRows - grid.getWidth()) / 2;
   yOffset = (totalRows - grid.getHeight()) / 2;
 }
Beispiel #8
0
 public void randomize() {
   Random r = new Random();
   for (int i = 0; i < grid.getWidth() * grid.getHeight() / 4; i++) {
     grid.getCell(r.nextInt(grid.getWidth()), r.nextInt(grid.getHeight())).incrementState();
   }
 }