home *** CD-ROM | disk | FTP | other *** search
/ BURKS 2 / BURKS_AUG97.ISO / BURKS / LANGUAGE / JAVA / NOTES / SOURCE / cacanvas.jav < prev    next >
Text File  |  1996-12-20  |  7KB  |  263 lines

  1.  
  2. /*
  3.    This code is, for the moment, totally uncommented.  Sorry.
  4.    
  5.    David Eck
  6.    Department of Mathematics and Computer Science
  7.    Hobart and William Smith Colleges
  8.    Geneva, NY   14456
  9.    E-mail:  eck@hws.edu
  10.    WWW:     http://www.hws.edu/~eck
  11.    
  12.    June 18, 1996
  13.    
  14.    NOTE:  YOU CAN DO ANYTHING YOU WANT WITH THIS CODE, EXCEPT COPYRIGHT IT,
  15.           PATENT IT, OR OTHERWISE TRY TO CLAIM CREDIT FOR IT.
  16.           
  17. */
  18.  
  19. import java.awt.*;
  20.  
  21. class CACanvas extends Canvas {
  22.  
  23.    private boolean backedUp = true;
  24.    private Image OSC = null;
  25.    private int[] rule = null;
  26.    private int ruleCt = 8;
  27.    private int states = 2;
  28.    private int neighbors = 3;
  29.    private int halfNeighborCt = 1;
  30.    private int[] startWorld = null;
  31.    private int[] world = null;
  32.    private int[] newWorld = null;
  33.    private Color[] stateColor = null;
  34.    private int width = 0;
  35.    private int height = 0;
  36.    private int lineCt = 0;
  37.       
  38.    synchronized void properties(int stateCt, int neighborCt, 
  39.                                 int[] theRules, Color[] theColors, 
  40.                                 boolean useOffScreenCanvas) {
  41.       backedUp = useOffScreenCanvas;
  42.       states = stateCt;
  43.       if (states < 2)
  44.          states = 2;
  45.       neighbors = neighborCt;
  46.       if (neighbors % 2 == 0)
  47.         neighbors++;
  48.       if (neighborCt < 3)
  49.          neighborCt = 3;
  50.       ruleCt = 1;
  51.       for (int i=0; i<neighbors; i++) {
  52.          if (ruleCt > 32768) {
  53.             states = 2;
  54.             neighbors = 3;
  55.             ruleCt = 8;
  56.             break;
  57.          }
  58.          ruleCt = ruleCt*states;
  59.       }
  60.       halfNeighborCt = neighbors / 2;
  61.       if (theRules != null && theRules.length >= ruleCt)
  62.          rule = theRules;
  63.       else 
  64.          rule = null;
  65.       if (theColors != null && theColors.length >= states)
  66.          stateColor = theColors;
  67.       else
  68.          theColors = null;
  69.    }
  70.    
  71.    int getWidth() {
  72.      return (width == 0)? size().width : width;
  73.    }
  74.    
  75.    void disposeOSC() {
  76.       OSC = null;
  77.    }
  78.       
  79.    synchronized void set(int[] theWorld) {
  80.        if ((width != size().width || height != size().height))
  81.           OSC = null;
  82.        width = size().width;
  83.        height = size().height;
  84.        if (world == null || world.length < width)
  85.           world = new int[width];
  86.        if (newWorld == null || newWorld.length < width)
  87.           newWorld = new int[width];
  88.        if (rule == null || rule.length < ruleCt) {
  89.           rule = new int[ruleCt];
  90.           for (int i = 0; i < ruleCt; i++)
  91.              rule[i] = 1;
  92.           rule[0] = 0;
  93.           rule[ruleCt-1] = 0;
  94.        }
  95.        if (stateColor == null || stateColor.length < states) {
  96.           stateColor = new Color[states];
  97.           stateColor[0] = Color.white;
  98.           stateColor[1] = Color.black;
  99.           if (states>2) {
  100.              float hue = 0.0F;
  101.              for (int i = 2; i < states; i++) {
  102.                  stateColor[i] = Color.getHSBColor(hue,1.0F,1.0F); 
  103.                  hue = hue + (1.0F / (states - 2));
  104.              }
  105.           }
  106.        }
  107.        if (theWorld == null) {
  108.           for (int i=0; i<width; i++)
  109.              world[i] = 0;
  110.           world[width/2] = 1;
  111.        }
  112.        else {
  113.           for (int i=0; i<width; i++)
  114.              if (i < theWorld.length && theWorld[i] >= 0 && theWorld[i] < states)
  115.                 world[i] = theWorld[i];
  116.              else
  117.                world[i] = 0;
  118.        }
  119.        if (startWorld == null || startWorld.length < width)
  120.           startWorld = new int[width];
  121.        for (int i = 0; i<width; i++)
  122.           startWorld[i] = world[i];
  123.        if (backedUp && OSC == null)
  124.            OSC = createImage(width,height);
  125.        clear();
  126.    }
  127.    
  128.    void reset() {
  129.       if (startWorld != null) {
  130.           if (world == null || world.length < width)
  131.              world = new int[width];
  132.           for (int i=0; i<width; i++)
  133.              if (i < startWorld.length && startWorld[i] >= 0 && startWorld[i] < states)
  134.                 world[i] = startWorld[i];
  135.              else
  136.                world[i] = 0;
  137.          clear();
  138.       }
  139.    }
  140.    
  141.    void setStart() {
  142.       if (world != null) {
  143.          if (startWorld == null || startWorld.length < world.length)
  144.             startWorld = new int[world.length];
  145.          for (int i=0; i<world.length; i++)
  146.             startWorld[i] = world[i];
  147.       }
  148.    }
  149.    
  150.    synchronized void setCell(int cellNum, int state) {
  151.        if (world != null && cellNum >= 0 && cellNum < world.length && state >= 0 && state < states)
  152.             world[cellNum] = state;
  153.    }
  154.    
  155.    synchronized void clear() {
  156.       if (OSC != null) {
  157.          Graphics g = OSC.getGraphics();
  158.          if (stateColor != null)
  159.             g.setColor(stateColor[0]);
  160.          else
  161.             g.setColor(Color.black);
  162.          g.fillRect(0,0,width,height);
  163.          g.dispose();
  164.          repaint();
  165.       }
  166.       else {
  167.          Graphics g = getGraphics();
  168.          if (stateColor != null)
  169.             g.setColor(stateColor[0]);
  170.          else
  171.             g.setColor(Color.black);
  172.          g.fillRect(0,0,width,height);
  173.          g.dispose();
  174.       }
  175.       lineCt = 0;
  176.    }
  177.    
  178.    synchronized void  setRules(int[] theRules) {
  179.       if (theRules.length >= ruleCt)
  180.          rule = theRules;
  181.    }
  182.    
  183.    synchronized void  setRule(int ruleNum, int value) {
  184.       if ((rule != null) && (value >= 0) && (value < states) && (ruleNum >= 0) && (ruleNum < rule.length))
  185.          rule[ruleNum] = value;
  186.    }
  187.    
  188.    synchronized void  setColors(Color[] theColors) {
  189.       if (theColors.length >= states)
  190.          stateColor = theColors;
  191.    }
  192.  
  193.    synchronized void  setColor(int colorNum, Color value) {
  194.       if ((stateColor != null) && (value != null) && (colorNum >= 0) && (colorNum < stateColor.length))
  195.          stateColor[colorNum] = value;
  196.    }
  197.    
  198.    public void update(Graphics g) {
  199.       paint(g);
  200.    }
  201.    
  202.    public void paint(Graphics g) {
  203.       if (OSC != null)
  204.          g.drawImage(OSC,0,0,this);
  205.    }
  206.    
  207.    synchronized void putWorld() {
  208.       Graphics g=null;
  209.       if (OSC == null)
  210.          g = getGraphics();
  211.       else
  212.          g = OSC.getGraphics();
  213.       if (lineCt >= height && g != null) {
  214.          g.copyArea(0,0,width,height,0,-1);
  215.          lineCt = height-1;
  216.       }
  217.       if (g != null) {
  218.          for (int i=0; i< width; i++) {
  219.             g.setColor(stateColor[world[i]]);
  220.             g.fillRect(i,lineCt,1,1);
  221.          }
  222.          lineCt++;
  223.          g.dispose();
  224.       }
  225.       if (OSC != null) {
  226.          g = getGraphics();
  227.          if (g != null && OSC != null) {
  228.             g.drawImage(OSC,0,0,this);
  229.             g.dispose();
  230.          }
  231.       }
  232.    }
  233.    
  234.    synchronized void next() {
  235.       if (width > 0) {
  236.         for (int i=0; i < width; i++) {
  237.            int left = i - halfNeighborCt;
  238.            if (left<0)
  239.               left = width+left;
  240.            int right = i + halfNeighborCt;
  241.            if (right>=width)
  242.               right = right-width;
  243.            int k = 0;
  244.            if (left < right) {
  245.               for (int j=left; j<=right; j++)
  246.                  k = states*k + world[j];
  247.            }
  248.            else {
  249.               for (int j=left; j<width; j++)
  250.                 k = states*k + world[j];
  251.               for (int j=0; j<=right; j++)
  252.                 k = states*k + world[j];
  253.            }
  254.            newWorld[i] = rule[k];
  255.          }
  256.          putWorld();
  257.          for (int i = 0; i < width; i++)
  258.             world[i] = newWorld[i];
  259.       }
  260.    }   // next()
  261.    
  262. }  // class CACanvas
  263.