Ejemplo n.º 1
0
  public void actionPerformed(ActionEvent ae) {

    if (ae.getSource() == this.buttonStart && !animate) {
      animate = true;
      animatorThread = new Thread(animator);
      animatorThread.start();
    }

    if (ae.getSource() == this.buttonStop && animate) {
      animate = false;
      animatorThread = null;
    }

    if (ae.getSource() == this.buttonNextRound && !animate) {
      board.nextRound();
      numOfGenerations++;
      grid.setBoard(board.getCurrentRound());
      grid.repaint();
      generationsLabel.setText("Generation " + numOfGenerations);
    }

    if (ae.getSource() == this.buttonClearBoard && !animate) {
      board.clear();
      this.numOfGenerations = 0;
      grid.repaint();
      generationsLabel.setText("Generation " + numOfGenerations);
    }

    if (ae.getSource() == this.drawingCheckBox) {
      draw = drawingCheckBox.isSelected();
    }
  }
Ejemplo n.º 2
0
  public void prepareGUI() {
    mainFrame = new JFrame("Game of life");
    mainFrame.setSize(750, 670);
    mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    grid = new GridPanel(board.getNumOfRows(), board.getNumOfColumns());
    grid.setPreferredSize(new Dimension(600, 600));
    grid.setBoard(this.board.getCurrentRound());
    grid.addMouseListener(this);

    scrollPane = new JScrollPane(grid);
    scrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
    scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);

    buttonStart = new JButton("Start");
    buttonStop = new JButton("Stop");
    buttonNextRound = new JButton("Next round");
    buttonClearBoard = new JButton("Clear board");

    buttonStart.setMaximumSize(
        new Dimension(Integer.MAX_VALUE, buttonStart.getMinimumSize().height));
    buttonStop.setMaximumSize(new Dimension(Integer.MAX_VALUE, buttonStop.getMinimumSize().height));
    buttonNextRound.setMaximumSize(
        new Dimension(Integer.MAX_VALUE, buttonNextRound.getMinimumSize().height));
    buttonClearBoard.setMaximumSize(
        new Dimension(Integer.MAX_VALUE, buttonClearBoard.getMinimumSize().height));

    buttonStart.addActionListener(this);
    buttonStop.addActionListener(this);
    buttonNextRound.addActionListener(this);
    buttonClearBoard.addActionListener(this);

    generationsLabel = new JLabel("Generation " + numOfGenerations);

    drawingCheckBox = new JCheckBox("Drawing");

    buttonsPanel = new JPanel();
    buttonsPanel.setLayout(new BoxLayout(buttonsPanel, BoxLayout.PAGE_AXIS));
    buttonsPanel.add(buttonStart);
    buttonsPanel.add(Box.createVerticalStrut(2));
    buttonsPanel.add(buttonStop);
    buttonsPanel.add(Box.createVerticalStrut(2));
    buttonsPanel.add(buttonNextRound);
    buttonsPanel.add(Box.createVerticalStrut(2));
    buttonsPanel.add(buttonClearBoard);
    buttonsPanel.add(Box.createVerticalStrut(2));
    buttonsPanel.add(generationsLabel);
    buttonsPanel.add(Box.createVerticalStrut(2));
    buttonsPanel.add(drawingCheckBox);

    mainFrame.add(scrollPane, BorderLayout.CENTER);
    mainFrame.add(buttonsPanel, BorderLayout.EAST);
  }
Ejemplo n.º 3
0
        @Override
        public void run() {

          while (animate) {
            board.nextRound();
            grid.setBoard(board.getCurrentRound());
            numOfGenerations++;
            grid.repaint();
            generationsLabel.setText("Generation " + numOfGenerations);

            try {
              Thread.sleep(250);
            } catch (InterruptedException e) {
              e.printStackTrace();
            }
          }
        }
Ejemplo n.º 4
0
  @Override
  public void mouseReleased(MouseEvent e) {
    if (animate || e.getButton() != MouseEvent.BUTTON1) {
      return;
    }

    int row = e.getY() / 10;
    int column = e.getX() / 10;

    if (row >= board.getNumOfRows() || column >= board.getNumOfColumns()) {
      return;
    }

    toggleCell(new Position(column, row));
    numOfGenerations = 0;

    grid.repaint();
    generationsLabel.setText("Generation " + numOfGenerations);
  }