import java.awt.*; import java.applet.*; import java.util.Vector; public class Histogram extends Applet { private int WIDTH, HEIGHT; private Image offScreenImage; private Graphics offScreenGraphics; public void init() { Dimension dim = getSize(); WIDTH = dim.width; HEIGHT = dim.height; } private Vector Vectorize(String str) { Vector items = new Vector(); while (str.length() > 0) { int index = 0; while (index < str.length() && Character.isWhitespace(str.charAt(index))) { index++; } if (index < str.length()) { String item = ""; while (index < str.length() && !Character.isWhitespace(str.charAt(index))) { item += str.charAt(index); index++; } items.addElement(item); } if (index < str.length()) { str = str.substring(index+1, str.length()); } else { str = ""; } } return items; } public void drawHistogram(String dataString) { Vector data = Vectorize(dataString); offScreenImage = createImage(WIDTH, HEIGHT); offScreenGraphics = offScreenImage.getGraphics(); for (int i = 0; i < data.size(); i+=2) { int size = Integer.parseInt((String)data.elementAt(i+1)); offScreenGraphics.setColor(Color.yellow); offScreenGraphics.fillRect(0, 40*i/2, size, 40); offScreenGraphics.setColor(Color.black); offScreenGraphics.drawRect(0, 40*i/2, size, 40); offScreenGraphics.drawString(data.elementAt(i)+ " ("+ size +")", 10, (i/2)*40+25); } paint(getGraphics()); } public void paint(Graphics g) { g.drawImage(offScreenImage, 0, 0, null); } }