示例#1
0
 public void doSequence() { // goes through size^2 generations and then displays the results
   long start = System.currentTimeMillis();
   for (int i = 0;
       i < (Math.pow(2, Math.pow(viewport.arrayW, 2))) / 100000;
       i++) { // uses width as size (must have a square world for this!)
     viewport.doWorldLogic();
   }
   long end = System.currentTimeMillis();
   System.out.println("it took: " + (end - start) + " ms for 1 gen");
   System.out.println("check: " + viewport.check);
   viewport.repaint();
 }
示例#2
0
  public GUI() {

    // setup the modal dialog to input the width, height, and scale for the viewport:
    JPanel inputPanel = new JPanel(); // panel that will hold the text fields for the dialog box
    inputPanel.setLayout(new BoxLayout(inputPanel, BoxLayout.Y_AXIS));

    JLabel lWidth = new JLabel("Width: ");
    lWidth.setPreferredSize(new Dimension(140, 30));
    inputPanel.add(lWidth);

    JTextField tWidth = new JTextField("15"); // default value of the width textfield
    tWidth.setPreferredSize(new Dimension(140, 30));
    inputPanel.add(tWidth);

    JLabel lHeight = new JLabel("Height: ");
    lHeight.setPreferredSize(new Dimension(140, 30));
    inputPanel.add(lHeight);

    JTextField tHeight = new JTextField("15"); // default value of the height field
    tHeight.setPreferredSize(new Dimension(140, 30));
    inputPanel.add(tHeight);

    JLabel lScale = new JLabel("Scale: ");
    lScale.setPreferredSize(new Dimension(140, 30));
    inputPanel.add(lScale);

    JTextField tScale = new JTextField("30"); // default value of the scale field
    tScale.setPreferredSize(new Dimension(140, 30));
    inputPanel.add(tScale);

    String[] options = {"GO!"}; // string array for the options on the dialog

    int selectedOption =
        JOptionPane.showOptionDialog(
            null,
            inputPanel,
            "Set Sizes",
            JOptionPane.NO_OPTION,
            JOptionPane.QUESTION_MESSAGE,
            null,
            options,
            options[0]);

    if (selectedOption == 0) {
      // set the values from the main window's "width", "height", and "scale" variables to the
      // values in the dialog's textboxes
      this.width = Integer.valueOf(tWidth.getText());
      this.height = Integer.valueOf(tHeight.getText());
      this.scale = Integer.valueOf(tScale.getText());
    }
    // done with the dialog box

    // setup layout for window and it's viewport:
    this.setLayout(new BoxLayout(this.getContentPane(), BoxLayout.X_AXIS));
    this.setBackground(Color.LIGHT_GRAY);
    viewport =
        new Viewport(
            width, height,
            scale); // size and scale of simulation (see Viewport constructor for more)15, 15,
    // 30//TODO Width cannot be larger than height!
    viewport.setPreferredSize(new Dimension(viewport.panelW, viewport.panelH));
    viewport.setMinimumSize(new Dimension(viewport.panelW, viewport.panelH));
    viewport.addMouseListener(this);

    // setup the guiPanel (that holds the GUI)
    guiPanel = new JPanel();
    guiPanel.setPreferredSize(new Dimension(200, viewport.panelH));
    guiPanel.setMinimumSize(new Dimension(200, viewport.panelH));

    // setup the gui components:
    Dimension guiDimension = new Dimension(140, 30);

    lAgents = new JLabel("Number of live agents:");
    guiPanel.add(lAgents);

    tAgents = new JTextField();
    tAgents.setPreferredSize(guiDimension);
    tAgents.setDisabledTextColor(Color.BLACK);
    tAgents.setEnabled(false); // set the agents textfield to e un-editable
    guiPanel.add(tAgents);

    lGens = new JLabel("Number of generations: ");
    guiPanel.add(lGens);

    tGens = new JTextField();
    tGens.setPreferredSize(guiDimension);
    tGens.setDisabledTextColor(Color.BLACK);
    tGens.setEnabled(false);
    guiPanel.add(tGens);

    lFrameRate = new JLabel("Framerate: ");
    guiPanel.add(lFrameRate);

    tFrameRate = new JTextField("15"); // set default frame rate to 15
    tFrameRate.setPreferredSize(guiDimension);
    guiPanel.add(tFrameRate);

    lPath = new JLabel("Absolute Path for screenshots: ");
    lPath.setPreferredSize(guiDimension);
    guiPanel.add(lPath);

    tPath =
        new JTextField(
            "/home/silvertale/Pictures/ALifeImages/CLife_"); // sets path for writing image out to
    // file
    tPath.setPreferredSize(guiDimension);
    guiPanel.add(tPath);

    screenShot = new JButton("Screenshot...");
    screenShot.setPreferredSize(guiDimension);
    screenShot.addActionListener(this);
    guiPanel.add(screenShot);

    startStop = new JButton("start/stop");
    startStop.setPreferredSize(guiDimension);
    startStop.addActionListener(this);
    guiPanel.add(startStop);

    info =
        new JLabel(
            "<html><center>Written by Nicholas Sallis <br>([email protected])</center></html>");
    info.setPreferredSize(new Dimension(160, 70));
    guiPanel.add(info);

    // build window:
    this.add(viewport);
    this.add(guiPanel);
    this.setMinimumSize(
        new Dimension(viewport.panelW + 200, viewport.panelH)); // add 200 to side for gui
    this.setSize(viewport.getViewportSize().width, viewport.getViewportSize().height + 30);
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.setTitle("Conway's Game of Life");
    this.setVisible(true);

    while (true) { // main  loop:
      startTime = System.currentTimeMillis() % 1000;
      viewport.repaint();
      if (!editMode) {
        viewport.doWorldLogic();
      }
      // check for changes in the textfields:
      tAgents.setText(String.valueOf(viewport.numberOfOrganisms));
      tGens.setText(String.valueOf(viewport.numberOfFrames));

      if (!tFrameRate.getText().isEmpty()) {
        framerate = Integer.valueOf(tFrameRate.getText());
      } else {
        framerate =
            5; // secretly set the framerate to 5, but don't show it. wait for the user to enter a
        // new framerate.
      }

      // smooth framerate:
      endTime = System.currentTimeMillis() % 1000;
      try {
        if ((1000 / framerate) - (endTime - startTime)
            > 0) { // check for bad framerates dynamically(on the fly)
          Thread.sleep((1000 / framerate) - (endTime - startTime));
        } else {
          // create a warning dialog that the framerate is too high:
          JOptionPane.showMessageDialog(
              this,
              "The framerate is too high to be handled by your computer! Please lower it and try again. ");
          tFrameRate.setText("15"); // reset the framerate to a reasonable value
        }
      } catch (InterruptedException e) {
        System.out.println("There was a problem with the frame control!");
        e.printStackTrace();
      }
    }
  }