View Javadoc
1   package org.woehlke.computer.kurzweil.mandelbrot.model;
2   
3   import org.woehlke.computer.kurzweil.mandelbrot.config.Config;
4   import org.woehlke.computer.kurzweil.mandelbrot.model.fractal.GaussianNumberPlane;
5   import org.woehlke.computer.kurzweil.mandelbrot.model.helper.Point;
6   import org.woehlke.computer.kurzweil.mandelbrot.model.state.ApplicationStateMachine;
7   import org.woehlke.computer.kurzweil.mandelbrot.model.turing.MandelbrotTuringMachine;
8   import org.woehlke.computer.kurzweil.mandelbrot.view.ApplicationFrame;
9   
10  /**
11   * Mandelbrot Set drawn by a Turing Machine.
12   *
13   * (C) 2006 - 2015 Thomas Woehlke.
14   * https://thomas-woehlke.blogspot.com/2016/01/mandelbrot-set-drawn-by-turing-machine.html
15   * @author Thomas Woehlke
16   *
17   * Created by tw on 16.12.2019.
18   */
19  public class ApplicationModel {
20  
21      private volatile GaussianNumberPlane gaussianNumberPlane;
22      private volatile MandelbrotTuringMachine mandelbrotTuringMachine;
23      private volatile ApplicationStateMachine applicationStateMachine;
24  
25      private volatile Config config;
26      private volatile ApplicationFrame frame;
27  
28      public ApplicationModel(Config config, ApplicationFrame frame) {
29          this.config = config;
30          this.frame = frame;
31          this.gaussianNumberPlane = new GaussianNumberPlane(this);
32          this.mandelbrotTuringMachine = new MandelbrotTuringMachine(this);
33          this.applicationStateMachine = new ApplicationStateMachine();
34      }
35  
36      public synchronized boolean click(Point c) {
37          applicationStateMachine.click();
38          boolean repaint = true;
39          switch (applicationStateMachine.getApplicationState()) {
40              case MANDELBROT:
41                  mandelbrotTuringMachine.start();
42                  repaint = false;
43                  break;
44              case JULIA_SET:
45                  gaussianNumberPlane.computeTheJuliaSetFor(c);
46                  break;
47              case MANDELBROT_ZOOM:
48                  gaussianNumberPlane.zoomIntoTheMandelbrotSet(c);
49                  break;
50              case JULIA_SET_ZOOM:
51                  gaussianNumberPlane.zoomIntoTheJuliaSetFor(c);
52                  break;
53          }
54          return repaint;
55      }
56  
57      public synchronized boolean step() {
58          boolean repaint = false;
59          switch (applicationStateMachine.getApplicationState()) {
60              case MANDELBROT:
61                  repaint = mandelbrotTuringMachine.step();
62                  break;
63              case JULIA_SET:
64              case MANDELBROT_ZOOM:
65              case JULIA_SET_ZOOM:
66                  break;
67          }
68          return repaint;
69      }
70  
71      public synchronized int getCellStatusFor(int x, int y) {
72          return gaussianNumberPlane.getCellStatusFor(x, y);
73      }
74  
75      public Point getWorldDimensions() {
76          int width = config.getWidth();
77          int height = config.getHeight();
78          return new Point(width, height);
79      }
80  
81      public void setModeSwitch() {
82          this.applicationStateMachine.setModeSwitch();
83          this.frame.setModeSwitch();
84      }
85  
86      public void setModeZoom() {
87          this.gaussianNumberPlane.setModeZoom();
88          this.applicationStateMachine.setModeZoom();
89          this.frame.setModeZoom();
90      }
91  
92      public GaussianNumberPlane getGaussianNumberPlane() {
93          return gaussianNumberPlane;
94      }
95  
96      public ApplicationFrame getFrame() {
97          return frame;
98      }
99  
100     public Config getConfig() {
101         return config;
102     }
103 
104     public void zoomOut() {
105         switch (applicationStateMachine.getApplicationState()) {
106             case MANDELBROT:
107             case JULIA_SET:
108                 break;
109             case MANDELBROT_ZOOM:
110                 gaussianNumberPlane.zoomOutOfTheMandelbrotSet();
111                 break;
112             case JULIA_SET_ZOOM:
113                 gaussianNumberPlane.zoomOutOfTheJuliaSet();
114                 break;
115         }
116     }
117 }