View Javadoc
1   package org.woehlke.computer.kurzweil.tabs.cca.view;
2   
3   import org.woehlke.computer.kurzweil.tabs.cca.config.ObjectRegistry;
4   
5   import javax.swing.*;
6   import java.awt.*;
7   import java.io.Serializable;
8   
9   /**
10   * Cyclic Cellular Automaton.
11   *
12   * (C) 2006 - 2013 Thomas Woehlke.
13   * http://thomas-woehlke.de/p/cyclic-cellular-automaton/
14   * @author Thomas Woehlke
15   *
16   * Date: 05.02.2006
17   * Time: 00:51:51
18   */
19  public class CyclicCellularAutomatonCanvas extends JComponent implements Serializable {
20  
21      private static final long serialVersionUID = -3057254130516052936L;
22  
23      private ObjectRegistry ctx;
24  
25      public CyclicCellularAutomatonCanvas(ObjectRegistry ctx) {
26          this.ctx = ctx;
27          Dimension preferredSize = new Dimension(
28              (int) ctx.getConfig().getLatticeDimensions().getX(),
29              (int) ctx.getConfig().getLatticeDimensions().getY()
30          );
31          this.setPreferredSize(preferredSize);
32      }
33  
34      public void paint(Graphics g) {
35          super.paintComponent(g);
36          for(int y = 0; y < ctx.getConfig().getLatticeDimensions().getY(); y++){
37              for(int x = 0; x < ctx.getConfig().getLatticeDimensions().getX(); x++){
38                  int state = this.ctx.getLattice().getCellStatusFor(x,y);
39                  Color stateColor = this.ctx.getColorScheme().getColorForState(state);
40                  g.setColor(stateColor);
41                  g.drawLine(x,y,x,y);
42              }
43          }
44      }
45  
46      public void update(Graphics g) {
47          paint(g);
48      }
49  
50  }