View Javadoc
1   package org.woehlke.simulation.evolution.gui;
2   
3   import org.woehlke.simulation.evolution.dom.ISimGenWorld;
4   import org.woehlke.simulation.evolution.beans.SimGenPoint;
5   
6   import java.awt.*;
7   import java.util.ArrayList;
8   import java.util.Iterator;
9   
10  /**
11   * (C) 2006 - 2008 Thomas Woehlke
12   * http://www.thomas-woehlke.de
13   * @author Thomas Woehlke
14   * Date: 05.02.2006
15   * Time: 00:51:51
16   */
17  public class SimGenWorldCanvas extends Canvas implements ISimGenWorldCanvas {
18      private ISimGenWorld world;
19      private SimGenPoint dimensions;
20  
21      Color water = Color.BLACK;
22      Color food = Color.GREEN;
23      Color bazillus = Color.RED;
24  
25      public SimGenWorldCanvas(int x, int y) {
26          this.dimensions = new SimGenPoint(x, y);
27          this.setBackground(water);
28          this.setSize(x, y);
29      }
30  
31      public SimGenWorldCanvas(SimGenPoint dimensions) {
32          this.dimensions = dimensions;
33          this.setBackground(water);
34      }
35  
36      public void paint(Graphics g) {
37          int width = dimensions.getX();
38          int height = dimensions.getY();
39          g.clearRect(0, 0, width, height);
40          for (int y = 0; y < height; y++) {
41              for (int x = 0; x < width; x++) {
42                  if (world.hasFood(x, y)) {
43                      g.setColor(food);
44                      g.drawLine(x, y, x, y);
45                  }
46              }
47          }
48          g.setColor(bazillus);
49          ArrayList<SimGenPoint> population = world.getPositionsOfAllCells();
50          Iterator<SimGenPoint> it = population.iterator();
51          while (it.hasNext()) {
52              SimGenPoint p = it.next();
53              g.fillRect(p.getX() - 1, p.getY() - 1, 3, 3);
54          }
55      }
56  
57      public void update(Graphics g) {
58          paint(g);
59      }
60  
61      public SimGenPoint getDimensions() {
62          return dimensions;
63      }
64  
65      public void setDimensions(SimGenPoint dimensions) {
66          this.dimensions = dimensions;
67      }
68  
69      public void setWorld(ISimGenWorld world) {
70          this.world = world;
71      }
72  
73      public ISimGenWorld getWorld() {
74          return world;
75      }
76  }