home *** CD-ROM | disk | FTP | other *** search
Java Source | 1998-12-14 | 3.7 KB | 176 lines |
- import java.applet.Applet;
- import java.awt.*;
-
- /*
- * class to hold color values
- */
- class LineColors {
-
- /*
- * an array of colors proper
- */
- Color color[];
-
- /*
- * the constructor initializes the color array by
- * using an arbitrary algorithm
- */
- public LineColors () {
-
- color = new Color[24];
- int i, rgb;
-
- rgb = 0xff;
- for (i=0; i<24; i+=1) {
- color[i] = new Color (rgb);
- rgb <<= 1;
- if ((rgb & 0x1000000) != 0) {
- rgb |= 1;
- rgb &= 0xffffff;
- }
- }
- }
- } // class LineColors
-
- /*
- * class to handle the drawing of one line segment
- */
- class Segment {
-
- /*
- * x1, y1 - x and y position of first endpoint
- * x2, y2 - x and y position of second endpoint
- */
- double x1, y1, x2, y2;
-
- /*
- * velocities of the endpoints, respectively
- */
- double dx1, dy1, dx2, dy2;
-
- /*
- * whichcolor - color index for this segment
- */
- int whichcolor;
-
- /*
- * width and height of bounding panel
- */
- int width, height;
-
- /*
- * instance of LineColors
- */
- LineColors LC;
-
- /*
- * class constructor
- * saves arguments and initializes position and velocities
- * to random values
- * @param w, h - width and height of bounding panel
- * @param c - starting color
- * @param lc - instance of LineColor
- */
- public Segment (int w, int h, int c, LineColors lc) {
-
- whichcolor = c;
- width = w;
- height = h;
- LC = lc;
- x1 = (double) w * Math.random ();
- y1 = (double) h * Math.random ();
- x2 = (double) w * Math.random ();
- y2 = (double) h * Math.random ();
-
- dx1 = 5 - 10 * Math.random ();
- dy1 = 5 - 10 * Math.random ();
- dx2 = 5 - 10 * Math.random ();
- dy2 = 5 - 10 * Math.random ();
- }
-
- /*
- * increments color index and calculates new endpoint positions
- */
- void compute () {
-
- whichcolor += 1;
- whichcolor %= 24;
-
- x1 += dx1;
- y1 += dy1;
- x2 += dx2;
- y2 += dy2;
-
- if (x1 < 0 || x1 > width) dx1 = -dx1;
- if (y1 < 0 || y1 > height) dy1 = -dy1;
- if (x2 < 0 || x2 > width) dx2 = -dx2;
- if (y2 < 0 || y2 > height) dy2 = -dy2;
- }
-
- /**
- * prints status message showing the different colors
- * @param g - destination graphics object
- */
- void paint (Graphics g) {
-
- g.setColor (LC.color [whichcolor]);
- g.drawLine ((int) x1, (int) y1, (int) x2, (int) y2);
- }
- } // class Segment
-
- public class Status extends Applet {
-
- /*
- * width and height of bounding panel
- */
- int width, height;
-
- /*
- * The number of lines will be set to 1 because the color values
- * displayed will be valid for only one line
- */
- final int NLines = 1;
-
- /*
- * array of instances of Segment
- */
- Segment lines[] = new Segment[NLines];
-
- /*
- * instance of LineColor
- */
- LineColors LC = new LineColors ();
-
- /*
- * called when applet is loaded
- * save panel dimensions and create instance of Segment
- */
- public void init () {
-
-
- width = 200;
- height = 200;
-
- int i;
- for (i=0; i<NLines; i+=1)
- lines[i] = new Segment (width, height, (2*i) % 24, LC);
- }
-
- /**
- * draw the line and print status message
- * @param g - destination graphics object
- */
- public void paint (Graphics g) {
-
- int i;
- for (i=0; i<NLines; i+=1) {
- lines[i].compute ();
- lines[i].paint (g);
- }
- showStatus("red = "+g.getColor().getRed() + " green = " +
- g.getColor().getGreen() + " blue = " +
- g.getColor().getBlue());
-
- repaint (50);
- }
- }