CHART.SRC
上传用户:sunrenlu
上传日期:2022-06-13
资源大小:1419k
文件大小:17k
源码类别:

操作系统开发

开发平台:

DOS

  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.                    import java.net.URL;
  38.                    public class Chart extends java.applet.Applet {
  39.                        static final int    VERTICAL = 0;
  40.                        static final int    HORIZONTAL = 1;
  41.                        static final int    SOLID = 0;
  42.                        static final int    STRIPED = 1;
  43.                        int                 orientation;
  44.                        String              title;
  45.                        Font                titleFont;
  46.                        FontMetrics         titleFontMetrics;
  47.                        int                 titleHeight = 15;
  48.                        int                 columns;
  49.                        int                 values[];
  50.                        Object              colors[];
  51.                        Object              labels[];
  52.                        int                 styles[];
  53.                        int                 scale = 10;
  54.                        int                 maxLabelWidth = 0;
  55.                        int                 barWidth;
  56.                        int                 barSpacing = 10;
  57.                        int                 max = 0;
  58.                        public synchronized void init() {
  59.                            String rs;
  60.                            
  61.                            titleFont = new java.awt.Font("Courier", Font.BOLD, 12);
  62.                            titleFontMetrics = getFontMetrics(titleFont);
  63.                            title = getParameter("title");
  64.                            if (title == null) {
  65.                                title = "Chart";
  66.                            }
  67.                            rs = getParameter("columns");
  68.                            if (rs == null) {
  69.                                columns = 5;
  70.                            } else {
  71.                                columns = Integer.parseInt(rs);
  72.                            }
  73.                            rs = getParameter("scale");
  74.                            if (rs == null) {
  75.                                scale = 10;
  76.                            } else {
  77.                                scale = Integer.parseInt(rs);
  78.                            }
  79.                            rs = getParameter("orientation");
  80.                            if (rs == null) {
  81.                                orientation = VERTICAL;
  82.                            } else if (rs.toLowerCase().equals("vertical")) {
  83.                                orientation = VERTICAL;
  84.                            } else if (rs.toLowerCase().equals("horizontal")) {
  85.                                orientation = HORIZONTAL;
  86.                            } else {
  87.                                orientation = VERTICAL;
  88.                            }
  89.                            values = new int[columns];
  90.                            colors = new Color[columns];
  91.                            labels = new String[columns];
  92.                            styles = new int[columns];
  93.                            for (int i=0; i < columns; i++) {
  94.                                // parse the value for this column
  95.                                rs = getParameter("C" + (i+1));
  96.                                if (rs != null) {
  97.                                    try {
  98.                                        values[i] = Integer.parseInt(rs);
  99.                                    } catch (NumberFormatException e) {
  100.                                        values[i] = 0;
  101.                                    }
  102.                                }
  103.                                if (values[i] > max) {
  104.                                    max = values[i];
  105.                                }
  106.                                // parse the label for this column
  107.                                rs = getParameter("C" + (i+1) + "_label");
  108.                                labels[i] = (rs == null) ? "" : rs;
  109.                                maxLabelWidth = Math.max(titleFontMetrics.stringWidth((String)(labels[i])),
  110.                                                         maxLabelWidth);
  111.                                // parse the bar style
  112.                                rs = getParameter("C" + (i+1) + "_style");
  113.                                if (rs == null || rs.toLowerCase().equals("solid")) {
  114.                                    styles[i] = SOLID;
  115.                                } else if (rs.toLowerCase().equals("striped")) {
  116.                                    styles[i] = STRIPED;
  117.                                } else {
  118.                                    styles[i] = SOLID;
  119.                                }
  120.                                // parse the color attribute for this column
  121.                                rs = getParameter("C" + (i+1) + "_color");
  122.                                if (rs != null) {
  123.                                    if (rs.equals("red")) {
  124.                                        colors[i] = Color.red;
  125.                                    } else if (rs.equals("green")) {
  126.                                        colors[i] = Color.green;
  127.                                    } else if (rs.equals("blue")) {
  128.                                        colors[i] = Color.blue;
  129.                                    } else if (rs.equals("pink")) {
  130.                                        colors[i] = Color.pink;
  131.                                    } else if (rs.equals("orange")) {
  132.                                        colors[i] = Color.orange;
  133.                                    } else if (rs.equals("magenta")) {
  134.                                        colors[i] = Color.magenta;
  135.                                    } else if (rs.equals("cyan")) {
  136.                                        colors[i] = Color.cyan;
  137.                                    } else if (rs.equals("white")) {
  138.                                        colors[i] = Color.white;
  139.                                    } else if (rs.equals("yellow")) {
  140.                                        colors[i] = Color.yellow;
  141.                                    } else if (rs.equals("gray")) {
  142.                                        colors[i] = Color.gray;
  143.                                    } else if (rs.equals("darkGray")) {
  144.                                        colors[i] = Color.darkGray;
  145.                                    } else {
  146.                                        colors[i] = Color.gray;
  147.                                    }
  148.                                } else {
  149.                                    colors[i] = Color.gray;
  150.                                }
  151.                            }
  152.                            switch (orientation) {
  153.                              case VERTICAL:
  154.                              default:
  155.                                barWidth = maxLabelWidth;
  156.                                resize(Math.max(columns * (barWidth + barSpacing),
  157.                                                titleFontMetrics.stringWidth(title)) +
  158.                                       titleFont.getSize() + 5,
  159.                                       (max * scale) + (2 * titleFont.getSize()) + 5 + titleFont.getSize());
  160.                                break;
  161.                              case HORIZONTAL:
  162.                                barWidth = titleFont.getSize();
  163.                                resize(Math.max((max * scale) + titleFontMetrics.stringWidth("" + max),
  164.                                                titleFontMetrics.stringWidth(title)) + maxLabelWidth + 5,
  165.                                       (columns * (barWidth + barSpacing)) + titleFont.getSize() + 10);
  166.                                break;
  167.                            }
  168.                        }
  169.                        public synchronized void paint(Graphics g) {
  170.                            int i, j;
  171.                            int cx, cy;
  172.                            char l[] = new char[1];
  173.                            // draw the title centered at the bottom of the bar graph
  174.                            g.setColor(Color.black);
  175.                            i = titleFontMetrics.stringWidth(title);
  176.                            g.setFont(titleFont);
  177.                            g.drawString(title, Math.max((size().width - i)/2, 0),
  178.                                         size().height - titleFontMetrics.getDescent()); 
  179.                            for (i=0; i < columns; i++) {
  180.                                switch (orientation) {
  181.                                  case VERTICAL:
  182.                                  default:
  183.                                    // set the next X coordinate to account for the label
  184.                                    // being wider than the bar size().width.
  185.                                    cx = (Math.max((barWidth + barSpacing),maxLabelWidth) * i) +
  186.                                        barSpacing;
  187.                                    // center the bar chart
  188.                                    cx += Math.max((size().width - (columns *
  189.                                                             (barWidth + (2 * barSpacing))))/2,0);
  190.                                    
  191.                                    // set the next Y coordinate to account for the size().height
  192.                                    // of the bar as well as the title and labels painted
  193.                                    // at the bottom of the chart.
  194.                                    cy = size().height - (values[i] * scale) - 1 - (2 * titleFont.getSize());
  195.                                    // draw the label
  196.                                    g.setColor(Color.black);                
  197.                                    g.drawString((String)labels[i], cx,
  198.                                                 size().height - titleFont.getSize() - titleFontMetrics.getDescent());
  199.                                    // draw the shadow bar
  200.                                    if (colors[i] == Color.black) {
  201.                                        g.setColor(Color.gray);
  202.                                    }
  203.                                    g.fillRect(cx + 5, cy - 3, barWidth,  (values[i] * scale));
  204.                                    // draw the bar with the specified color
  205.                                    g.setColor((Color)(colors[i]));
  206.                                    switch (styles[i]) {
  207.                                      case SOLID:
  208.                                      default:
  209.                                        g.fillRect(cx, cy, barWidth, (values[i] * scale));
  210.                                        break;
  211.                                      case STRIPED:
  212.                                        {
  213.                                            int steps = (values[i] * scale) / 2;
  214.                                            int ys;
  215.                                            for (j=0; j < steps; j++) {
  216.                                                ys = cy + (2 * j);
  217.                                                g.drawLine(cx, ys, cx + barWidth, ys);
  218.                                            }
  219.                                        }
  220.                                        break;
  221.                                    }
  222.                                    g.drawString("" + values[i],
  223.                                                 cx,
  224.                                                 cy - titleFontMetrics.getDescent());
  225.                                    break;
  226.                                  case HORIZONTAL:
  227.                                    // set the Y coordinate
  228.                                    cy = ((barWidth + barSpacing) * i) + barSpacing;
  229.                                                   
  230.                                    // set the X coordinate to be the size().width of the widest
  231.                                    // label 
  232.                                    cx = maxLabelWidth + 1;
  233.                                    cx += Math.max((size().width - (maxLabelWidth + 1 +
  234.                                                             titleFontMetrics.stringWidth("" +
  235.                                                                                   max) +
  236.                                                             (max * scale))) / 2, 0);
  237.                                    // draw the labels and the shadow
  238.                                    g.setColor(Color.black);                
  239.                                    g.drawString((String)labels[i], cx - maxLabelWidth - 1,
  240.                                                 cy + titleFontMetrics.getAscent());
  241.                                    if (colors[i] == Color.black) {
  242.                                        g.setColor(Color.gray);
  243.                                    }
  244.                                    g.fillRect(cx + 3,
  245.                                               cy + 5,
  246.                                               (values[i] * scale),
  247.                                               barWidth);
  248.                                    // draw the bar in the current color
  249.                                    g.setColor((Color)(colors[i]));
  250.                                    switch (styles[i]) {
  251.                                      case SOLID:
  252.                                      default:
  253.                                        g.fillRect(cx,
  254.                                                   cy,
  255.                                                   (values[i] * scale),
  256.                                                   barWidth);
  257.                                        break;
  258.                                      case STRIPED:
  259.                                        {
  260.                                            int steps = (values[i] * scale) / 2;
  261.                                            int ys;
  262.                                            for (j=0; j < steps; j++) {
  263.                                                ys = cx + (2 * j);
  264.                                                g.drawLine(ys, cy, ys, cy + barWidth);
  265.                                            }
  266.                                        }
  267.                                        break;
  268.                                    }
  269.                                    g.drawString("" + values[i],
  270.                                                 cx + (values[i] * scale) + 3,
  271.                                                 cy + titleFontMetrics.getAscent());
  272.                                                 
  273.                                    break;
  274.                                }
  275.                            }
  276.                        }
  277.                    }