View Javadoc
1   package org.woehlke.simulation.cyclic.cellular.automaton.view;
2   
3   import org.woehlke.simulation.cyclic.cellular.automaton.model.*;
4   import org.woehlke.simulation.cyclic.cellular.automaton.model.Point;
5   
6   import javax.swing.*;
7   import java.awt.*;
8   
9   
10  /**
11   * (C) 2006 - 2013 Thomas Woehlke.
12   * http://thomas-woehlke.de/p/cyclic-cellular-automaton/
13   * @author Thomas Woehlke
14   * Date: 05.02.2006
15   * Time: 00:51:51
16   */
17  public class WorldCanvas extends JComponent {
18  
19      private CyclicCellularAutomaton cyclicCellularAutomaton;
20      private Point worldDimensions;
21  
22      private Color stateColor[] = new Color[CyclicCellularAutomaton.MAX_STATUS];
23  
24      public WorldCanvas(Point worldDimensions, CyclicCellularAutomaton cyclicCellularAutomaton) {
25          this.worldDimensions = worldDimensions;
26          this.setSize(this.worldDimensions.getX(), this.worldDimensions.getY());
27          this.cyclicCellularAutomaton=cyclicCellularAutomaton;
28          for(int state=0;state<CyclicCellularAutomaton.MAX_STATUS;state++){
29              switch(state){
30                  case 0: stateColor[state]=Color.BLACK ; break;
31                  case 1: stateColor[state]=Color.DARK_GRAY ; break;
32                  case 2: stateColor[state]=Color.GRAY ; break;
33                  case 3: stateColor[state]=Color.LIGHT_GRAY ; break;
34                  case 4: stateColor[state]=Color.WHITE ; break;
35                  case 5: stateColor[state]=Color.MAGENTA ; break;
36                  case 6: stateColor[state]=Color.RED ; break;
37                  case 7: stateColor[state]=Color.ORANGE ; break;
38                  case 8: stateColor[state]=Color.YELLOW ; break;
39                  case 9: stateColor[state]=Color.PINK ; break;
40                  case 10: stateColor[state]=Color.BLUE ; break;
41                  case 11: stateColor[state]=Color.CYAN ; break;
42                  case 12: stateColor[state]=Color.GREEN ; break;
43              }
44          }
45      }
46  
47      public void paint(Graphics g) {
48          super.paintComponent(g);
49          for(int y=0;y<worldDimensions.getY();y++){
50              for(int x=0;x<worldDimensions.getX();x++){
51                  int state = cyclicCellularAutomaton.getCellStatusFor(x,y);
52                   g.setColor(stateColor[state]);
53                   g.drawLine(x,y,x,y);
54              }
55          }
56      }
57  
58      public void update(Graphics g) {
59          paint(g);
60      }
61  
62      public Point getWorldDimensions() {
63          return worldDimensions;
64      }
65  }