home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / JBuilder8.iso / Solaris / resource / jre / demo / plugin / applets / BarChart / BarChart.java < prev    next >
Encoding:
Java Source  |  2002-09-06  |  11.0 KB  |  320 lines

  1. /*
  2.  * Copyright (c) 2002 Sun Microsystems, Inc. All  Rights Reserved.
  3.  * 
  4.  * Redistribution and use in source and binary forms, with or without
  5.  * modification, are permitted provided that the following conditions
  6.  * are met:
  7.  * 
  8.  * -Redistributions of source code must retain the above copyright
  9.  *  notice, this list of conditions and the following disclaimer.
  10.  * 
  11.  * -Redistribution in binary form must reproduct the above copyright
  12.  *  notice, this list of conditions and the following disclaimer in
  13.  *  the documentation and/or other materials provided with the distribution.
  14.  * 
  15.  * Neither the name of Sun Microsystems, Inc. or the names of contributors
  16.  * may be used to endorse or promote products derived from this software
  17.  * without specific prior written permission.
  18.  * 
  19.  * This software is provided "AS IS," without a warranty of any kind. ALL
  20.  * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING
  21.  * ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
  22.  * OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN AND ITS LICENSORS SHALL NOT
  23.  * BE LIABLE FOR ANY DAMAGES OR LIABILITIES SUFFERED BY LICENSEE AS A RESULT
  24.  * OF OR RELATING TO USE, MODIFICATION OR DISTRIBUTION OF THE SOFTWARE OR ITS
  25.  * DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR ANY LOST
  26.  * REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL,
  27.  * INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY
  28.  * OF LIABILITY, ARISING OUT OF THE USE OF OR INABILITY TO USE SOFTWARE, EVEN
  29.  * IF SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
  30.  * 
  31.  * You acknowledge that Software is not designed, licensed or intended for
  32.  * use in the design, construction, operation or maintenance of any nuclear
  33.  * facility.
  34.  */
  35.  
  36. /*
  37.  * @(#)BarChart.java    1.12 02/06/13
  38.  */
  39.  
  40. import java.awt.*;
  41.  
  42. /**
  43.  * A simple bar chart demo
  44.  * @version     1.6
  45.  * @author Sami Shaio
  46.  * @modified 06/21/00 Daniel Peek : refactored, comments
  47.  */
  48. public class BarChart extends java.applet.Applet {
  49.     private static final int VERTICAL = 0;
  50.     private static final int HORIZONTAL = 1;
  51.  
  52.     private static final int SOLID = 0;
  53.     private static final int STRIPED = 1;
  54.  
  55.     private int orientation;
  56.     private String title;
  57.     private Font font;
  58.     private FontMetrics metrics;
  59.     private int fontHeight = 15;
  60.     private int columns;
  61.     private int values[];
  62.     private Color colors[];
  63.     private String labels[];
  64.     private int styles[];
  65.     private int scale = 10;
  66.     private int maxLabelWidth = 0;
  67.     private int barSpacing = 10;
  68.     private int maxValue = 0;
  69.  
  70.     public void init() {
  71.   
  72.         getSettings();
  73.         
  74.         values = new int[columns];
  75.         labels = new String[columns];
  76.         styles = new int[columns];
  77.         colors = new Color[columns];
  78.  
  79.         for (int i=0; i < columns; i++) {
  80.             parseValue(i);
  81.             parseLabel(i);
  82.             parseStyle(i);
  83.             parseColor(i);
  84.         }
  85.     }
  86.             
  87.     private void getSettings() {
  88.         font = new java.awt.Font("Monospaced", Font.BOLD, 12);
  89.         metrics = getFontMetrics(font);
  90.  
  91.         title = getParameter("title");
  92.         if (title == null) {
  93.             title = "Chart";
  94.         }
  95.         
  96.         String temp = getParameter("columns");
  97.         if (temp == null) {
  98.             columns = 5;
  99.         } else {
  100.             columns = Integer.parseInt(temp);
  101.         }
  102.         
  103.         temp = getParameter("scale");
  104.         if (temp == null) {
  105.             scale = 10;
  106.         } else {
  107.             scale = Integer.parseInt(temp);
  108.         }
  109.  
  110.         temp = getParameter("orientation");
  111.         if (temp == null) {
  112.             orientation = VERTICAL;
  113.         } else if (temp.equalsIgnoreCase("horizontal")) {
  114.             orientation = HORIZONTAL;
  115.         } else {
  116.             orientation = VERTICAL;
  117.         }
  118.     }
  119.  
  120.     private void parseValue(int i) {
  121.         String temp = getParameter("C" + (i+1));
  122.         try {
  123.             values[i] = Integer.parseInt(temp);
  124.         } catch (NumberFormatException e) {
  125.             values[i] = 0;
  126.         } catch (NullPointerException e) {
  127.             values[i] = 0;
  128.         }
  129.         maxValue = Math.max(maxValue, values[i]);
  130.     }
  131.     
  132.     private void parseLabel(int i) {
  133.         String temp = getParameter("C" + (i+1) + "_label");
  134.         if (temp==null) {
  135.             labels[i] = "";
  136.         } else {
  137.             labels[i] = temp;
  138.         }
  139.         maxLabelWidth = Math.max(metrics.stringWidth
  140.                                  ((String) (labels[i])), maxLabelWidth);
  141.     }
  142.  
  143.     private void parseStyle(int i) {
  144.         String temp = getParameter("C" + (i+1) + "_style");
  145.         if (temp == null || temp.equalsIgnoreCase("solid")) {
  146.             styles[i] = SOLID;
  147.         } else if (temp.equalsIgnoreCase("striped")) {
  148.             styles[i] = STRIPED;
  149.         } else {
  150.             styles[i] = SOLID;
  151.         }
  152.     }
  153.     
  154.     private void parseColor(int i) {
  155.         String temp = getParameter("C" + (i+1) + "_color");
  156.         if (temp != null) {
  157.             temp = temp.toLowerCase();
  158.             if (temp.equals("red")) {
  159.                 colors[i] = Color.red;
  160.             } else if (temp.equals("green")) {
  161.                 colors[i] = Color.green;
  162.             } else if (temp.equals("blue")) {
  163.                 colors[i] = Color.blue;
  164.             } else if (temp.equals("pink")) {
  165.                 colors[i] = Color.pink;
  166.             } else if (temp.equals("orange")) {
  167.                 colors[i] = Color.orange;
  168.             } else if (temp.equals("magenta")) {
  169.                 colors[i] = Color.magenta;
  170.             } else if (temp.equals("cyan")) {
  171.                 colors[i] = Color.cyan;
  172.             } else if (temp.equals("white")) {
  173.                 colors[i] = Color.white;
  174.             } else if (temp.equals("yellow")) {
  175.                 colors[i] = Color.yellow;
  176.             } else if (temp.equals("gray")) {
  177.                 colors[i] = Color.gray;
  178.             } else if (temp.equals("darkGray")) {
  179.                 colors[i] = Color.darkGray;
  180.             } else {
  181.                 colors[i] = Color.gray;
  182.             }
  183.         } else {
  184.             colors[i] = Color.gray;
  185.         }
  186.     }
  187.  
  188.     public void paint(Graphics g) {
  189.         // draw the title centered at the bottom of the bar graph
  190.         g.setColor(Color.black);
  191.         g.setFont(font);
  192.         
  193.         g.drawRect(0, 0, getSize().width - 1, getSize().height - 1);
  194.  
  195.         int titleWidth = metrics.stringWidth(title);
  196.         int cx = Math.max((getSize().width - titleWidth) / 2, 0);
  197.         int cy = getSize().height - metrics.getDescent();
  198.         g.drawString(title, cx, cy);
  199.  
  200.         // draw the bars and their titles
  201.         if(orientation == HORIZONTAL) {
  202.             paintHorizontal(g);
  203.         } else {  // VERTICAL
  204.             paintVertical(g);
  205.         }
  206.     }
  207.         
  208.     private void paintHorizontal(Graphics g) {
  209.         // x and y coordinates to draw/write to 
  210.         int cx, cy;
  211.         int barHeight = metrics.getHeight();
  212.  
  213.         for (int i = 0; i < columns; i++) {
  214.             
  215.             // set the X coordinate for this bar and label and center it
  216.             int widthOfItems = maxLabelWidth + 3 + (maxValue * scale) + 5  
  217.                 + metrics.stringWidth(Integer.toString(maxValue));
  218.             cx = Math.max((getSize().width - widthOfItems) / 2, 0);
  219.             
  220.             // set the Y coordinate for this bar and label
  221.             cy = getSize().height - metrics.getDescent() - metrics.getHeight()
  222.                 - barSpacing - ((columns - i - 1) * (barSpacing + barHeight));
  223.  
  224.             // draw the label
  225.             g.setColor(Color.black);
  226.             g.drawString(labels[i], cx, cy);
  227.             cx += maxLabelWidth + 3;
  228.  
  229.         
  230.             // draw the shadow
  231.             g.fillRect(cx + 4, cy - barHeight + 4,
  232.                        (values[i] * scale), barHeight);
  233.  
  234.             // draw the bar
  235.             g.setColor(colors[i]);
  236.             if (styles[i] == STRIPED) {
  237.                 for (int k = 0; k <= values[i] * scale; k += 2) {
  238.                     g.drawLine(cx + k, cy - barHeight, cx + k, cy);
  239.                 }
  240.             } else {      // SOLID
  241.                 g.fillRect(cx, cy - barHeight, 
  242.                            (values[i] * scale) + 1, barHeight + 1);
  243.             }
  244.             cx += (values[i] * scale) + 4;
  245.             
  246.             // draw the value at the end of the bar 
  247.             g.setColor(g.getColor().darker());
  248.             g.drawString(Integer.toString(values[i]), cx, cy);
  249.         } 
  250.     }
  251.     
  252.     private void paintVertical(Graphics g) {
  253.         int barWidth = maxLabelWidth;
  254.  
  255.         for (int i = 0; i < columns; i++) {
  256.  
  257.             // X coordinate for this label and bar (centered)
  258.             int widthOfItems = (barWidth + barSpacing) * columns - barSpacing;
  259.             int cx = Math.max((getSize().width - widthOfItems) / 2, 0);
  260.             cx += (maxLabelWidth + barSpacing) * i;
  261.             
  262.             // Y coordinate for this label and bar
  263.             int cy = getSize().height - metrics.getHeight()
  264.                 - metrics.getDescent() - 4;
  265.  
  266.             // draw the label
  267.             g.setColor(Color.black);
  268.             g.drawString(labels[i], cx, cy);
  269.             cy -= metrics.getHeight() - 3;
  270.  
  271.             // draw the shadow
  272.             g.fillRect(cx + 4, cy - (values[i] * scale) - 4, 
  273.                        barWidth, (values[i] * scale));
  274.             
  275.             // draw the bar
  276.             g.setColor(colors[i]);
  277.             if (styles[i] == STRIPED) {
  278.                 for (int k=0; k <= values[i] * scale; k+=2) {
  279.                     g.drawLine(cx, cy - k, 
  280.                                cx + barWidth, cy - k);
  281.                 }
  282.             } else {
  283.                 g.fillRect(cx, cy - (values[i] * scale), 
  284.                            barWidth + 1, (values[i] * scale) + 1);
  285.             }
  286.             cy -= (values[i] * scale) + 5;
  287.  
  288.             // draw the value on top of the bar
  289.             g.setColor(g.getColor().darker());
  290.             g.drawString(Integer.toString(values[i]), cx, cy);
  291.         }
  292.     }    
  293.     
  294.     public String getAppletInfo() {
  295.         return "Title: Bar Chart \n"
  296.             + "Author: Sami Shaio \n"
  297.             + "A simple bar chart demo.";
  298.     }
  299.     
  300.     public String[][] getParameterInfo() {
  301.         String[][] info = {
  302.             {"title", "string", "The title of bar graph.  Default is 'Chart'"},
  303.             {"scale", "int", "The scale of the bar graph.  Default is 10."},
  304.             {"columns", "int", "The number of columns/rows.  Default is 5."},
  305.             {"orientation", "{VERTICAL, HORIZONTAL}", 
  306.              "The orienation of the bar graph.  Default is VERTICAL."},
  307.             {"c#", "int", "Subsitute a number for #.  " 
  308.              + "The value/size of bar #.  Default is 0."},
  309.             {"c#_label", "string", "The label for bar #.  "
  310.              + "Default is an empty label."},
  311.             {"c#_style", "{SOLID, STRIPED}", "The style of bar #.  "
  312.              + "Default is SOLID."},
  313.             {"c#_color", "{RED, GREEN, BLUE, PINK, ORANGE, MAGENTA, CYAN, "
  314.              + "WHITE, YELLOW, GRAY, DARKGRAY}", 
  315.              "The color of bar #.  Default is GRAY."}
  316.         };
  317.         return info;
  318.     }
  319. }
  320.