View Javadoc
1   package org.woehlke.computer.kurzweil.tabs.simulatedevolution;
2   
3   import lombok.Getter;
4   import lombok.Setter;
5   import lombok.extern.log4j.Log4j2;
6   import org.woehlke.computer.kurzweil.commons.tabs.TabCanvas;
7   import org.woehlke.computer.kurzweil.tabs.simulatedevolution.model.Cell;
8   import org.woehlke.computer.kurzweil.tabs.simulatedevolution.model.WorldPoint;
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   * http://thomas-woehlke.de/p/simulated-evolution/
20   * @author Thomas Woehlke
21   * Date: 05.02.2006
22   * Time: 00:51:51
23   */
24  @Log4j2
25  @Getter
26  public class SimulatedEvolutionCanvas extends JComponent implements TabCanvas, SimulatedEvolution {
27  
28      static final long serialVersionUID = 242L;
29  
30      /**
31       * Reference to the Data Model.
32       */
33      @Setter
34      private SimulatedEvolutionModel tabModel;
35  
36      private WorldPoint worldDimensions;
37  
38      private final Color WATER = Color.BLACK;
39      private final Color FOOD = Color.GREEN;
40  
41      public SimulatedEvolutionCanvas(WorldPoint worldDimensions) {
42          this.worldDimensions = worldDimensions;
43          this.setBackground(WATER);
44          this.setSize(this.worldDimensions.getX(), this.worldDimensions.getY());
45      }
46  
47      /**
48       * Paint the World on the Canvas and show Food and Bacteria Cells.
49       * @param g Graphics Context with the Tools for Painting.
50       */
51      public void paint(Graphics g) {
52          super.paintComponent(g);
53          int width = worldDimensions.getX();
54          int height = worldDimensions.getY();
55          g.setColor(WATER);
56          g.fillRect(0,0,width,height);
57          g.setColor(FOOD);
58          for (int y = 0; y < height; y++) {
59              for (int x = 0; x < width; x++) {
60                  if (tabModel.hasFood(x, y)) {
61                      g.drawLine(x,y,x,y);
62                  }
63              }
64          }
65          List<Cell> population = tabModel.getAllCells();
66          for (Cell p:population) {
67              WorldPoint[] square = p.getPosition().getNeighbourhood(worldDimensions);
68              g.setColor(p.getLifeCycleStatus().getColor());
69              for(WorldPoint pixel:square){
70                  g.drawLine(pixel.getX(),pixel.getY(),pixel.getX(),pixel.getY());
71              }
72          }
73      }
74  
75      public void update(Graphics g) {
76          paint(g);
77      }
78  
79  }