/** Set up the GUI. */ public Wator() { thisFrame = this; this.setLayout(new BorderLayout()); this.add(display, BorderLayout.CENTER); this.add(controlPanel, BorderLayout.SOUTH); controlPanel.setLayout(new BorderLayout()); controlPanel.add(statisticsPanel, BorderLayout.NORTH); controlPanel.add(runPanel, BorderLayout.SOUTH); statisticsPanel.setLayout(new GridLayout(1, 2)); statisticsPanel.add(sharkPanel); statisticsPanel.add(fishPanel); runPanel.setLayout(new FlowLayout()); sharkPanel.setLayout(new GridLayout(6, 1)); fishPanel.setLayout(new GridLayout(6, 1)); sharkPanel.setBorder(sharkBorder); sharkPanel.add(new JLabel("Initial number:")); sharkPanel.add(sharkCount); sharkPanel.add(new JLabel("Gestation period:")); sharkPanel.add(sharkGestationPeriod); sharkPanel.add(new JLabel("Starvation period:")); sharkPanel.add(sharkStarvationPeriod); fishPanel.setBorder(fishBorder); fishPanel.add(new JLabel("Initial number:")); fishPanel.add(fishCount); fishPanel.add(new JLabel("Gestation period:")); fishPanel.add(fishGestationPeriod); // remove fish starvation button from GUI // fishPanel.add(new JLabel("Starvation period:")); // fishPanel.add(fishStarvationPeriod); runPanel.setLayout(new BorderLayout()); runPanel.add(speedControlPanel, BorderLayout.CENTER); runPanel.add(buttonPanel, BorderLayout.EAST); buttonPanel.setLayout(new FlowLayout()); buttonPanel.add(populateButton); buttonPanel.add(runButton); speedControlPanel.add(speedControl); speedControl.setMajorTickSpacing(20); speedControl.setMinorTickSpacing(5); speedControl.setPaintTicks(true); speedControl.setPaintLabels(true); ocean = new Ocean(OCEAN_SIZE, OCEAN_SIZE); display.setOcean(ocean); attachListeners(); ocean.addObserver(display); pack(); this.setSize(550, 800); this.setVisible(true); this.setDefaultCloseOperation(EXIT_ON_CLOSE); }
/** Clear the ocean and add sharks and fish, according to the values specified in the GUI. */ private void populate() { try { int count = Integer.valueOf(sharkCount.getText()); int gestation = Integer.valueOf(sharkGestationPeriod.getText()); int starvation = Integer.valueOf(sharkStarvationPeriod.getText()); Parameters.setSharkStatistics(count, gestation, starvation); count = Integer.valueOf(fishCount.getText()); gestation = Integer.valueOf(fishGestationPeriod.getText()); // remove fish starvation calculation as fish cannot starve // starvation = Integer.valueOf(fishStarvationPeriod.getText()); Parameters.setFishStatistics(count, gestation, starvation); ocean.fillWithWater(); ocean.populate(); } catch (Exception ex) { JOptionPane.showMessageDialog(thisFrame, ex.getMessage()); } }