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
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
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 }