View Javadoc
1   package org.woehlke.computer.kurzweil.mandelbrot.julia.view.canvas;
2   
3   
4   import org.woehlke.computer.kurzweil.mandelbrot.julia.model.ApplicationModel;
5   
6   import javax.swing.*;
7   import java.awt.*;
8   import java.io.Serial;
9   
10  
11  /**
12   * Mandelbrot Set drawn by a Turing Machine.
13   * (C) 2006 - 2022 Thomas Woehlke.
14   * @author Thomas Woehlke
15   *
16   * @see <a href="https://thomas-woehlke.blogspot.com/2016/01/mandelbrot-set-drawn-by-turing-machine.html">Blog Article</a>
17   * @see <a href="https://github.com/Computer-Kurzweil/mandelbrot-julia">Github Repository</a>
18   * @see <a href="https://java.woehlke.org/mandelbrot-julia/">Maven Project Repository</a>
19   *
20   * @see ApplicationModel
21   * @see Dimension
22   *
23   * @see JComponent
24   * @see Graphics
25   *
26   * Date: 05.02.2006
27   * Time: 00:51:51
28   */
29  public class MandelbrotJuliaCanvas extends JComponent {
30  
31      @Serial
32      private final static long serialVersionUID = 242L;
33  
34      private final ApplicationModel app;
35      private final Dimension preferredSize;
36  
37      public MandelbrotJuliaCanvas(ApplicationModel app) {
38          this.app = app;
39          int width = this.app.getWorldDimensions().getWidth();
40          int height = this.app.getWorldDimensions().getHeight();
41          this.preferredSize = new Dimension(width, height);
42          this.setSize(this.preferredSize);
43          this.setPreferredSize(preferredSize);
44      }
45  
46      public void paint(Graphics g) {
47          this.setSize(this.preferredSize);
48          this.setPreferredSize(preferredSize);
49          super.paintComponent(g);
50          for(int y = 0; y < app.getWorldDimensions().getY(); y++){
51              for(int x = 0; x < app.getWorldDimensions().getX(); x++){
52                  Color stateColor = getColorForCellStatus(app.getCellStatusFor(x,y));
53                  g.setColor(stateColor);
54                  g.drawLine(x,y,x,y);
55              }
56          }
57      }
58  
59      private Color getColorForCellStatus(int cellStatus){
60          int red = 0;
61          int green = 0;
62          int blue = cellStatus * 3 + 32;
63          blue = Math.min(blue,255);
64          return new Color(red, green, blue);
65      }
66  
67      public void update(Graphics g) {
68          paint(g);
69      }
70  
71  }