home *** CD-ROM | disk | FTP | other *** search
Java Source | 1998-12-14 | 4.9 KB | 223 lines |
- 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 ();
- }
- }