home *** CD-ROM | disk | FTP | other *** search
/ BUG 15 / BUGCD1998_06.ISO / aplic / jbuilder / jsamples.z / Chart.java < prev    next >
Encoding:
Java Source  |  1997-07-30  |  8.8 KB  |  297 lines

  1. // $Header: z:/admin/metro_examples/java/demo/BarChart/rcs/Chart.java 1.1 1997/02/06 00:29:46 IPGIntel-2 Exp $ 
  2. /*
  3.  * @(#)Chart.java    1.3 96/12/06
  4.  *
  5.  * Copyright (c) 1994-1996 Sun Microsystems, Inc. All Rights Reserved.
  6.  *
  7.  * Sun grants you ("Licensee") a non-exclusive, royalty free, license to use,
  8.  * modify and redistribute this software in source and binary code form,
  9.  * provided that i) this copyright notice and license appear on all copies of
  10.  * the software; and ii) Licensee does not utilize the software in a manner
  11.  * which is disparaging to Sun.
  12.  *
  13.  * This software is provided "AS IS," without a warranty of any kind. ALL
  14.  * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING ANY
  15.  * IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR
  16.  * NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN AND ITS LICENSORS SHALL NOT BE
  17.  * LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING
  18.  * OR DISTRIBUTING THE SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN OR ITS
  19.  * LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT,
  20.  * INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER
  21.  * CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF
  22.  * OR INABILITY TO USE SOFTWARE, EVEN IF SUN HAS BEEN ADVISED OF THE
  23.  * POSSIBILITY OF SUCH DAMAGES.
  24.  *
  25.  * This software is not designed or intended for use in on-line control of
  26.  * aircraft, air traffic, aircraft navigation or aircraft communications; or in
  27.  * the design, construction, operation or maintenance of any nuclear
  28.  * facility. Licensee represents and warrants that it will not use or
  29.  * redistribute the Software for such purposes.
  30.  */
  31.  
  32. import java.awt.Graphics;
  33. import java.awt.Color;
  34. import java.awt.Font;
  35. import java.awt.FontMetrics;
  36. import java.io.*;
  37. import java.lang.*;
  38. import java.net.URL;
  39.  
  40. public class Chart extends java.applet.Applet {
  41.     static final int    VERTICAL = 0;
  42.     static final int     HORIZONTAL = 1;
  43.  
  44.     static final int    SOLID = 0;
  45.     static final int    STRIPED = 1;
  46.  
  47.     int            orientation;
  48.     String        title;
  49.     Font        titleFont;
  50.     FontMetrics        titleFontMetrics;
  51.     int            titleHeight = 15;
  52.     int            columns;
  53.     int            values[];
  54.     Object        colors[];
  55.     Object        labels[];
  56.     int            styles[];
  57.     int            scale = 10;
  58.     int            maxLabelWidth = 0;
  59.     int            barWidth;
  60.     int            barSpacing = 10;
  61.     int            max = 0;
  62.  
  63.     public synchronized void init() {
  64.     String rs;
  65.     
  66.     titleFont = new java.awt.Font("Courier", Font.BOLD, 12);
  67.     titleFontMetrics = getFontMetrics(titleFont);
  68.     title = getParameter("title");
  69.  
  70.     if (title == null) {
  71.         title = "Chart";
  72.     }
  73.     rs = getParameter("columns");
  74.     if (rs == null) {
  75.         columns = 5;
  76.     } else {
  77.         columns = Integer.parseInt(rs);
  78.     }
  79.     rs = getParameter("scale");
  80.     if (rs == null) {
  81.         scale = 10;
  82.     } else {
  83.         scale = Integer.parseInt(rs);
  84.     }
  85.  
  86.     rs = getParameter("orientation");
  87.     if (rs == null) {
  88.         orientation = VERTICAL;
  89.     } else if (rs.toLowerCase().equals("vertical")) {
  90.         orientation = VERTICAL;
  91.     } else if (rs.toLowerCase().equals("horizontal")) {
  92.         orientation = HORIZONTAL;
  93.     } else {
  94.         orientation = VERTICAL;
  95.     }
  96.     values = new int[columns];
  97.     colors = new Color[columns];
  98.     labels = new String[columns];
  99.     styles = new int[columns];
  100.     for (int i=0; i < columns; i++) {
  101.         // parse the value for this column
  102.         rs = getParameter("C" + (i+1));
  103.         if (rs != null) {
  104.         try {
  105.             values[i] = Integer.parseInt(rs);
  106.         } catch (NumberFormatException e) {
  107.             values[i] = 0;
  108.         }
  109.         }
  110.         if (values[i] > max) {
  111.         max = values[i];
  112.         }
  113.  
  114.         // parse the label for this column
  115.         rs = getParameter("C" + (i+1) + "_label");
  116.         labels[i] = (rs == null) ? "" : rs;
  117.         maxLabelWidth = Math.max(titleFontMetrics.stringWidth((String)(labels[i])),
  118.                      maxLabelWidth);
  119.  
  120.         // parse the bar style
  121.         rs = getParameter("C" + (i+1) + "_style");
  122.         if (rs == null || rs.toLowerCase().equals("solid")) {
  123.         styles[i] = SOLID;
  124.         } else if (rs.toLowerCase().equals("striped")) {
  125.         styles[i] = STRIPED;
  126.         } else {
  127.         styles[i] = SOLID;
  128.         }
  129.         // parse the color attribute for this column
  130.         rs = getParameter("C" + (i+1) + "_color");
  131.         if (rs != null) {
  132.         if (rs.equals("red")) {
  133.             colors[i] = Color.red;
  134.         } else if (rs.equals("green")) {
  135.             colors[i] = Color.green;
  136.         } else if (rs.equals("blue")) {
  137.             colors[i] = Color.blue;
  138.         } else if (rs.equals("pink")) {
  139.             colors[i] = Color.pink;
  140.         } else if (rs.equals("orange")) {
  141.             colors[i] = Color.orange;
  142.         } else if (rs.equals("magenta")) {
  143.             colors[i] = Color.magenta;
  144.         } else if (rs.equals("cyan")) {
  145.             colors[i] = Color.cyan;
  146.         } else if (rs.equals("white")) {
  147.             colors[i] = Color.white;
  148.         } else if (rs.equals("yellow")) {
  149.             colors[i] = Color.yellow;
  150.         } else if (rs.equals("gray")) {
  151.             colors[i] = Color.gray;
  152.         } else if (rs.equals("darkGray")) {
  153.             colors[i] = Color.darkGray;
  154.         } else {
  155.             colors[i] = Color.gray;
  156.         }
  157.         } else {
  158.         colors[i] = Color.gray;
  159.         }
  160.     }
  161.     switch (orientation) {
  162.       case VERTICAL:
  163.       default:
  164.         barWidth = maxLabelWidth;
  165.         resize(Math.max(columns * (barWidth + barSpacing),
  166.                 titleFontMetrics.stringWidth(title)) +
  167.            titleFont.getSize() + 5,
  168.            (max * scale) + (2 * titleFont.getSize()) + 5 + titleFont.getSize());
  169.         break;
  170.       case HORIZONTAL:
  171.         barWidth = titleFont.getSize();
  172.         resize(Math.max((max * scale) + titleFontMetrics.stringWidth("" + max),
  173.                 titleFontMetrics.stringWidth(title)) + maxLabelWidth + 5,
  174.            (columns * (barWidth + barSpacing)) + titleFont.getSize() + 10);
  175.         break;
  176.     }
  177.     }
  178.  
  179.     public synchronized void paint(Graphics g) {
  180.     int i, j;
  181.     int cx, cy;
  182.     char l[] = new char[1];
  183.  
  184.  
  185.     // draw the title centered at the bottom of the bar graph
  186.     g.setColor(Color.black);
  187.     i = titleFontMetrics.stringWidth(title);
  188.     g.setFont(titleFont);
  189.     g.drawString(title, Math.max((size().width - i)/2, 0),
  190.              size().height - titleFontMetrics.getDescent()); 
  191.     for (i=0; i < columns; i++) {
  192.         switch (orientation) {
  193.           case VERTICAL:
  194.           default:
  195.         // set the next X coordinate to account for the label
  196.         // being wider than the bar size().width.
  197.         cx = (Math.max((barWidth + barSpacing),maxLabelWidth) * i) +
  198.             barSpacing;
  199.  
  200.         // center the bar chart
  201.         cx += Math.max((size().width - (columns *
  202.                      (barWidth + (2 * barSpacing))))/2,0);
  203.         
  204.         // set the next Y coordinate to account for the size().height
  205.         // of the bar as well as the title and labels painted
  206.         // at the bottom of the chart.
  207.         cy = size().height - (values[i] * scale) - 1 - (2 * titleFont.getSize());
  208.  
  209.         // draw the label
  210.         g.setColor(Color.black);        
  211.         g.drawString((String)labels[i], cx,
  212.                  size().height - titleFont.getSize() - titleFontMetrics.getDescent());
  213.  
  214.         // draw the shadow bar
  215.         if (colors[i] == Color.black) {
  216.             g.setColor(Color.gray);
  217.         }
  218.         g.fillRect(cx + 5, cy - 3, barWidth,  (values[i] * scale));
  219.         // draw the bar with the specified color
  220.         g.setColor((Color)(colors[i]));
  221.         switch (styles[i]) {
  222.           case SOLID:
  223.           default:
  224.             g.fillRect(cx, cy, barWidth, (values[i] * scale));
  225.             break;
  226.           case STRIPED:
  227.             {
  228.             int steps = (values[i] * scale) / 2;
  229.             int ys;
  230.  
  231.             for (j=0; j < steps; j++) {
  232.                 ys = cy + (2 * j);
  233.                 g.drawLine(cx, ys, cx + barWidth, ys);
  234.             }
  235.             }
  236.             break;
  237.         }
  238.         g.drawString("" + values[i],
  239.                  cx,
  240.                  cy - titleFontMetrics.getDescent());
  241.         break;
  242.           case HORIZONTAL:
  243.         // set the Y coordinate
  244.         cy = ((barWidth + barSpacing) * i) + barSpacing;
  245.                    
  246.         // set the X coordinate to be the size().width of the widest
  247.         // label 
  248.         cx = maxLabelWidth + 1;
  249.  
  250.         cx += Math.max((size().width - (maxLabelWidth + 1 +
  251.                      titleFontMetrics.stringWidth("" +
  252.                                    max) +
  253.                      (max * scale))) / 2, 0);
  254.         // draw the labels and the shadow
  255.         g.setColor(Color.black);        
  256.         g.drawString((String)labels[i], cx - maxLabelWidth - 1,
  257.                  cy + titleFontMetrics.getAscent());
  258.         if (colors[i] == Color.black) {
  259.             g.setColor(Color.gray);
  260.         }
  261.         g.fillRect(cx + 3,
  262.                cy + 5,
  263.                (values[i] * scale),
  264.                barWidth);
  265.  
  266.         // draw the bar in the current color
  267.         g.setColor((Color)(colors[i]));
  268.         switch (styles[i]) {
  269.           case SOLID:
  270.           default:
  271.             g.fillRect(cx,
  272.                    cy,
  273.                    (values[i] * scale),
  274.                    barWidth);
  275.             break;
  276.           case STRIPED:
  277.             {
  278.             int steps = (values[i] * scale) / 2;
  279.             int ys;
  280.  
  281.             for (j=0; j < steps; j++) {
  282.                 ys = cx + (2 * j);
  283.                 g.drawLine(ys, cy, ys, cy + barWidth);
  284.             }
  285.             }
  286.             break;
  287.         }
  288.         g.drawString("" + values[i],
  289.                  cx + (values[i] * scale) + 3,
  290.                  cy + titleFontMetrics.getAscent());
  291.                  
  292.         break;
  293.         }
  294.     }
  295.     }
  296. }
  297.