View Javadoc
1   package org.woehlke.computer.kurzweil.tabs.simulatedevolution;
2   
3   import lombok.Setter;
4   import lombok.extern.log4j.Log4j2;
5   import org.woehlke.computer.kurzweil.commons.tabs.TabController;
6   
7   /**
8    * The ControllerThread controls the Interactions between Model and View (MVC-Pattern).
9    *
10   * © 2006 - 2013 Thomas Woehlke.
11   * http://thomas-woehlke.de/p/simulated-evolution/
12   * @author Thomas Woehlke
13   * Date: 05.02.2006
14   * Time: 00:36:20
15   */
16  @Log4j2
17  public class SimulatedEvolutionController extends Thread implements Runnable, TabController, SimulatedEvolution {
18  
19      /**
20       * Data Model for the Simulation
21       */
22      @Setter
23      private SimulatedEvolutionModel simulatedEvolutionModel;
24  
25      /**
26       * Canvas, where to paint in the GUI.
27       */
28      @Setter
29      private SimulatedEvolutionCanvas canvas;
30  
31      /**
32       * Time to Wait in ms.
33       */
34      private final int TIME_TO_WAIT = 100;
35  
36      /**
37       * Control for Threading
38       */
39      private Boolean mySemaphore;
40  
41      public SimulatedEvolutionController() {
42          mySemaphore = Boolean.TRUE;
43      }
44  
45      public void run() {
46          boolean doMyJob;
47          do {
48              synchronized (mySemaphore) {
49                  doMyJob = mySemaphore.booleanValue();
50              }
51              simulatedEvolutionModel.letLivePopulation();
52              canvas.repaint();
53              try {
54                  sleep(TIME_TO_WAIT);
55              }
56              catch (InterruptedException e) {
57                  e.printStackTrace();
58              }
59          }
60          while (doMyJob);
61      }
62  
63      public void exit() {
64          synchronized (mySemaphore) {
65              mySemaphore = Boolean.FALSE;
66          }
67      }
68  }