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
17
18
19
20
21
22
23
24
25
26
27
28 @Log4j2
29 @Getter
30 public class SimulatedEvolutionCanvas extends JComponent {
31
32 static final long serialVersionUID = 242L;
33
34
35
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
53
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 }