View Javadoc
1   package org.woehlke.computer.kurzweil.tabs.simulatedevolution;
2   
3   import lombok.EqualsAndHashCode;
4   import lombok.Getter;
5   import lombok.ToString;
6   import lombok.extern.log4j.Log4j2;
7   import org.woehlke.computer.kurzweil.commons.model.LatticeNeighbourhood;
8   import org.woehlke.computer.kurzweil.tabs.simulatedevolution.model.cell.Cell;
9   import org.woehlke.computer.kurzweil.commons.tabs.TabCanvas;
10  import org.woehlke.computer.kurzweil.commons.model.LatticePoint;
11  import org.woehlke.computer.kurzweil.commons.layouts.LayoutCanvas;
12  
13  import javax.swing.JComponent;
14  import javax.swing.border.Border;
15  import java.awt.*;
16  import java.util.List;
17  
18  import static org.woehlke.computer.kurzweil.tabs.simulatedevolution.canvas.SimulatedEvolutionWorldColor.COLOR_FOOD;
19  import static org.woehlke.computer.kurzweil.tabs.simulatedevolution.canvas.SimulatedEvolutionWorldColor.COLOR_WATER;
20  
21  
22  /**
23   * View for the World Data Model for Displaying Food and Bacteria Cells.
24   * <p>
25   * Simulated Evolution.
26   * Artificial Life Simulation of Bacteria Motion depending on DNA.
27   * <p>
28   * &copy; 2006 - 2018 Thomas Woehlke.
29   * http://thomas-woehlke.de/p/simulated-evolution/
30   *
31   * @author Thomas Woehlke
32   * Date: 05.02.2006
33   * Time: 00:51:51
34   */
35  @Log4j2
36  @Getter
37  @ToString(callSuper = true, exclude={"tabCtx","border","preferredSize","layout","tab"})
38  @EqualsAndHashCode(callSuper=true, exclude={"tabCtx","border","preferredSize","layout","tab"})
39  public class SimulatedEvolutionCanvas extends JComponent implements TabCanvas, SimulatedEvolution {
40  
41      //TODO: new serialVersionUID
42      private static final long serialVersionUID = -27002509360079509L;
43  
44      private final SimulatedEvolutionContext tabCtx;
45      private final Border border;
46      private final LayoutCanvas layout;
47      private final Dimension preferredSize;
48      private final SimulatedEvolutionTab tab;
49      private final SimulatedEvolutionModel tabModel;
50  
51      private final static int startX = 0;
52      private final static int startY = 0;
53      private final int worldX;
54      private final int worldY;
55  
56      public SimulatedEvolutionCanvas(
57          SimulatedEvolutionContext tabCtx
58      ) {
59          this.tabCtx = tabCtx;
60          this.tab = tabCtx.getTab();
61          this.tabModel = new SimulatedEvolutionModel(this.tabCtx);
62          this.border = this.tabCtx.getCtx().getCanvasBorder();
63          this.worldX = this.tabCtx.getCtx().getWorldDimensions().getWidth();
64          this.worldY = this.tabCtx.getCtx().getWorldDimensions().getHeight();
65          this.preferredSize = new Dimension(worldX,worldY);
66          this.layout = new LayoutCanvas(this);
67          this.setLayout(layout);
68          this.setBackground(COLOR_WATER.getColor());
69          this.setSize(preferredSize);
70          this.setPreferredSize(preferredSize);
71          this.setMinimumSize(preferredSize);
72          this.setMaximumSize(preferredSize);
73      }
74  
75    /**
76     * Paint the World on the Canvas and show Food and Bacteria Cells.
77     *
78     * @param graphics Graphics Context with the Tools for Painting.
79     */
80    public void paint(Graphics graphics) {
81      super.paintComponent(graphics);
82        log.info("paint START (Graphics graphics)");
83        log.info("paint Background (Graphics graphics)");
84        // paint Background
85        graphics.setColor(COLOR_WATER.getColor());
86        graphics.fillRect(startX, startY, worldX, worldY);
87        // paint Food
88        log.info("paint Food (Graphics graphics)");
89        graphics.setColor(COLOR_FOOD.getColor());
90        int posX;
91        int posY;
92        for (posY = 0; posY < worldY; posY++) {
93            for (posX = 0; posX < worldX; posX++) {
94                if (tabModel.hasFood(posX, posY)) {
95                    graphics.drawLine(posX, posY, posX, posY);
96                }
97            }
98        }
99        // paint Population
100       log.info("paint Population (Graphics graphics)");
101       List<Cell> population = tabModel.getAllCells();
102       for (Cell cell : population) {
103           posX = cell.getPosition().getX();
104           posY = cell.getPosition().getY();
105           LatticePoint[] square = LatticeNeighbourhood.get(worldX,worldY,posX,posY);
106           graphics.setColor(cell.getLifeCycleStatus().getColor());
107           for (LatticePoint pixel : square) {
108               graphics.drawLine(pixel.getX(), pixel.getY(), pixel.getX(), pixel.getY());
109           }
110       }
111       log.info("paint DONE (Graphics graphics)");
112   }
113 
114   public void update(Graphics graphics) {
115       log.info("update (Graphics graphics)");
116       this.setSize(preferredSize);
117       paint(graphics);
118       log.info("updated (Graphics graphics)");
119   }
120 
121     @Override
122     public void showMe() {
123         log.info("showMe");
124         //log.info("this: "+this.toString());
125     }
126 
127 }