/*File: drawHistogram.java */ //Now we try to create a more general histogram class import java.awt.*; import java.applet.*; /** * Draws a histogram given appropriate information * @author Charles Stanton * @version 1.1 January 29, 1997 */ class drawHistogram extends Canvas { int[] count; //array of counts for each bin int bins; //number of bins Applet applet; double[] theoreticalCount; // The theoretical distribution counts boolean drawTheoretical; // Whether to plot the theoretical histogram // Which must be supplied /** * Initialize the histogram, no theoretical lines drawn * @param count is an array of counts of the objects in the bins * @param bins tells how many bins to make * @param applet is the applet calling the class */ public drawHistogram(int[] count, int bins, Applet applet) { this.applet=applet; this.bins = bins; this.count =count; } /** * Initialize the histogram to draw theoretical distribution * @param count is an array of counts of the objects in the bins * @param bins tells how many bins to make * @param applet is the applet calling the class * @param theoreticalCount is the array of theoretical distribution values */ public drawHistogram(int[] count, int bins, double[] theoreticalCount, Applet applet) { this.applet=applet; this.bins = bins; this.count =count; this.theoreticalCount=theoreticalCount; drawTheoretical = true; } /** * Painting method for drawHistogram */ public void paint(Graphics g){ Dimension s=size(); int height=s.height; int width=s.width/(bins+2); int y; for (int j=0; j< bins; j++) { g.setColor(Color.white); g.fillRect((j+1)*width,height-10-count[j]*4,width,count[j]*4); if (drawTheoretical) { y = (int)(height-10-theoreticalCount[j]*4); g.setColor(Color.red); g.drawLine((j+1)*width,y,(j+2)*width-1,y); g.drawLine((j+1)*width,y+1,(j+2)*width-1,y+1); } } } public void update(int[] count){ this.count = count; repaint(); } public void update(int[] count, double[] theoreticalCount){ this.count = count; this.theoreticalCount=theoreticalCount; repaint(); } /** * clears Historgram and resets count and the number of bins * @param count is the number of objects in each bin * @param bins is the new number of bins */ public void clearHistogram(int[] count,int bins) { this.bins = bins; this.count = count; repaint(); } public void clearHistogram(int[] count,int bins, double[] theoreticalCount) { this.bins = bins; this.count = count; this.theoreticalCount=theoreticalCount; repaint(); } public Dimension preferredSize(){ Dimension d = new Dimension(200, 250); return d; } }