home *** CD-ROM | disk | FTP | other *** search
/ Java 1.2 How-To / JavaHowTo.iso / javafile / ch11 / Lines.java < prev    next >
Encoding:
Java Source  |  1998-12-14  |  3.4 KB  |  156 lines

  1. import java.applet.Applet;
  2. import java.awt.*;
  3.  
  4. /**
  5.  * class LineColors holds 24 color values
  6.  */
  7. class LineColors {
  8.  
  9. /**
  10.  * color[] array holds the colors to be used
  11.  */
  12. Color color[];
  13.  
  14. /**
  15.  * class constructor
  16.  * initializes the color array using an arbitrary algorithm
  17.  */
  18. public LineColors () {
  19.  
  20.     color = new Color[24];
  21.     int i, rgb;
  22.  
  23.     rgb = 0xff;
  24.     for (i=0; i<24; i+=1) {
  25.         color[i] = new Color (rgb);
  26.         rgb <<= 1;
  27.         if ((rgb & 0x1000000) != 0) {
  28.             rgb |= 1;
  29.             rgb &= 0xffffff;
  30.         }
  31.     }
  32. }
  33. }
  34.  
  35. /**
  36.  * class describing one line segment
  37.  */
  38. class Segment {
  39.  
  40. /*
  41.  * x1, y1 - starting coordinates for this segment
  42.  * x2, y2 - ending coordinates for this segment
  43.  * dx1,...dy2 - velocities for the endpoints
  44.  * whichcolor - the current index into color array
  45.  * width, height - width and height of bounding panel
  46.  * LC - instance of LineColors class
  47.  */
  48. double x1, y1, x2, y2;
  49. double dx1, dy1, dx2, dy2;
  50. int whichcolor, width, height;
  51. LineColors LC; 
  52.  
  53. /**
  54.  * class constructor
  55.  * initialize endpoints and velocities to random values
  56.  * @param w - width of bounding panel
  57.  * @param h - height of bounding panel
  58.  * @param c - starting color index
  59.  * @param lc - instance of LineColors class
  60.  */
  61. public Segment (int w, int h, int c, LineColors lc) {
  62.  
  63.     whichcolor = c;
  64.     width = w;
  65.     height = h;
  66.     LC = lc;
  67.     x1 = (double) w * Math.random ();
  68.     y1 = (double) h * Math.random ();
  69.     x2 = (double) w * Math.random ();
  70.     y2 = (double) h * Math.random ();
  71.  
  72.     dx1 = 5 - 10 * Math.random ();
  73.     dy1 = 5 - 10 * Math.random ();
  74.     dx2 = 5 - 10 * Math.random ();
  75.     dy2 = 5 - 10 * Math.random ();
  76. }
  77.  
  78. /*
  79.  * increment color index
  80.  * calculate the next endpoint position for this segment
  81.  */
  82. void compute () {
  83.  
  84.     whichcolor += 1;
  85.     whichcolor %= 24;
  86.  
  87.     x1 += dx1;
  88.     y1 += dy1;
  89.     x2 += dx2;
  90.     y2 += dy2;
  91.  
  92.     if (x1 < 0 || x1 > width) dx1 = -dx1;
  93.     if (y1 < 0 || y1 > height) dy1 = -dy1;
  94.     if (x2 < 0 || x2 > width) dx2 = -dx2;
  95.     if (y2 < 0 || y2 > height) dy2 = -dy2;
  96. }
  97.  
  98. /**
  99.  * draw the line segment using the current color
  100.  * @param g - destination graphics object
  101.  */
  102. void paint (Graphics g) {
  103.  
  104.     g.setColor (LC.color [whichcolor]);
  105.     g.drawLine ((int) x1, (int) y1, (int) x2, (int) y2);
  106. }
  107. }
  108.  
  109. /**
  110.  * The applet/application proper
  111.  */
  112. public class Lines extends Applet {
  113.  
  114. /*
  115.  * Nlines - number of line segments to be displayed
  116.  * lines - array of instances of Segment class
  117.  * LC - instance of LineColors class
  118.  */
  119. int width,height;
  120. final int NLines = 4;
  121. Segment lines[] = new Segment[NLines];
  122. LineColors LC = new LineColors ();
  123.  
  124. /**
  125.  * init is called when the applet is loaded
  126.  * save the width and height
  127.  * create instances of Segment class
  128.  */
  129. public void init () {
  130.  
  131.     setLayout(null);
  132.     width = 300;
  133.     height = 300;
  134.     setSize(width, height);
  135.  
  136.     int i;
  137.     for (i=0; i<NLines; i+=1)
  138.         lines[i] = new Segment (width, height, (2*i) % 24, LC);
  139. }
  140.  
  141. /**
  142.  * recompute the next endpoint coordinates for each line
  143.  * invoke paint() method for each line
  144.  * call repaint() to force painting 50ms later
  145.  * @param g - destination graphics object
  146.  */
  147. public void paint (Graphics g) {
  148.  
  149.     int i;
  150.     for (i=0; i<NLines; i+=1) {
  151.         lines[i].compute ();
  152.         lines[i].paint (g);
  153.     }
  154.     repaint (50); 
  155. }
  156.