home *** CD-ROM | disk | FTP | other *** search
/ Internet Publisher's Toolbox 2.0 / Internet Publisher's Toolbox.iso / java / applets / pythag~1 / serp~1.jav < prev   
Encoding:
Text File  |  1995-10-31  |  5.7 KB  |  175 lines

  1. /*----------------------------------------------------------------------*/
  2.  
  3. /*----------------------------------------------------------------------*/
  4. /* serp.java -- draws the 1-7th itteration of Serpinski triangle        */
  5. /*----------------------------------------------------------------------*/
  6. /*                    Jim Morey - morey@math.ubc.ca - July              */
  7. /*----------------------------------------------------------------------*/
  8.  
  9. import java.io.InputStream;
  10. import java.awt.*;
  11. import java.net.*;
  12.  
  13. public class serp extends java.applet.Applet implements Runnable{
  14.   private int size,max_lev,max_turn,step,lev;  
  15.   private int turn[];
  16.   private double TT=3.1415f;
  17.   private int MAX = 7;
  18.   private int scr_x, scr_y, scr_x_last, scr_y_last;
  19.   private double x,y,len,angle;
  20.   Color gcol = null;
  21.   Graphics    offscreen;
  22.   Image       im;
  23.   Thread kicker = null;
  24.   boolean im_ready=false;
  25.  
  26.   /* -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  - */
  27.   public void init() {
  28.     try{
  29.       size = Integer.parseInt(getParameter("size"));
  30.     } catch(Exception e) { size = 200; }
  31.  
  32.     try{
  33.       max_lev = Integer.parseInt(getParameter("levels"));
  34.     } catch(Exception e) { max_lev = 3; }
  35.  
  36.     String colour = getParameter("colour");
  37.     if (colour != null){
  38.       if (colour.equals("white")) gcol = java.awt.Color.white;
  39.       else if (colour.equals("red")) gcol = java.awt.Color.red;
  40.       else if (colour.equals("green")) gcol = java.awt.Color.green;
  41.       else if (colour.equals("blue")) gcol = java.awt.Color.blue;
  42.     }
  43.     else gcol = java.awt.Color.black;
  44.  
  45.     max_turn = 3;
  46.     
  47.     resize(size+2, (int)(size*Math.sin(TT/3))+1);
  48.   }
  49.  
  50.   /* -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  - */
  51.   public void start() {
  52.     if (kicker == null) {
  53.       kicker = new Thread(this);
  54.       kicker.start();
  55.     }
  56.   }
  57.  
  58.   /* -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  - */
  59.   public void stop() {
  60.     kicker = null;
  61.   }
  62.  
  63.   /* -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  - */
  64.   public void run() {                                                         
  65.     Thread.currentThread().setPriority(Thread.MIN_PRIORITY);
  66.  
  67.     /* .. get a blank window to draw the triangle .. */ 
  68.     im = createImage(size().width, size().height);
  69.     offscreen = im.getGraphics();
  70.     offscreen.setColor(Color.lightGray);
  71.     offscreen.fillRect(0, 0, size().width, size().height);
  72.     if (gcol != null) offscreen.setColor(gcol);
  73.     else offscreen.setColor(Color.black);
  74.     im_ready = true;
  75.  
  76.     turn = new int[MAX];
  77.     int speed[] = {256,128,64,32,16,8,8,8,8,8,8,8};
  78.  
  79.     /* .. This part seems like a logo program since the drawing is done 
  80.           without lifting the pen.  The angle, len, x and y tell you everything
  81.           about the "turtle" including it's size.  After taking a step the 
  82.           turtle changes it's attributes according to the algorithm and the 
  83.           stack (turn & lev).  .. */
  84.     len = size/4;
  85.     angle = TT/3;
  86.     x = size/2+1 ;
  87.     y = 1;
  88.     scr_x_last = (int)x;
  89.     scr_y_last = (int) (size().height - y);
  90.     scr_x = 0;
  91.     scr_y = 0;
  92.     step = 0;
  93.     lev = 0;
  94.     turn[lev] = 0;
  95.  
  96.     /* .. draw triangle .. */
  97.     offscreen.drawLine(size,scr_y_last,scr_x_last,scr_y_last);
  98.     repaint();
  99. try {Thread.sleep(speed[0]);} catch (InterruptedException e){}
  100.     offscreen.drawLine(size,scr_y_last,scr_x_last,1);
  101.     repaint();
  102. try {Thread.sleep(speed[0]);} catch (InterruptedException e){}
  103.     offscreen.drawLine(1,scr_y_last,scr_x_last,1);
  104.     repaint();
  105.     offscreen.drawLine(1,scr_y_last,scr_x_last,scr_y_last);
  106. try {Thread.sleep(speed[0]);} catch (InterruptedException e){}
  107.     repaint();
  108.  
  109.     while (kicker != null && max_lev != 0 ) {
  110.       /* .. take a step .. */
  111.       x = len*Math.cos(angle) + x;
  112.       y = len*Math.sin(angle) + y;
  113.       scr_x = (int)x;
  114.       scr_y = (int)(size().height - y);
  115.  
  116.       /* .. add the next line to the picture .. */
  117.       offscreen.drawLine(scr_x,scr_y,scr_x_last,scr_y_last);
  118.       repaint();
  119.  
  120.       scr_x_last = scr_x;
  121.       scr_y_last = scr_y;
  122.  
  123.       if (step == 0){  /* .. the first step .. */
  124.         if (lev == max_lev-1){ /* .. finished getting smaller? .. */
  125.           step = 1; 
  126.         } else{ /* .. turn in and shrink .. */
  127.           lev = lev + 1;
  128.           turn[lev] = 0;
  129.           angle = angle - (2*TT)/3;
  130.           len = len/2;
  131.         }
  132.       } else{  /* .. the second step .. */
  133.         if (turn[lev] == max_turn-1){
  134.           if (lev == 0) { /* .. back at the start? .. */
  135.             stop();
  136.           } else { /* .. get bigger & turn back (same as turn in) .. */
  137.             lev = lev - 1;
  138.             len = len * 2;
  139.             angle = angle - (2*TT)/3;
  140.           }
  141.         } else{ /* .. make a turn (this is a turn out) .. */
  142.           step = 0; 
  143.           turn[lev] = turn[lev] + 1;
  144.           angle = angle + (2*TT)/3;
  145.         } 
  146.       }    
  147. try {Thread.sleep(speed[max_lev]);} catch (InterruptedException e){}
  148.     }
  149.   }
  150.                         
  151.   /* -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  - */
  152.   public void paint(Graphics g) {
  153.     update(g);
  154.   }
  155.   public void update(Graphics g) {
  156.     if (im_ready) g.drawImage(im, 0, 0, this);
  157.   }
  158.  
  159.   /* -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  - */
  160.   public boolean mouseDown(java.awt.Event evt, int x, int y) {
  161.     /* .. restart the program with a different level (or depth)  .. */
  162.     stop();
  163.     max_lev = max_lev+1;
  164.     if (max_lev == MAX) max_lev = 0;
  165.  
  166. try {Thread.sleep(300);  /* .. to make sure the program stops .. */} catch (InterruptedException e){}
  167.     start();
  168.     return true;
  169.   }
  170.  
  171. }
  172.  
  173.  
  174.  
  175.