View Javadoc
1   package org.woehlke.computer.kurzweil.simulated.evolution.view.canvas;
2   
3   import lombok.Getter;
4   import lombok.Setter;
5   import lombok.extern.log4j.Log4j2;
6   import org.woehlke.computer.kurzweil.simulated.evolution.model.cell.Cell;
7   import org.woehlke.computer.kurzweil.simulated.evolution.model.world.WorldPoint;
8   import org.woehlke.computer.kurzweil.simulated.evolution.model.SimulatedEvolutionModel;
9   
10  import javax.swing.*;
11  import java.awt.*;
12  import java.util.List;
13  
14  
15  /**
16   * View for the World Data Model for Displaying Food and Bacteria Cells.
17   *
18   * © 2006 - 2008 Thomas Woehlke.
19   * @author Thomas Woehlke
20   *
21   * @see <a href="https://thomas-woehlke.blogspot.com/2016/01/mandelbrot-set-drawn-by-turing-machine.html">Blog Article</a>
22   * @see <a href="https://github.com/Computer-Kurzweil/simulated-evolution">Github Repository</a>
23   * @see <a href="https://java.woehlke.org/simulated-evolution/">Maven Project Repository</a>
24   *
25   * Date: 05.02.2006
26   * Time: 00:51:51
27   */
28  @Log4j2
29  @Getter
30  public class SimulatedEvolutionCanvas extends JComponent {
31  
32      static final long serialVersionUID = 242L;
33  
34      /**
35       * Reference to the Data Model.
36       */
37      @Setter
38      private SimulatedEvolutionModel tabModel;
39  
40      private WorldPoint worldDimensions;
41  
42      private final Color WATER = Color.BLACK;
43      private final Color FOOD = Color.GREEN;
44  
45      public SimulatedEvolutionCanvas(WorldPoint worldDimensions) {
46          this.worldDimensions = worldDimensions;
47          this.setBackground(WATER);
48          this.setSize(this.worldDimensions.getX(), this.worldDimensions.getY());
49      }
50  
51      /**
52       * Paint the World on the Canvas and show Food and Bacteria Cells.
53       * @param g Graphics Context with the Tools for Painting.
54       */
55      public void paint(Graphics g) {
56          super.paintComponent(g);
57          int width = worldDimensions.getX();
58          int height = worldDimensions.getY();
59          g.setColor(WATER);
60          g.fillRect(0,0,width,height);
61          g.setColor(FOOD);
62          for (int y = 0; y < height; y++) {
63              for (int x = 0; x < width; x++) {
64                  if (tabModel.hasFood(x, y)) {
65                      g.drawLine(x,y,x,y);
66                  }
67              }
68          }
69          List<Cell> population = tabModel.getAllCells();
70          for (Cell p:population) {
71              WorldPoint[] square = p.getPosition().getNeighbourhood(worldDimensions);
72              g.setColor(p.getLifeCycleStatus().getColor());
73              for(WorldPoint pixel:square){
74                  g.drawLine(pixel.getX(),pixel.getY(),pixel.getX(),pixel.getY());
75              }
76          }
77      }
78  
79      public void update(Graphics g) {
80          paint(g);
81      }
82  
83  }