home *** CD-ROM | disk | FTP | other *** search
Text File | 1998-12-14 | 28.1 KB | 1,289 lines |
- Sine.java:
-
- import java.applet.Applet;
- import java.awt.*;
-
- /**
- * Sine curve applet/application
- * Draws one cycle of a sine curve.
- */
- public class Sine extends Applet {
-
- /*
- * width and height of the applet panel
- */
- int width, height;
-
- /*
- * init() is called when the applet is loaded
- * just get the width and height and save it
- */
- public void init () {
-
- setLayout(null);
- width = 300;
- height = 300;
- setSize(width, height);
- }
-
- /**
- * paint() does the drawing of the axes and sine curve
- * @param g - destination graphics object
- */
- public void paint (Graphics g) {
-
- int x, x0;
- double y0, y, A, f, t, offset;
-
- A = (double) height / 4;
- f = 2;
- offset = (double) height / 2;
- x0 = 0;
- y0 = offset;
-
- g.drawLine (x0, (int) y0, width, (int) y0);
- g.drawLine (width/2, 0, width/2, height);
- for (x=0; x<width; x+=1) {
- t = (double) x / ((double) width);
- y = offset - A * Math.sin (2 * Math.PI * f * t);
- g.drawLine (x0, (int) y0, x, (int) y);
- x0 = x;
- y0 = y;
- }
- }
- }
-
-
-
-
-
-
- Lines.java:
-
- 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);
- }
-
- }
-
-
-
-
-
-
-
-
- DrawApp.java:
-
- import java.applet.Applet;
- import java.awt.*;
- import java.awt.event.*;
- import java.io.*;
- import java.util.StringTokenizer;
-
- /**
- * class describing a shape
- */
- class Shape {
-
- /**
- * constants for the shape type
- */
- static final int rectType = 1;
- static final int ovalType = 2;
- static final int arcType = 3;
- static final int polyType = 4;
-
- /*
- * the shape type
- */
- int type;
-
- /*
- * color for this shape
- */
- Color color;
- static final int MaxPoints = 10;
-
- /*
- * arrays of x and y points for this shape
- */
- int xp[] = new int[MaxPoints];
- int yp[] = new int[MaxPoints];
-
- /*
- * the number of points in this shape
- */
- int npoints;
-
- /**
- * shape constructor
- * saves parameters
- * @param tp - shape type
- * @param n - number of points
- * @param pts[] - array of endpoints
- * @param c - color of the shape
- */
- public Shape (int tp, int n, int pts[], Color c) {
-
- int i;
- type = tp;
- color = c;
- npoints = n < MaxPoints ? n : MaxPoints;
- if (type == polyType) {
- npoints >>= 1;
- for (i=0; i<npoints; i+=1) {
- xp[i] = pts[i << 1];
- yp[i] = pts[(i << 1) +1];
- }
- } else {
- for (i=0; i<npoints; i+=1)
- xp[i] = pts[i];
- }
- }
-
- /**
- * draw the shape
- * @param g - destination graphics object
- */
- void paint (Graphics g) {
-
- g.setColor (color);
- switch (type) {
-
- case rectType:
- g.drawRect (xp[0], xp[1], xp[2], xp[3]);
- break;
-
- case ovalType:
- g.drawOval (xp[0], xp[1], xp[2], xp[3]);
- break;
-
- case arcType:
- g.drawArc (xp[0], xp[1], xp[2], xp[3], xp[4], xp[5]);
- break;
-
- case polyType:
- g.drawPolygon (xp, yp, npoints);
- break;
- }
- }
- }
-
- /**
- * application class proper
- */
- public class DrawApp extends Panel {
-
- /*
- * the maximum number of shapes allowed
- */
- static final int MaxShapes = 25;
-
- /*
- * nshapes - the number of shapes read in
- * nlines - the line number in the input file
- */
- static int nshapes, nlines = 0;
-
- /*
- * array of instances of class shape
- */
- static Shape shapes[] = new Shape[MaxShapes];
-
- /**
- * invoke paint() method for each shape
- * @param g - destination graphics object
- */
- public void paint (Graphics g) {
-
- int i;
- for (i=0; i<nshapes; i+=1)
- shapes[i].paint (g);
- }
-
- /**
- * application entry point
- * @param args - command-line arguments
- */
- public static void main (String args[]) {
-
- String buf;
- FileInputStream fs=null;
- int i, type = 0;
-
- if (args.length != 1) {
- System.out.println ("usage: java DrawApp <file>");
- System.exit (1);
- }
-
- /*
- * Try to open the file specified by args[0]
- */
- try {
- fs = new FileInputStream (args[0]);
- } catch (Exception e) {
- System.out.println (e);
- System.exit (1);
- }
-
- /*
- * Create a DataInputStream BufferedReader
- * associated with FileInputStream fs
- */
- BufferedReader ds = new BufferedReader(new InputStreamReader(fs));
- String token;
- Color color = Color.white;
- int pts[] = new int[2 * Shape.MaxPoints];
-
- /*
- * loop until end of file or error
- * read a line and parse it
- */
- while (true) {
- try {
- buf = ds.readLine (); // read 1 line
- if (buf == null) break;
- } catch (IOException e) {
- System.out.println (e);
- break;
- }
- nlines += 1;
- StringTokenizer st = new StringTokenizer (buf);
- token = st.nextToken ();
- if (token.equals ("white")) {
- color = Color.white;
- token = st.nextToken ();
- } else if (token.equals ("lightgray")) {
- color = Color.lightGray;
- token = st.nextToken ();
- } else if (token.equals ("gray")) {
- color = Color.gray;
- token = st.nextToken ();
- } else if (token.equals ("darkgray")) {
- color = Color.darkGray;
- token = st.nextToken ();
- } else if (token.equals ("black")) {
- color = Color.black;
- token = st.nextToken ();
- } else if (token.equals ("red")) {
- color = Color.red;
- token = st.nextToken ();
- } else if (token.equals ("pink")) {
- color = Color.pink;
- token = st.nextToken ();
- } else if (token.equals ("orange")) {
- color = Color.orange;
- token = st.nextToken ();
- } else if (token.equals ("yellow")) {
- color = Color.yellow;
- token = st.nextToken ();
- } else if (token.equals ("green")) {
- color = Color.green;
- token = st.nextToken ();
- } else if (token.equals ("magenta")) {
- color = Color.magenta;
- token = st.nextToken ();
- } else if (token.equals ("cyan")) {
- color = Color.cyan;
- token = st.nextToken ();
- } else if (token.equals ("blue")) {
- color = Color.blue;
- token = st.nextToken ();
- } else {
- System.out.println ("Unknown color: "+token);
- System.out.println ("line "+nlines);
- System.exit (1);
- }
-
- int npoints = 0;
- if (token.equals ("rect")) {
- npoints = getInt (st, pts, 4);
- type = Shape.rectType;
- } else if (token.equals ("oval")) {
- npoints = getInt (st, pts, 4);
- type = Shape.ovalType;
- } else if (token.equals ("arc")) {
- npoints = getInt (st, pts, 6);
- type = Shape.arcType;
- } else if (token.equals ("poly")) {
- npoints = getInt (st, pts, Shape.MaxPoints);
- type = Shape.polyType;
- } else {
- System.out.println ("Unknown shape: "+token);
- System.out.println ("line "+nlines);
- System.exit (1);
- }
- shapes[nshapes++] = new Shape (type, npoints, pts, color);
- }
- /*
- * close can throw an exception also; catch it for completeness
- */
- try {
- fs.close ();
- } catch (IOException e) {
- System.out.println (e);
- }
-
- Frame f = new Frame ("Drawing shapes");
- DrawApp drawApp = new DrawApp ();
-
- f.setSize(410, 430);
- f.addWindowListener(new WindowCloser());
- f.add ("Center", drawApp);
- f.show ();
- }
-
- /**
- * parse points
- * @param st - StringTokenizer for current line
- * @param pts[] - array of points to be returned
- * @param nmax - maximum number of points to accept
- */
- static int getInt (StringTokenizer st, int pts[], int nmax) {
-
- int i;
- String token;
-
- for (i=0; i<nmax; i+=1) {
- if (st.hasMoreTokens () == false) break;
- token = st.nextToken ();
- try {
- pts[i] = Integer.valueOf (token).intValue ();
- } catch (NumberFormatException e) {
- System.out.println (e);
- System.out.println ("line "+nlines);
- System.exit (1);
- }
- }
- return i;
- }
- }
- class WindowCloser extends WindowAdapter
- {
- public void windowClosing(WindowEvent e)
- {
- Window win = e.getWindow();
- win.setVisible(false);
- win.dispose();
- System.exit(0);
- }//windowClosing
- }//class WindowCloser
-
-
-
-
-
-
-
-
- ChartApp.java:
-
- import java.applet.Applet;
- import java.awt.*;
-
- /**
- * parent class
- */
- class Chart {
-
- /*
- * x and y positions of the upper-left of the chart
- * nvalues - number of values for this chart
- */
- int xpos, ypos, nvalues;
-
- /*
- * width and height of this chart
- */
- int width, height;
-
- /*
- * maximum number of values allowed
- */
- final int MaxValues = 10;
-
- /*
- * data values for this chart
- */
- double values[] = new double[MaxValues];
-
- /*
- * color associated with each value
- */
- Color colors[] = new Color[MaxValues];
-
- /*
- * sum total of values, used for scaling purposes
- */
- double total;
-
- /**
- * class constructor
- * save values and normalizes them so that the max. value is 1.0
- * @param x, y - top-left coordinates
- * @param w, h - width and height
- * @param n - number of points
- * @param val[] - array of values
- * @param c[] - array of colors corresponding to values
- */
- public Chart (int x, int y, int w, int h, int n, double val[], Color c[]) {
-
- int i;
- double extreme;
-
- xpos = x;
- ypos = y;
- width = w;
- height = h;
- nvalues = n;
- if (nvalues > MaxValues) nvalues = MaxValues;
- extreme = 0.0;
- for (i=0; i<nvalues; i+=1) {
- if (Math.abs (val[i]) > extreme)
- extreme = Math.abs (val[i]);
- colors[i] = c[i];
- }
- extreme = 1/extreme;
- total = 0;
- for (i=0; i<nvalues; i+=1) {
- values[i] = extreme * val[i];
- total += values[i];
- }
- }
- }
-
- /**
- * class implements a bar chart
- */
- class BarChart extends Chart {
-
- /**
- * constructor just calls Chart constructor
- * @param x, y - top left coordinates
- * @param w, h - width and height
- * @param n - number of points
- * @param val[] - array of values
- * @param c[] - array of colors corresponding to values
- */
- public BarChart (int x, int y, int w, int h, int n, double val[], Color c[]) {
-
- super (x, y, w, h, n, val, c);
- }
-
- /**
- * need to add a paint method
- * draws the bar chart using fill3DRect
- * @param g - destination graphics object
- */
- void paint (Graphics g) {
-
- int i;
- int barwidth = 3 * width / (4 * nvalues);
- int bardx = width / nvalues;
- int x, y, h;
-
- g.setColor (Color.black);
- g.fillRect (xpos, ypos-height, width, height);
- for (i=0; i<nvalues; i+=1) {
- g.setColor (colors[i]);
- x = xpos + bardx*i;
- h = (int) (values[i] * height);
- y = ypos - h;
- g.fill3DRect (x, y, barwidth, h, true);
- }
- }
- }
-
- /**
- * class implements a pie chart
- */
- class PieChart extends Chart {
-
- /**
- * class constructor just calls Chart constructor
- * @param x, y - top-left coordinates
- * @param w, h - width and height
- * @param n - number of points
- * @param val[] - array of values
- * @param c[] - array of colors corresponding to values
- */
- public PieChart (int x, int y, int w, int h, int n, double val[], Color c[]) {
-
- super (x, y, w, h, n, val, c);
- }
-
- /**
- * need to add a paint method
- * draws the pie chart using fillArc
- * @param g - destination graphics object
- */
- void paint (Graphics g) {
-
- int i, y;
- int startAngle, arcAngle;
-
- startAngle = 0;
- y = ypos - height;
- for (i=0; i<nvalues; i+=1) {
- arcAngle = (int) (360.0 * values[i] / total);
- g.setColor (colors[i]);
- g.fillArc (xpos, y, width, height, startAngle, arcAngle);
- startAngle += arcAngle;
- }
- }
- }
-
- /**
- * the applet/application proper
- */
- public class ChartApp extends Applet {
-
- /*
- * width and height of the bounding panel
- */
- int width, height;
-
- /*
- * instances of BarChart and PieChart
- */
- BarChart bc1;
- PieChart pc1;
-
- /*
- * called when applet is loaded
- * generate random values and plot them
- */
- public void init () {
-
- int i;
- double values[] = new double[5];
- Color colors[] = new Color[5];
-
- width = 410;
- height = 230;
- colors[0] = Color.blue;
- colors[1] = Color.orange;
- colors[2] = Color.yellow;
- colors[3] = Color.green;
- colors[4] = Color.magenta;
-
- for (i=0; i<5; i+=1) values[i] = Math.random () + 0.001;
- int w = (width-40)/2;
- int h = height-20;
- bc1 = new BarChart (10, height-10, w, h, 5, values, colors);
- pc1 = new PieChart (width/2, height-10, w, h, 5, values, colors);
- }
-
- /**
- * invoke the chart paint methods
- * @param g - destination graphics object
- */
- public void paint (Graphics g) {
-
- bc1.paint (g);
- pc1.paint (g);
- }
-
- /**
- * application entry point
- * create a window frame and add the applet inside
- * @param args[] - command-line arguments
- */
- public static void main (String args[]) {
-
- Frame f = new Frame ("Charts");
- ChartApp chart = new ChartApp ();
-
- f.setSize (410, 230);
- f.add ("Center", chart);
- f.show ();
- chart.init ();
- chart.start ();
- }
- }
-
-
-
-
-
-
-
-
- ScrollApp.java:
-
- import java.applet.Applet;
- import java.awt.*;
-
- /**
- * a class that handles scrolling text
- */
- class Scroll {
-
- /*
- * x and y coordinates of starting point
- */
- int xstart, ystart;
-
- /*
- * width and height of bounding panel
- */
- int width, height;
-
- /*
- * text to be scrolled
- */
- String text;
-
- /*
- * x and y velocities, respectively
- */
- int deltaX, deltaY;
-
- /*
- * current x and y position of the text
- */
- int xpos, ypos;
-
- /*
- * the color of the text
- */
- Color color;
-
- /**
- * class constructor just saves arguments
- * @param x, y - starting coordinates
- * @param dx, dy - x and y velocities
- * @param w, h - width and height of bounding panel
- * @param t - the text string
- * @param c - color of the text
- */
- public Scroll (int x, int y, int dx, int dy, int w, int h, String t, Color c) {
-
- xstart = x;
- ystart = y;
- width = w;
- height = h;
- text = t;
- deltaX = dx;
- deltaY = dy;
- color = c;
- xpos = xstart;
- ypos = ystart;
- }
-
- /*
- * draw the text at the current position
- * advance the position and reinitialize outside bounding panel
- * @param g - destination graphics object
- */
- void paint (Graphics g) {
-
- g.setColor (color);
- g.drawString (text, xpos, ypos);
- xpos += deltaX;
- ypos += deltaY;
-
- FontMetrics fm = g.getFontMetrics ();
- int textw = fm.stringWidth (text);
- int texth = fm.getHeight ();
- if (deltaX < 0 && xpos < -textw) xpos = xstart;
- if (deltaX > 0 && xpos > width) xpos = xstart;
- if (deltaY < 0 && ypos < 0) ypos = ystart;
- if (deltaY > 0 && ypos > height+texth) ypos = ystart;
- }
- }
-
- /**
- * the applet/application proper
- */
- public class ScrollApp extends Applet {
-
- /*
- * width and height of the bounding panel
- */
- int width, height;
-
- /*
- * instances of Scroll for demonstration
- */
- Scroll left, right, up, down, diag;
-
- /*
- * called when the applet is loaded
- * create new instances of Scroll
- */
- public void init () {
-
-
- width = 410;
- height = 230;
-
- left = new Scroll (400, 50, -5, 0, width, height,
- "Moving left", Color.red);
- right = new Scroll (0, 150, 5, 0, width, height,
- "Moving right", Color.green);
- up = new Scroll (100, 200, 0, -5, width, height,
- "Moving up", Color.blue);
- down = new Scroll (200, 0, 0, 5, width, height,
- "Moving down", Color.cyan);
- diag = new Scroll (0, 0, 7, 3, width, height,
- "Moving diagonally", Color.magenta);
- }
-
- /*
- * invoke the paint method of each scrolling text instance
- * force a repaint 50ms later
- */
- public void paint (Graphics g) {
-
- left.paint (g);
- right.paint (g);
- up.paint (g);
- down.paint (g);
- diag.paint (g);
- repaint (50);
- }
-
- /*
- * Application entry point
- * @param args - command-line arguments
- */
- public static void main (String args[]) {
-
- Frame f = new Frame ("Scrolling text");
- ScrollApp scrollApp = new ScrollApp ();
-
- f.setSize (410, 230);
- f.add ("Center", scrollApp);
- f.show ();
- scrollApp.init ();
- }
- }
-
-
-
-
-
-
-
-
- Fonts.java:
-
- import java.applet.Applet;
- import java.awt.*;
-
- /**
- * Class that determines which fonts are available
- */
- public class Fonts extends Applet {
-
- /*
- * Maximum number of fonts to display
- */
- final int MaxFonts = 10;
-
- /*
- * Width and height of bounding panel
- */
- int width, height;
-
- /*
- * Array of font names
- */
- String fontName[];
-
- /*
- * Array of fonts
- * Holds plain, italic, and bold for each font
- */
- Font theFonts[] = new Font[3 * MaxFonts];
-
- /*
- * The number of fonts found
- */
- int nfonts = 0;
-
- /*
- * Applet entry point
- */
- public void init () {
-
- int i;
- Dimension d = getSize ();
-
- width = d.width;
- height = d.height;
-
- theFonts[0] = new Font ("Courier", Font.PLAIN, 12);
- theFonts[1] = new Font ("System", Font.BOLD, 16);
- theFonts[2] = new Font ("Helvetica", Font.BOLD, 18);
- }
-
- /*
- * Draw the font names.
- * @param g - destination graphics object
- */
- public void paint (Graphics g) {
-
- int i;
-
-
- g.setFont (theFonts[0]);
- g.drawString ("Courier", 10, 30);
- g.setFont (theFonts[1]);
- g.drawString ("System", 70, 70);
- g.setFont (theFonts[2]);
- g.drawString ("Helvetica", 150, 90);
-
- }
-
- /*
- * Application entry point
- * Creates a window frame and adds the applet inside
- * @param args[] - command-line arguments
- */
- public static void main (String args[]) {
-
- Frame f = new Frame ("Fonts");
- Fonts fonts = new Fonts ();
-
- f.setSize (250, 200);
- f.add ("Center", fonts);
- f.show ();
- fonts.init ();
- }
- }
-
-
-
-
-
-
-
-
- CrazyText.java:
-
- import java.applet.Applet;
- import java.awt.*;
-
- /*
- * application/applet class
- */
- public class CrazyText extends Applet {
-
- String text = "Java"; // string to be displayed
- int delta = 5; // "craziness" factor: max pixel offset
- String fontName = "TimesRoman";
- int fontSize = 36;
-
- char chars[]; // individual chars in 'text'
- int positions[]; // base horizontal position for each char
- FontMetrics fm;
-
- /*
- * called when the applet is loaded
- * creates a font and initializes positions of characters
- */
- public void init() {
-
- int fontStyle = Font.BOLD + Font.ITALIC;
- setFont(new Font(fontName, fontStyle, fontSize));
- fm = getFontMetrics(getFont());
-
- chars = new char[text.length()];
- text.getChars(0, text.length(), chars, 0);
-
- positions = new int[text.length()];
- for (int i = 0; i < text.length(); i++) {
- positions[i] = fm.charsWidth(chars, 0, i) + 20;
- }
- }
-
- /*
- * draws the characters and forces a repaint 100ms later
- * @param g - destination graphics object
- */
- public void paint (Graphics g) {
-
- int x, y;
- g.setColor (new Color((float) Math.random(),
- (float) Math.random(),
- (float) Math.random()));
- for (int i = 0; i < text.length(); i++) {
- x = (int)(Math.random() * delta * 2) + positions[i];
- y = (int)(Math.random() * delta * 2) + fm.getAscent() - 1;
- g.drawChars (chars, i, 1, x, y);
- }
- repaint (100);
- }
-
- /*
- * override default update() method to eliminate
- * erasing of the panel
- */
- public void update (Graphics g) {
- paint (g);
- }
-
- /*
- * application entry point
- * create a window frame and add the applet inside
- * @param args[] - command line arguments
- */
- public static void main (String args[]) {
-
- Frame f = new Frame ("Crazy");
- CrazyText crazy = new CrazyText ();
-
- f.setSize (130, 80);
- f.add ("Center", crazy);
- f.show ();
- crazy.init ();
- }
- }
-
-
-
-
-
-
-
-
-
- Status.java:
-
- 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);
- }
- }
-
-
-
-
-