Facebook
From Soft Agouti, 3 Years ago, written in Java.
Embed
Download Paste or View Raw
Hits: 51
  1. package agh.cs.project1.animation;
  2.  
  3. import agh.cs.project1.simulation.Parameters;
  4. import agh.cs.project1.simulation.SimulationEngine;
  5.  
  6. import javax.swing.*;
  7. import java.awt.event.*;
  8.  
  9. public class Main implements ActionListener {
  10.     private final Timer timer;
  11.     private final MapPanel mapPanel;
  12.     private final SimulationEngine engine1;
  13.     private final SimulationEngine engine2;
  14.     private final JButton stopSimulationButton;
  15.     private final JButton resumeSimulationButton;
  16.     private final JButton showAnimalsWithDominantGenotype;
  17.  
  18.  
  19.     public Main(SimulationEngine engine1,SimulationEngine engine2, Parameters params){
  20.         int delay = params.getDelay();
  21.         this.timer = new Timer(delay,this);
  22.         Frame frame = new Frame();
  23.         this.engine1 = engine1;
  24.         this.engine2 = engine2;
  25.         this.mapPanel = new MapPanel(engine1,engine2, params);
  26.  
  27.         this.stopSimulationButton = new JButton("Stop simulation");
  28.         this.resumeSimulationButton = new JButton("Resume simulation");
  29.         this.showAnimalsWithDominantGenotype = new JButton("Show animals with dominant genotype");
  30.  
  31.         stopSimulationButton.addActionListener(this);
  32.         resumeSimulationButton.addActionListener(this);
  33.         showAnimalsWithDominantGenotype.addActionListener(this);
  34.  
  35.         mapPanel.add(stopSimulationButton);
  36.         mapPanel.add(resumeSimulationButton);
  37.         mapPanel.add(showAnimalsWithDominantGenotype);
  38.  
  39.         mapPanel.addMouseListener(new MouseAdapter() {
  40.             @Override
  41.             public void mouseClicked(MouseEvent e) {
  42.                 super.mouseClicked(e);
  43.                 mapPanel.findClickedAnimal(e.getPoint().x,e.getPoint().y);
  44.             }
  45.         });
  46.  
  47.         frame.setVisible(true);
  48.         frame.add(mapPanel);
  49.     }
  50.  
  51.     @Override
  52.     public void actionPerformed(ActionEvent e) {
  53.  
  54.         if (engine1.allAnimalsDead() && engine2.allAnimalsDead())
  55.             timer.stop();
  56.  
  57.         Object source = e.getSource();
  58.         if (source == stopSimulationButton) {
  59.             timer.stop();
  60.  
  61.         }
  62.         else if (source == resumeSimulationButton)
  63.             timer.start();
  64.  
  65.         else if (source == showAnimalsWithDominantGenotype) {
  66.             mapPanel.repaint();
  67.             mapPanel.setShowDominant(true);
  68.         }
  69.  
  70.  
  71.         else {
  72.             mapPanel.repaint();
  73.             engine1.nextDay();
  74.             engine2.nextDay();
  75.         }
  76.  
  77.     }
  78.  
  79.  
  80.  
  81.     public void run(){
  82.         timer.start();
  83.     }
  84. }