home *** CD-ROM | disk | FTP | other *** search
/ Java Programmer's Toolkit / Java Programmer's Toolkit.iso / solaris2 / book / applets / graphs / bardiagr.jav < prev    next >
Encoding:
Text File  |  1995-10-16  |  7.8 KB  |  292 lines

  1. /* 
  2.  * Copyright (c) 1994-1995 Sun Microsystems, Inc. All Rights Reserved.
  3.  *
  4.  * Permission to use, copy, modify, and distribute this software
  5.  * and its documentation for NON-COMMERCIAL or COMMERCIAL purposes and
  6.  * without fee is hereby granted.
  7.  * Please refer to the file http://java.sun.com/copy_trademarks.html
  8.  * for further important copyright and trademark information and to
  9.  * http://java.sun.com/licensing.html for further important licensing
  10.  * information for the Java (tm) Technology.
  11.  *
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
  13.  * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
  14.  * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  15.  * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
  16.  * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
  17.  * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
  18.  *
  19.  * THIS SOFTWARE IS NOT DESIGNED OR INTENDED FOR USE OR RESALE AS ON-LINE
  20.  * CONTROL EQUIPMENT IN HAZARDOUS ENVIRONMENTS REQUIRING FAIL-SAFE
  21.  * PERFORMANCE, SUCH AS IN THE OPERATION OF NUCLEAR FACILITIES, AIRCRAFT
  22.  * NAVIGATION OR COMMUNICATION SYSTEMS, AIR TRAFFIC CONTROL, DIRECT LIFE
  23.  * SUPPORT MACHINES, OR WEAPONS SYSTEMS, IN WHICH THE FAILURE OF THE
  24.  * SOFTWARE COULD LEAD DIRECTLY TO DEATH, PERSONAL INJURY, OR SEVERE
  25.  * PHYSICAL OR ENVIRONMENTAL DAMAGE ("HIGH RISK ACTIVITIES").  SUN
  26.  * SPECIFICALLY DISCLAIMS ANY EXPRESS OR IMPLIED WARRANTY OF FITNESS FOR
  27.  * HIGH RISK ACTIVITIES.
  28.  */
  29.  
  30. import java.lang.*; 
  31. import java.io.InputStream;
  32. import java.awt.*;
  33. import java.net.*;
  34. import java.util.*;
  35.  
  36. /**
  37.  * A image loop applet, first raise smiles and then drop them
  38.  *
  39.  * @author      Siebe R. Brouwer
  40.  * version      1.1, 29 September 1995
  41.  */
  42.  
  43. class BarDiagram extends Diagram {
  44.     
  45.     protected Vector fields;
  46.     protected Vector indexs;
  47.     protected int indexNumber;
  48.     protected Display display;
  49.     protected int virtualYbegin;
  50.     protected int virtualYgap;
  51.     protected int yBegin;
  52.     
  53.     BarDiagram(Display d, int vYbegin, int vYgap) {
  54.     display = d;    
  55.     fields = new Vector();
  56.     indexs = new Vector();
  57.         virtualYgap = vYgap;
  58.     virtualYbegin = vYbegin;
  59.     float i = (float)vYbegin / (float)vYgap;
  60.     yBegin = (int)(i * display.yGap());
  61.         indexNumber = 0;
  62.     }
  63.  
  64.     public void addIndex(String indexLabel, Color color) {
  65.         if(!hasIndex(indexLabel) && !fields.isEmpty()) {
  66.         indexs.addElement(new Index(indexLabel, color, indexNumber));
  67.         updateFields(indexNumber, color);
  68.         indexNumber++;
  69.     }
  70.     }
  71.  
  72.     public void addField(String label) {
  73.     if(!hasField(label)) {
  74.         fields.addElement(new Field(label));
  75.     }
  76.     }
  77.     public void addValue(int vHeight, String indexLabel, String fieldLabel) {
  78.         if(hasField(fieldLabel) && hasIndex(indexLabel)) {
  79.         int f = getField(fieldLabel);
  80.         int i = getIndex(indexLabel);
  81.         Index index = (Index)indexs.elementAt(i);
  82.         Field field = (Field)fields.elementAt(f);
  83.         
  84.         int indexNumber = index.indexNumber();
  85.         int height = convertYvalue(vHeight);
  86.         
  87.         field.updateBar(indexNumber, height); 
  88.         }
  89.     }
  90.     
  91.     protected boolean hasField(String fieldLabel) {
  92.         for(Enumeration e = fields.elements();e.hasMoreElements() ; ) {
  93.         Field field= (Field)e.nextElement();
  94.             if (field.label() == fieldLabel) {
  95.            
  96.         return true;
  97.         }
  98.     }
  99.     
  100.     return false;
  101.     }
  102.     protected boolean hasIndex(String indexLabel) {
  103.     for(Enumeration e = indexs.elements();e.hasMoreElements() ; ) {
  104.         Index index= (Index)e.nextElement();
  105.             if (index.label() == indexLabel) {
  106.      
  107.         return true;
  108.         }
  109.     }
  110.     
  111.     return false;
  112.     }
  113.     
  114.     protected int getField(String fieldLabel) {
  115.     for(Enumeration e = fields.elements();e.hasMoreElements() ; ) {
  116.         Field field = (Field)e.nextElement();
  117.             if (field.label() == fieldLabel) {
  118.            return fields.indexOf(field);
  119.         }
  120.     }
  121.         return -1;
  122.     }
  123.     protected int getIndex(String indexLabel) {
  124.         for(Enumeration e = indexs.elements();e.hasMoreElements() ; ) {
  125.         Index index= (Index)e.nextElement();
  126.             if (index.label() == indexLabel) {
  127.            return indexs.indexOf(index);
  128.         }
  129.     }
  130.         return -1;
  131.     }
  132.     protected void updateFields(int indexNumber, Color color) {
  133.     for(Enumeration e = fields.elements();e.hasMoreElements() ; ) {
  134.         Field field = (Field)e.nextElement();
  135.             field.addBar(color);
  136.     }
  137.     }
  138.     
  139.     public void drawData(Graphics g, int gaps, int left, int moved) { 
  140.     int xStart = display.xStart();
  141.     int yStart = display.yStart();
  142.     int xTop = display.xTop();
  143.     int yTop = display.yTop();
  144.     int xGap = display.xGap();
  145.     int yGap = display.yGap();
  146.     int xBegin = xStart + moved;
  147.     int vYgap = virtualYbegin;
  148.     int xIndex = xTop - ((xTop - xStart) / 3);
  149.     int yIndex = 50;
  150.     
  151.     
  152.     for(int i = yStart; i >= yTop; i -= yGap) {
  153.         g.drawString(Integer.toString(vYgap), xStart - 40, i);
  154.         vYgap += virtualYgap;
  155.     }
  156.     
  157.     g.drawString("INDEX", xIndex, yIndex);
  158.     yIndex+=5;
  159.     g.drawLine(xIndex, yIndex, xIndex + 30, yIndex);
  160.     yIndex +=15;
  161.  
  162.     for(Enumeration e = indexs.elements();e.hasMoreElements() ; ) {
  163.         Index index= (Index)e.nextElement();
  164.         index.drawIndex(g, xIndex, yIndex);    
  165.         yIndex += 15;
  166.     }
  167.     for(Enumeration e = fields.elements();e.hasMoreElements() ; ) {
  168.         Field field= (Field)e.nextElement();
  169.        
  170.         field.drawField(g, xBegin, xStart, yStart, xTop, yTop, xGap);
  171.         xBegin += xGap;       
  172.     }
  173.     }
  174.     
  175.     protected int convertYvalue(float i) {
  176.         float j = (float)virtualYbegin / (float)virtualYgap;
  177.     int yBegin = (int)(j * display.yGap());
  178.     int yGap = display.yGap();
  179.     float virtual = i / virtualYgap;
  180.     virtual = virtual * yGap;
  181.     return (int)(virtual - yBegin);
  182.     }
  183. }
  184.  
  185.     
  186. class Field {
  187.     
  188.     protected String label;
  189.     protected Vector bars;
  190.     protected int depth;
  191.     
  192.     Field(String l) {
  193.     label = l;
  194.     depth = 0;
  195.     bars = new Vector();
  196.     }
  197.     
  198.     public void addBar(Color color) {
  199.     depth++;
  200.     bars.addElement(new Bar(0, color));
  201.     }
  202.  
  203.     public void updateBar(int indexNumber, int height) {
  204.     
  205.     Bar bar= (Bar)bars.elementAt(indexNumber);
  206.     bar.changeHeight(height);
  207.     }
  208.     
  209.     public String label() {
  210.     return label;
  211.     }
  212.  
  213.     public void drawField(Graphics g, int xBegin, int xStart, int yStart, int xTop, int yTop,int xCap) {
  214.     
  215.     if(xBegin >= xStart && xBegin <= xTop) {
  216.         g.setColor(Color.black);
  217.         g.drawString(label, xBegin, yStart + 15);
  218.     }
  219.         int barLength; 
  220.     xCap -= (depth * 2);
  221.     barLength = depth > 0 ? (xCap / depth): xCap;
  222.     int begin = xBegin;
  223.     Graphics graphics = g.create();
  224.  
  225.     for(Enumeration e = bars.elements();e.hasMoreElements() ; ) {
  226.         graphics.clipRect(xStart+1, yTop, xTop - xStart, yStart - yTop);
  227.         Bar bar = (Bar)e.nextElement();
  228.             int height = bar.height();
  229.                 
  230.         graphics.setColor(Color.black);
  231.         graphics.drawRect(begin, yStart - height, barLength, height);
  232.         graphics.setColor(bar.color());
  233.         graphics.fillRect(begin+1, yStart - height +1, barLength -1, height -1);    
  234.         begin += barLength + 2;    
  235.     } 
  236.     }
  237. }
  238.     
  239. class Index {
  240.     
  241.     protected String label;
  242.     protected int indexNumber;
  243.     protected Color color;
  244.  
  245.     Index(String l, Color c, int i) {
  246.     label = l;
  247.     indexNumber = i;
  248.     color = c;
  249.     }
  250.     
  251.     public String label() {
  252.     return label;
  253.     }
  254.     public int indexNumber() {
  255.         return indexNumber;
  256.     }
  257.     public Color color() {
  258.     return color;
  259.     }
  260.     public void drawIndex(Graphics g, int x, int y) {
  261.         g.setColor(Color.black);
  262.     g.drawRect(x,y,5,5);
  263.     g.setColor(color);
  264.     g.fillRect(x+1,y+1,4,4);
  265.     g.setColor(Color.black);
  266.     g.drawString(label, x+10,y+5);
  267.     }
  268. }
  269.  
  270. class Bar {
  271.     protected Color color;
  272.     protected int height;
  273.         
  274.     Bar(int h, Color c) {
  275.     height = h;
  276.     color = c;
  277.     }
  278.     
  279.     public Color color() {
  280.     return color;
  281.     }
  282.     public int height() {
  283.     return height;
  284.     }
  285.     public void changeHeight(int h) {
  286.     height = h;
  287.     }
  288.     public void changeColor(Color c) {
  289.         color = c;
  290.     }
  291. }
  292.