home *** CD-ROM | disk | FTP | other *** search
Java Source | 1998-12-14 | 3.6 KB | 161 lines |
- import java.applet.Applet;
- import java.awt.*;
-
- /**
- * class LineColors holds 24 color values
- */
- class LineColors {
-
- /**
- * color[] array holds the colors to be used
- */
- Color color[];
-
- /**
- * class constructor
- * initializes the color array 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 describing one line segment
- */
- class Segment {
-
- /*
- * x1, y1 - starting coordinates for this segment
- * x2, y2 - ending coordinates for this segment
- * dx1,...dy2 - velocities for the endpoints
- * whichcolor - the current index into color array
- * width, height - width and height of bounding panel
- * LC - instance of LineColors class
- */
- double x1, y1, x2, y2;
- double dx1, dy1, dx2, dy2;
- int whichcolor, width, height;
- LineColors LC;
-
- /**
- * class constructor
- * initialize endpoints and velocities to random values
- * @param w - width of bounding panel
- * @param h - height of bounding panel
- * @param c - starting color index
- * @param lc - instance of LineColors class
- */
- 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 ();
- }
-
- /*
- * increment color index
- * calculate the next endpoint position for this segment
- */
- 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;
- }
-
- /**
- * draw the line segment using the current color
- * @param g - destination graphics object
- */
- void paint (Graphics g) {
-
- g.setColor (LC.color [whichcolor]);
- g.drawLine ((int) x1, (int) y1, (int) x2, (int) y2);
- }
- }
-
- /**
- * The applet/application proper
- */
- public class Lines extends Applet {
-
- /*
- * Nlines - number of line segments to be displayed
- * lines - array of instances of Segment class
- * LC - instance of LineColors class
- */
- int width,height;
- final int NLines = 4;
- Segment lines[] = new Segment[NLines];
- LineColors LC = new LineColors ();
-
- /**
- * init is called when the applet is loaded
- * save the width and height
- * create instances of Segment class
- */
- public void init () {
-
- setLayout(null);
- width = 300;
- height = 300;
- setSize(width, height);
-
- int i;
- for (i=0; i<NLines; i+=1)
- lines[i] = new Segment (width,
- σheight, (2*i) % 24, LC);
- }
-
- /**
- * recompute the next endpoint coordinates for each line
- * invoke paint() method for each line
- * call repaint() to force painting 50ms. later
- * @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);
- }
- repaint (50);
- }
-
- }
-
-
-