home *** CD-ROM | disk | FTP | other *** search
/ Java 1.2 How-To / JavaHowTo.iso / javafile / ch03 / ChartApp.java next >
Encoding:
Java Source  |  1998-12-14  |  4.9 KB  |  223 lines

  1. import java.applet.Applet;
  2. import java.awt.*;
  3.  
  4. /**
  5.  * parent class
  6.  */
  7. class Chart {
  8.  
  9. /*
  10.  * x and y positions of the upper-left of the chart
  11.  * nvalues - number of values for this chart
  12.  */
  13. int xpos, ypos, nvalues;
  14.  
  15. /*
  16.  * width and height of this chart
  17.  */
  18. int width, height;
  19.  
  20. /*
  21.  * maximum number of values allowed
  22.  */
  23. final int MaxValues = 10;
  24.  
  25. /*
  26.  * data values for this chart
  27.  */
  28. double values[] = new double[MaxValues];
  29.  
  30. /*
  31.  * color associated with each value
  32.  */
  33. Color colors[] = new Color[MaxValues];
  34.  
  35. /*
  36.  * sum total of values, used for scaling purposes
  37.  */
  38. double total;
  39.  
  40. /**
  41.  * class constructor
  42.  * save values and normalizes them so that the max. value is 1.0
  43.  * @param x, y - top-left coordinates
  44.  * @param w, h - width and height
  45.  * @param n - number of points
  46.  * @param val[] - array of values
  47.  * @param c[] - array of colors corresponding to values
  48.  */
  49. public Chart (int x, int y, int w, int h, int n, double val[], Color c[]) {
  50.  
  51.       int i;
  52.       double extreme; 
  53.       
  54.       xpos = x;
  55.       ypos = y;
  56.       width = w;
  57.       height = h;
  58.       nvalues = n;
  59.       if (nvalues > MaxValues) nvalues = MaxValues;
  60.       extreme = 0.0;
  61.       for (i=0; i<nvalues; i+=1) {
  62.             if (Math.abs (val[i]) > extreme)
  63.                   extreme = Math.abs (val[i]);
  64.             colors[i] = c[i]; 
  65.       }
  66.       extreme = 1/extreme;
  67.       total = 0;
  68.       for (i=0; i<nvalues; i+=1) {
  69.             values[i] = extreme * val[i];
  70.             total += values[i];
  71.       }
  72. }
  73. }
  74.  
  75. /**
  76.  * class implements a bar chart
  77.  */
  78. class BarChart extends Chart {
  79.  
  80. /**
  81.  * constructor just calls Chart constructor
  82.  * @param x, y - top left coordinates
  83.  * @param w, h - width and height
  84.  * @param n - number of points
  85.  * @param val[] - array of values
  86.  * @param c[] - array of colors corresponding to values
  87.  */
  88. public BarChart (int x, int y, int w, int h, int n, double val[], Color c[]) {
  89.  
  90.       super (x, y, w, h, n, val, c);
  91. }
  92.  
  93. /**
  94.  * need to add a paint method
  95.  * draws the bar chart using fill3DRect
  96.  * @param g - destination graphics object
  97.  */
  98. void paint (Graphics g) {
  99.  
  100.       int i;
  101.       int barwidth = 3 * width / (4 * nvalues);
  102.       int bardx = width / nvalues;
  103.       int x, y, h;
  104.  
  105.       g.setColor (Color.black);
  106.       g.fillRect (xpos, ypos-height, width, height);
  107.       for (i=0; i<nvalues; i+=1) {
  108.             g.setColor (colors[i]);
  109.             x = xpos + bardx*i;
  110.             h = (int) (values[i] * height);
  111.             y = ypos - h;
  112.             g.fill3DRect (x, y, barwidth, h, true);
  113.       }
  114. }
  115. }
  116.  
  117. /**
  118.  * class implements a pie chart
  119.  */
  120. class PieChart extends Chart {
  121.  
  122. /**
  123.  * class constructor just calls Chart constructor
  124.  * @param x, y - top-left coordinates
  125.  * @param w, h - width and height
  126.  * @param n - number of points
  127.  * @param val[] - array of values
  128.  * @param c[] - array of colors corresponding to values
  129.  */
  130. public PieChart (int x, int y, int w, int h, int n, double val[], Color c[]) {
  131.  
  132.       super (x, y, w, h, n, val, c); 
  133. }
  134.  
  135. /**
  136.  * need to add a paint method
  137.  * draws the pie chart using fillArc
  138.  * @param g - destination graphics object
  139.  */
  140. void paint (Graphics g) {
  141.  
  142.       int i, y;
  143.       int startAngle, arcAngle;
  144.       
  145.       startAngle = 0;
  146.       y = ypos - height;
  147.       for (i=0; i<nvalues; i+=1) {
  148.             arcAngle = (int) (360.0 * values[i] / total);
  149.             g.setColor (colors[i]);
  150.             g.fillArc (xpos, y, width, height, startAngle, arcAngle); 
  151. startAngle += arcAngle;
  152.       }
  153. }
  154. }
  155.  
  156. /**
  157.  * the applet/application  proper
  158.  */
  159. public class ChartApp extends Applet {
  160.  
  161. /*
  162.  * width and height of the bounding panel
  163.  */
  164. int width, height;
  165.  
  166. /*
  167.  * instances of BarChart and PieChart
  168.  */
  169. BarChart bc1;
  170. PieChart pc1;
  171.  
  172. /*
  173.  * called when applet is loaded
  174.  * generate random values and plot them
  175.  */
  176. public void init () {
  177.  
  178.       int i;
  179.       double values[] = new double[5];
  180.       Color colors[] = new Color[5];
  181.       
  182.       width = 410; 
  183.       height = 230;
  184.       colors[0] = Color.blue;
  185.       colors[1] = Color.orange;
  186.       colors[2] = Color.yellow;
  187.       colors[3] = Color.green;
  188.       colors[4] = Color.magenta;
  189.  
  190.       for (i=0; i<5; i+=1) values[i] = Math.random () + 0.001;
  191.       int w = (width-40)/2;
  192.       int h = height-20;
  193.       bc1 = new BarChart (10, height-10, w, h, 5, values, colors);
  194. pc1 = new PieChart (width/2, height-10, w, h, 5, values, colors); 
  195. }
  196.  
  197. /**
  198.  * invoke the chart paint methods
  199.  * @param g - destination graphics object
  200.  */
  201. public void paint (Graphics g) {
  202.  
  203.       bc1.paint (g);
  204.       pc1.paint (g);
  205. }
  206.  
  207. /**
  208.  * application entry point
  209.  * create a window frame and add the applet inside
  210.  * @param args[] - command-line arguments
  211.  */
  212. public static void main (String args[]) {
  213.  
  214.       Frame f = new Frame ("Charts");
  215.       ChartApp chart = new ChartApp ();
  216.       
  217.       f.setSize (410, 230);
  218.       f.add ("Center", chart);
  219.       f.show ();
  220.       chart.init ();
  221.       chart.start ();
  222. }
  223. }