home *** CD-ROM | disk | FTP | other *** search
/ Java 1.2 How-To / JavaHowTo.iso / javafile / ch03 / Status.java < prev   
Encoding:
Java Source  |  1998-12-14  |  3.7 KB  |  176 lines

  1. import java.applet.Applet;
  2. import java.awt.*;
  3.  
  4. /*
  5.  * class to hold color values
  6.  */
  7. class LineColors {
  8.  
  9. /*
  10.  * an array of colors proper
  11.  */
  12. Color color[];
  13.  
  14. /*
  15.  * the constructor initializes the color array by 
  16.  * 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. } // class LineColors
  34.  
  35. /*
  36.  * class to handle the drawing of one line segment
  37.  */
  38. class Segment {
  39.  
  40. /*
  41.  * x1, y1 - x and y position of first endpoint
  42.  * x2, y2 - x and y position of second endpoint
  43.  */
  44. double x1, y1, x2, y2;
  45.  
  46. /*
  47.  * velocities of the endpoints, respectively
  48.  */
  49. double dx1, dy1, dx2, dy2;
  50.  
  51. /*
  52.  * whichcolor - color index for this segment
  53.  */
  54. int whichcolor; 
  55.  
  56. /*
  57.  * width and height of bounding panel
  58.  */
  59. int width, height;
  60.  
  61. /*
  62.  * instance of LineColors
  63.  */
  64. LineColors LC;
  65.  
  66. /*
  67.  * class constructor
  68.  * saves arguments and initializes position and velocities
  69.  * to random values
  70.  * @param w, h - width and height of bounding panel
  71.  * @param c - starting color
  72.  * @param lc - instance of LineColor
  73.  */
  74. public Segment (int w, int h, int c, LineColors lc) {
  75.  
  76.         whichcolor = c;
  77.         width = w;
  78.         height = h; 
  79.         LC = lc;
  80.         x1 = (double) w * Math.random ();
  81.         y1 = (double) h * Math.random ();
  82.         x2 = (double) w * Math.random ();
  83.         y2 = (double) h * Math.random ();
  84.  
  85.         dx1 = 5 - 10 * Math.random ();
  86.         dy1 = 5 - 10 * Math.random ();
  87.         dx2 = 5 - 10 * Math.random ();
  88.         dy2 = 5 - 10 * Math.random ();
  89. }
  90.  
  91. /*
  92.  * increments color index and calculates new endpoint positions
  93.  */
  94. void compute () {
  95.  
  96.         whichcolor += 1;
  97.         whichcolor %= 24;
  98.         
  99.         x1 += dx1;
  100.         y1 += dy1;
  101.         x2 += dx2;
  102.         y2 += dy2;
  103.  
  104.         if (x1 < 0 || x1 > width) dx1 = -dx1;
  105.         if (y1 < 0 || y1 > height) dy1 = -dy1;
  106.         if (x2 < 0 || x2 > width) dx2 = -dx2;
  107.         if (y2 < 0 || y2 > height) dy2 = -dy2; 
  108. }
  109.  
  110. /**
  111.  * prints status message showing the different colors
  112.  * @param g - destination graphics object
  113.  */
  114. void paint (Graphics g) {
  115.  
  116.         g.setColor (LC.color [whichcolor]);
  117.         g.drawLine ((int) x1, (int) y1, (int) x2, (int) y2);
  118. }
  119. } // class Segment
  120.  
  121. public class Status extends Applet {
  122.  
  123. /*
  124.  * width and height of bounding panel
  125.  */
  126. int width, height;
  127.  
  128. /*
  129.  * The number of lines will be set to 1 because the color values
  130.  * displayed will be valid for only one line
  131.  */
  132. final int NLines = 1;
  133.  
  134. /*
  135.  * array of instances of Segment
  136.  */
  137. Segment lines[] = new Segment[NLines];
  138.  
  139. /*
  140.  * instance of LineColor
  141.  */
  142. LineColors LC = new LineColors ();
  143.  
  144. /*
  145.  * called when applet is loaded
  146.  * save panel dimensions and create instance of Segment
  147.  */
  148. public void init () {
  149.  
  150.        
  151.         width = 200;
  152.         height = 200; 
  153.         
  154.         int i;
  155.         for (i=0; i<NLines; i+=1)
  156.                 lines[i] = new Segment (width, height, (2*i) % 24, LC);
  157. }
  158.  
  159. /**
  160.  * draw the line and print status message
  161.  * @param g - destination graphics object
  162.  */
  163. public void paint (Graphics g) {
  164.  
  165.         int i; 
  166.         for (i=0; i<NLines; i+=1) {
  167.                 lines[i].compute ();
  168.                 lines[i].paint (g);
  169.         }
  170.         showStatus("red = "+g.getColor().getRed() + "  green = " +
  171.                 g.getColor().getGreen() + "  blue = " +
  172.                 g.getColor().getBlue());
  173.  
  174.         repaint (50);
  175. }
  176. }