home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 1996 December / PCWKCD1296.iso / vjplusb / msdev / samples / sun / barchart / chart.java < prev    next >
Text File  |  1996-07-10  |  9KB  |  295 lines

  1. /*
  2.  * @(#)Chart.java    1.6f 95/03/27 Sami Shaio
  3.  *
  4.  * Copyright (c) 1994-1995 Sun Microsystems, Inc. All Rights Reserved.
  5.  *
  6.  * Permission to use, copy, modify, and distribute this software
  7.  * and its documentation for NON-COMMERCIAL or COMMERCIAL purposes and
  8.  * without fee is hereby granted. 
  9.  * Please refer to the file http://java.sun.com/copy_trademarks.html
  10.  * for further important copyright and trademark information and to
  11.  * http://java.sun.com/licensing.html for further important licensing
  12.  * information for the Java (tm) Technology.
  13.  * 
  14.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
  15.  * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
  16.  * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  17.  * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
  18.  * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
  19.  * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
  20.  * 
  21.  * THIS SOFTWARE IS NOT DESIGNED OR INTENDED FOR USE OR RESALE AS ON-LINE
  22.  * CONTROL EQUIPMENT IN HAZARDOUS ENVIRONMENTS REQUIRING FAIL-SAFE
  23.  * PERFORMANCE, SUCH AS IN THE OPERATION OF NUCLEAR FACILITIES, AIRCRAFT
  24.  * NAVIGATION OR COMMUNICATION SYSTEMS, AIR TRAFFIC CONTROL, DIRECT LIFE
  25.  * SUPPORT MACHINES, OR WEAPONS SYSTEMS, IN WHICH THE FAILURE OF THE
  26.  * SOFTWARE COULD LEAD DIRECTLY TO DEATH, PERSONAL INJURY, OR SEVERE
  27.  * PHYSICAL OR ENVIRONMENTAL DAMAGE ("HIGH RISK ACTIVITIES").  SUN
  28.  * SPECIFICALLY DISCLAIMS ANY EXPRESS OR IMPLIED WARRANTY OF FITNESS FOR
  29.  * HIGH RISK ACTIVITIES.
  30.  */
  31. import java.awt.Graphics;
  32. import java.awt.Color;
  33. import java.awt.Font;
  34. import java.awt.FontMetrics;
  35. import java.io.*;
  36. import java.lang.*;
  37.  
  38. public class chart extends java.applet.Applet {
  39.     static final int    VERTICAL = 0;
  40.     static final int     HORIZONTAL = 1;
  41.  
  42.     static final int    SOLID = 0;
  43.     static final int    STRIPED = 1;
  44.  
  45.     int            orientation;
  46.     String        title;
  47.     Font        titleFont;
  48.     FontMetrics        titleFontMetrics;
  49.     int            titleHeight = 15;
  50.     int            columns;
  51.     int            values[];
  52.     Object        colors[];
  53.     Object        labels[];
  54.     int            styles[];
  55.     int            scale = 10;
  56.     int            maxLabelWidth = 0;
  57.     int            barWidth;
  58.     int            barSpacing = 10;
  59.     int            max = 0;
  60.  
  61.     public synchronized void init() {
  62.     String rs;
  63.     
  64.     titleFont = new java.awt.Font("Courier", Font.BOLD, 12);
  65.     titleFontMetrics = getFontMetrics(titleFont);
  66.     title = getParameter("title");
  67.  
  68.     if (title == null) {
  69.         title = "Chart";
  70.     }
  71.     rs = getParameter("columns");
  72.     if (rs == null) {
  73.         columns = 5;
  74.     } else {
  75.         columns = Integer.parseInt(rs);
  76.     }
  77.     rs = getParameter("scale");
  78.     if (rs == null) {
  79.         scale = 10;
  80.     } else {
  81.         scale = Integer.parseInt(rs);
  82.     }
  83.  
  84.     rs = getParameter("orientation");
  85.     if (rs == null) {
  86.         orientation = VERTICAL;
  87.     } else if (rs.toLowerCase().equals("vertical")) {
  88.         orientation = VERTICAL;
  89.     } else if (rs.toLowerCase().equals("horizontal")) {
  90.         orientation = HORIZONTAL;
  91.     } else {
  92.         orientation = VERTICAL;
  93.     }
  94.     values = new int[columns];
  95.     colors = new Color[columns];
  96.     labels = new String[columns];
  97.     styles = new int[columns];
  98.     for (int i=0; i < columns; i++) {
  99.         // parse the value for this column
  100.         rs = getParameter("C" + (i+1));
  101.         if (rs != null) {
  102.         try {
  103.             values[i] = Integer.parseInt(rs);
  104.         } catch (NumberFormatException e) {
  105.             values[i] = 0;
  106.         }
  107.         }
  108.         if (values[i] > max) {
  109.         max = values[i];
  110.         }
  111.  
  112.         // parse the label for this column
  113.         rs = getParameter("C" + (i+1) + "_label");
  114.         labels[i] = (rs == null) ? "" : rs;
  115.         maxLabelWidth = Math.max(titleFontMetrics.stringWidth((String)(labels[i])),
  116.                      maxLabelWidth);
  117.  
  118.         // parse the bar style
  119.         rs = getParameter("C" + (i+1) + "_style");
  120.         if (rs == null || rs.toLowerCase().equals("solid")) {
  121.         styles[i] = SOLID;
  122.         } else if (rs.toLowerCase().equals("striped")) {
  123.         styles[i] = STRIPED;
  124.         } else {
  125.         styles[i] = SOLID;
  126.         }
  127.         // parse the color attribute for this column
  128.         rs = getParameter("C" + (i+1) + "_color");
  129.         if (rs != null) {
  130.         if (rs.equals("red")) {
  131.             colors[i] = Color.red;
  132.         } else if (rs.equals("green")) {
  133.             colors[i] = Color.green;
  134.         } else if (rs.equals("blue")) {
  135.             colors[i] = Color.blue;
  136.         } else if (rs.equals("pink")) {
  137.             colors[i] = Color.pink;
  138.         } else if (rs.equals("orange")) {
  139.             colors[i] = Color.orange;
  140.         } else if (rs.equals("magenta")) {
  141.             colors[i] = Color.magenta;
  142.         } else if (rs.equals("cyan")) {
  143.             colors[i] = Color.cyan;
  144.         } else if (rs.equals("white")) {
  145.             colors[i] = Color.white;
  146.         } else if (rs.equals("yellow")) {
  147.             colors[i] = Color.yellow;
  148.         } else if (rs.equals("gray")) {
  149.             colors[i] = Color.gray;
  150.         } else if (rs.equals("darkGray")) {
  151.             colors[i] = Color.darkGray;
  152.         } else {
  153.             colors[i] = Color.gray;
  154.         }
  155.         } else {
  156.         colors[i] = Color.gray;
  157.         }
  158.     }
  159.     switch (orientation) {
  160.       case VERTICAL:
  161.       default:
  162.         barWidth = maxLabelWidth;
  163.         resize(Math.max(columns * (barWidth + barSpacing),
  164.                 titleFontMetrics.stringWidth(title)) +
  165.            titleFont.getSize() + 5,
  166.            (max * scale) + (2 * titleFont.getSize()) + 5 + titleFont.getSize());
  167.         break;
  168.       case HORIZONTAL:
  169.         barWidth = titleFont.getSize();
  170.         resize(Math.max((max * scale) + titleFontMetrics.stringWidth("" + max),
  171.                 titleFontMetrics.stringWidth(title)) + maxLabelWidth + 5,
  172.            (columns * (barWidth + barSpacing)) + titleFont.getSize() + 10);
  173.         break;
  174.     }
  175.     }
  176.  
  177.     public synchronized void paint(Graphics g) {
  178.     int i, j;
  179.     int cx, cy;
  180.     char l[] = new char[1];
  181.  
  182.  
  183.     // draw the title centered at the bottom of the bar graph
  184.     g.setColor(Color.black);
  185.     i = titleFontMetrics.stringWidth(title);
  186.     g.setFont(titleFont);
  187.     g.drawString(title, Math.max((size().width - i)/2, 0),
  188.              size().height - titleFontMetrics.getDescent()); 
  189.     for (i=0; i < columns; i++) {
  190.         switch (orientation) {
  191.           case VERTICAL:
  192.           default:
  193.         // set the next X coordinate to account for the label
  194.         // being wider than the bar size().width.
  195.         cx = (Math.max((barWidth + barSpacing),maxLabelWidth) * i) +
  196.             barSpacing;
  197.  
  198.         // center the bar chart
  199.         cx += Math.max((size().width - (columns *
  200.                      (barWidth + (2 * barSpacing))))/2,0);
  201.         
  202.         // set the next Y coordinate to account for the size().height
  203.         // of the bar as well as the title and labels painted
  204.         // at the bottom of the chart.
  205.         cy = size().height - (values[i] * scale) - 1 - (2 * titleFont.getSize());
  206.  
  207.         // draw the label
  208.         g.setColor(Color.black);        
  209.         g.drawString((String)labels[i], cx,
  210.                  size().height - titleFont.getSize() - titleFontMetrics.getDescent());
  211.  
  212.         // draw the shadow bar
  213.         if (colors[i] == Color.black) {
  214.             g.setColor(Color.gray);
  215.         }
  216.         g.fillRect(cx + 5, cy - 3, barWidth,  (values[i] * scale));
  217.         // draw the bar with the specified color
  218.         g.setColor((Color)(colors[i]));
  219.         switch (styles[i]) {
  220.           case SOLID:
  221.           default:
  222.             g.fillRect(cx, cy, barWidth, (values[i] * scale));
  223.             break;
  224.           case STRIPED:
  225.             {
  226.             int steps = (values[i] * scale) / 2;
  227.             int ys;
  228.  
  229.             for (j=0; j < steps; j++) {
  230.                 ys = cy + (2 * j);
  231.                 g.drawLine(cx, ys, cx + barWidth, ys);
  232.             }
  233.             }
  234.             break;
  235.         }
  236.         g.drawString("" + values[i],
  237.                  cx,
  238.                  cy - titleFontMetrics.getDescent());
  239.         break;
  240.           case HORIZONTAL:
  241.         // set the Y coordinate
  242.         cy = ((barWidth + barSpacing) * i) + barSpacing;
  243.                    
  244.         // set the X coordinate to be the size().width of the widest
  245.         // label 
  246.         cx = maxLabelWidth + 1;
  247.  
  248.         cx += Math.max((size().width - (maxLabelWidth + 1 +
  249.                      titleFontMetrics.stringWidth("" +
  250.                                    max) +
  251.                      (max * scale))) / 2, 0);
  252.         // draw the labels and the shadow
  253.         g.setColor(Color.black);        
  254.         g.drawString((String)labels[i], cx - maxLabelWidth - 1,
  255.                  cy + titleFontMetrics.getAscent());
  256.         if (colors[i] == Color.black) {
  257.             g.setColor(Color.gray);
  258.         }
  259.         g.fillRect(cx + 3,
  260.                cy + 5,
  261.                (values[i] * scale),
  262.                barWidth);
  263.  
  264.         // draw the bar in the current color
  265.         g.setColor((Color)(colors[i]));
  266.         switch (styles[i]) {
  267.           case SOLID:
  268.           default:
  269.             g.fillRect(cx,
  270.                    cy,
  271.                    (values[i] * scale),
  272.                    barWidth);
  273.             break;
  274.           case STRIPED:
  275.             {
  276.             int steps = (values[i] * scale) / 2;
  277.             int ys;
  278.  
  279.             for (j=0; j < steps; j++) {
  280.                 ys = cx + (2 * j);
  281.                 g.drawLine(ys, cy, ys, cy + barWidth);
  282.             }
  283.             }
  284.             break;
  285.         }
  286.         g.drawString("" + values[i],
  287.                  cx + (values[i] * scale) + 3,
  288.                  cy + titleFontMetrics.getAscent());
  289.                  
  290.         break;
  291.         }
  292.     }
  293.     }
  294. }
  295.