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

  1.  
  2. /*******************************************************************************
  3.  
  4. class Moire1 implements an animated Moire pattern.  A Moire patterns occurs when
  5. two similar patters are almost superimposed.  There is a kind of visual appearance.
  6. In this case, the pattern consists of lines radiating out from a common center.
  7. one such pattern is drawn with the lines radiating out from the center of the
  8. applet.  A second pattern has a center that drifts about, producing a changing
  9. interference pattern.
  10.  
  11. You can also click and drag on the applet to move the second pattern about by
  12. hand.
  13.  
  14. Several parameters can be set by <param> tags:
  15.  
  16.     Name       Default    Legal values     Meaning 
  17.     ---------  ---------  ---------------  -----------------------------------
  18.     lineCount   36         1 to 100         Number of lines drawn in each set
  19.                                                of lines.  (Note:  each "line"
  20.                                                gives two "spokes" radiating from
  21.                                                the center.)
  22.     lineColor   red        any color        Color of the lines.
  23.     bgColor     cyan       any color        Color seen behind the lines.
  24.     sleepTime   25         1 to 5000        Time, in milliseconds, between
  25.                                                movements of the pattern; smaller
  26.                                                values give faster movements.
  27.     border      0          0 to 50          Width of border drawn around the
  28.                                                Moire pattern; note that the
  29.                                                default is to have no border.
  30.     borderColor blue       any color        Color of the border.
  31.     
  32. Note that a color can be specified either as a set of three integers between
  33. 0 and 255, giving the red, blue, and green components of the color, or it
  34. can be specified as one of the built-in color names: white, black, red, green,
  35. blue, yellow, cyan, magneta, pink, orange, gray, lightGray, or darkGray.
  36. Color names are not case sensitive, although param names are.
  37.  
  38. Note:  You might find the method getColorParam() useful in other applications.
  39.  
  40. BY:  David Eck
  41.      Department of Mathematics and Computer Science
  42.      Hobart and William Smith Colleges
  43.      Geneva, NY   14456
  44.      
  45.      E-mail:  eck@hws.edu
  46.  
  47.  
  48. NOTE:  YOU CAN DO ANYTHING YOU WANT WITH THIS CODE AND APPLET, EXCEPT
  49.        TRY TO COPYRIGHT OR PATENT THEM YOURSELF.
  50.  
  51. *******************************************************************************/
  52.  
  53.  
  54. import java.awt.*;
  55. import java.util.Random;
  56.  
  57. public class Moire1 extends java.applet.Applet implements Runnable {
  58.  
  59.    static final Random rand = new Random();
  60.  
  61.    Thread runner;   // thread to produce the animation
  62.  
  63.    Image buffer = null;  // an off-screen canvas
  64.    int w,h;       // width and height of the buffer
  65.    double center_x, center_y;   // current position of the center of
  66.                                 // the second set of lines.
  67.    int cx,cy;  // for use in mouseDrag, mouseExit;
  68.                // position of center during dragging
  69.  
  70.    
  71.    int mouseStart_x, mouseStart_y;   // used during dragging to hold the
  72.                                      // location of the original mouse click
  73.    boolean dragging = false;    // set to "true" while dragging is in
  74.                                 // progress, as signal to "run" method
  75.                                 // to pause the regular animation.
  76.    boolean stopped = false;   // toggled when user shift-clicks on the
  77.                               // applet.
  78.    
  79.    int sleepTime = 25;             // applet <param>'s, described above
  80.    Color backColor = Color.cyan;
  81.    Color lineColor = Color.red;
  82.    int lines = 36;
  83.    int border = 0;
  84.    Color borderColor = Color.blue;
  85.       
  86.    double[] cos, sin;  // hold sines and cosines of angle; one for each line
  87.                        // line to be drawn.  This just avoids recomputing this
  88.                        // all the time.
  89.    
  90.    public void init() {
  91.       Color c;
  92.       Integer temp;
  93.       if ( (c = getColorParam("bgColor")) != null )
  94.          backColor = c;
  95.       if ( (c = getColorParam("lineColor")) != null )
  96.          lineColor = c;
  97.       if ( (temp = getIntParam("lineCount")) != null )
  98.          lines = temp.intValue();
  99.       if (lines < 1 || lines > 100)
  100.          lines = 36;
  101.       if ( (temp = getIntParam("sleepTime")) != null )
  102.          sleepTime = temp.intValue();
  103.       if (sleepTime < 1 || sleepTime > 5000)
  104.          sleepTime = 25;
  105.       if ( (temp = getIntParam("border")) != null )
  106.          border = temp.intValue();
  107.       if (border < 0 || border > 50)
  108.          border = 0;
  109.       if ( (c = getColorParam("borderColor")) != null )
  110.          borderColor = c;
  111.       setBackground(borderColor);
  112.       double delta = 180.0 / lines;
  113.       cos = new double[lines];
  114.       sin = new double[lines];
  115.       for (int i = 0; i < lines; i++) {
  116.          double angle = ((i * delta * Math.PI)/180.0);
  117.          cos[i] = Math.cos(angle);
  118.          sin[i] = Math.sin(angle);  
  119.       }
  120.    }
  121.    
  122.     Integer getIntParam(String paramName) {
  123.        // retrieve an integer <param>; return null if the specified
  124.        // param is not present or if the value is illegal
  125.        String param = getParameter(paramName);
  126.        if (param == null)
  127.           return null;
  128.        int i;
  129.        try {
  130.           i = Integer.parseInt(param);
  131.        }
  132.        catch (NumberFormatException e) {
  133.           return null;
  134.        }
  135.        return new Integer(i);
  136.     }
  137.     
  138.     Color getColorParam(String paramName) {
  139.        // retrieve a color <param>; return null if the specified
  140.        // param is not present or if the value is illegal.  Legal
  141.        // values include the 13 named Java colors ("red", "black",
  142.        // etc.) and RGB values given as 3 integers between 0 and 255.
  143.        // Color names are not case sensitive.  Integers in RGB
  144.        // colors can be separated by any non-digit characters.
  145.        String param = getParameter(paramName);
  146.        if (param == null || param.length() == 0)
  147.           return null;
  148.        if (Character.isDigit(param.charAt(0))) {  // try to parse RGB color
  149.           int r=0,g=0,b=0;
  150.           int pos=0;
  151.           int d=0;
  152.           int len=param.length();
  153.           while (pos < len && Character.isDigit(param.charAt(pos)) && r < 255) {
  154.               d = Character.digit(param.charAt(pos),10);
  155.               r = 10*r + d;
  156.               pos++;
  157.           }
  158.           if (r > 255)
  159.              return null;
  160.           while (pos < len && !Character.isDigit(param.charAt(pos)))
  161.              pos++;
  162.           if (pos >= len)
  163.              return null;
  164.           while (pos < len && Character.isDigit(param.charAt(pos)) && g < 255) {
  165.               d = Character.digit(param.charAt(pos),10);
  166.               g = 10*g + d;
  167.               pos++;
  168.           }
  169.           if (g > 255)
  170.              return null;
  171.           while (pos < len && !Character.isDigit(param.charAt(pos)))
  172.              pos++;
  173.           if (pos >= len)
  174.              return null;
  175.           while (pos < len && Character.isDigit(param.charAt(pos)) && b < 255) {
  176.               d = Character.digit(param.charAt(pos),10);
  177.               b = 10*b + d;
  178.               pos++;
  179.           }
  180.           if (b > 255)
  181.              return null;
  182.           return new Color(r,g,b);          
  183.        }
  184.        param.toLowerCase();
  185.        if (param.equals("black"))
  186.           return Color.black;
  187.        if (param.equals("white"))
  188.           return Color.white;
  189.        if (param.equals("red"))
  190.           return Color.red;
  191.        if (param.equals("green"))
  192.           return Color.green;
  193.        if (param.equals("blue"))
  194.           return Color.blue;
  195.        if (param.equals("yellow"))
  196.           return Color.yellow;
  197.        if (param.equals("cyan"))
  198.           return Color.cyan;
  199.        if (param.equals("magenta"))
  200.           return Color.magenta;
  201.        if (param.equals("pink"))
  202.           return Color.pink;
  203.        if (param.equals("orange"))
  204.           return Color.orange;
  205.        if (param.equals("gray"))
  206.           return Color.gray;
  207.        if (param.equals("darkgray"))
  208.           return Color.darkGray;
  209.        if (param.equals("lightgray"))
  210.           return Color.l