// // hysteria.java // hysteria // // Created by Chris Thiel on Fri Dec 12 2003. // Copyright (c) 2003 Chris Thiel. All rights reserved. // Educational use is free with permission // send an email to me at cct@ktb.net // A simple Java applet that dynamically makes a hystogram // import javax.imageio.*; import java.awt.*; import java.applet.*; import java.awt.event.*; import java.util.*; public class hysteria extends Applet implements ActionListener,MouseListener,ItemListener, KeyListener { myCanvas canvas; Panel np=new Panel(); Image imago; Panel wp=new Panel(); Panel cp=new Panel(); Panel sp=new Panel(); Panel ep=new Panel(); Label l1=new Label("St. Francis\nMath Dept"); Label l2=new Label("Display\nBin Size",2); Label l3=new Label("\nShow Stats",2); Label l4=new Label("\nColor ",2); Label l5=new Label("\nBackground",2); public TextArea List=new TextArea("",20,10,1); public Button clearBtn=new Button("Clear"); public Button example1Btn=new Button("Example 1"); public Button example2Btn=new Button("Simulate One"); public Button redrawBtn=new Button("Redraw"); Choice DistributionMenu = new Choice(); Choice BinSizeMenu = new Choice(); Choice ShowStatsMenu = new Choice(); Choice vColorMenu = new Choice(); Choice bColorMenu = new Choice(); // globals public int xMax=540; public int yMax=400; int i; String[] cName = new String[12]; // names of the colors public void keyTyped(KeyEvent e) { } public void keyPressed(KeyEvent e) { } public void keyReleased(KeyEvent e) { UpdateEverything(); canvas.repaint(); // repaint(); } public void mouseClicked(MouseEvent e){ } public void mouseEntered(MouseEvent e){ } public void mouseExited(MouseEvent e){} public void mouseReleased(MouseEvent e){ UpdateEverything(); repaint(); canvas.repaint(); } public void mousePressed(MouseEvent e){ } public void itemStateChanged(ItemEvent ie){ UpdateEverything(); repaint(); canvas.repaint(); } public void init() { //URL url=getCodeBase(); l1.setForeground(Color.yellow); l2.setForeground(Color.yellow); l3.setForeground(Color.yellow); l4.setForeground(Color.yellow); l5.setForeground(Color.yellow); imago=getImage(getCodeBase(), "sflogo.gif"); cName[0]="black"; cName[1]="red"; cName[2]="green"; cName[3]="blue"; cName[4]="pink"; cName[5]="orange"; cName[6]="magenta"; cName[7]="cyan"; cName[8]="white"; cName[9]="yellow"; cName[10]="gray"; cName[11]="darkGrey"; setLayout(new BorderLayout()); canvas= new myCanvas(); canvas.setSize(xMax,yMax); add("Center", canvas ); add("South",sp); sp.setBackground(new Color(45,10,10,250)); //wp.setLayout(new GridLayout(3,1)); wp.add(List); add("West", wp); wp.setBackground(new Color(112,32,32,250)); ep.setBackground(new Color(112,32,32,250)); clearBtn.addActionListener(this); redrawBtn.addActionListener(this); example1Btn.addActionListener(this); example2Btn.addActionListener(this); sp.add(clearBtn); sp.add(redrawBtn); sp.add(example1Btn); sp.add(example2Btn); ep.setLayout(new GridLayout(10,1)); ep.add(l1);// menu for selecting which Model Distribution DistributionMenu.addItem("Data Only"); DistributionMenu.addItem("Normal"); DistributionMenu.addItem("Geometric"); ep.add(DistributionMenu); ep.add(l2);//binSize selector BinSizeMenu.addItem(".1"); BinSizeMenu.addItem(".5"); BinSizeMenu.addItem("1"); BinSizeMenu.addItem("2"); BinSizeMenu.addItem("5"); BinSizeMenu.addItem("10"); BinSizeMenu.addItem("25"); BinSizeMenu.addItem("50"); BinSizeMenu.addItem("100"); BinSizeMenu.addItem("1000"); ep.add(BinSizeMenu); BinSizeMenu.select("1"); ep.add(l3);//line thickness ShowStatsMenu.addItem("No Stats"); ShowStatsMenu.addItem("Data"); ShowStatsMenu.addItem("Data and Model"); ep.add(ShowStatsMenu); ep.add(l4);//vector color for (i=0; i<12; i++) vColorMenu.addItem(cName[i]); ep.add(vColorMenu); vColorMenu.select("blue"); ep.add(l5);//background color for (i=0; i<12; i++) bColorMenu.addItem(cName[i]); ep.add(bColorMenu); bColorMenu.select("white"); add("East", ep); //Add keylistener to immediately refresh canvas List.addKeyListener(this); repaint(); } private void clearList(){ List.setText(""); repaint(); } private void ex1(){ List.setText("1\n1\n10\n6\n13\n16\n3\n2\n2\n5\n6"); repaint(); } private void ex2(){ //Simulate a geometric event with p=1/5 int counter=1; while (Math.random() > .2) counter++; List.append(counter+"\n"); repaint(); } private Color colorOf(int c){ Color x=Color.white; switch (c) { case(0):x=Color.black; break; case(1):x=Color.red; break; case(2):x=Color.green; break; case(3):x=Color.blue; break; case(4):x=Color.pink; break; case(5):x=Color.orange; break; case(6):x=Color.magenta; break; case(7):x=Color.cyan; break; case(8):x=Color.white; break; case(9):x=Color.yellow; break; case(10):x=Color.gray; break; case(11):x=Color.darkGray; break; }//of switch return x; } private int thicknessOf(int t){ int x=t+1; switch (t) { case(3):x=5; break; case(4):x=8; break; case(5):x=10; break; case(6):x=15; break; }//of switch return x; } // the binSize private double scalingOf(int s){ double x=1.0; switch (s) { case(0):x=.1;break; case(1):x=.5;break; case(2):x=1;break; case(3):x=2;break; case(4):x=5;break; case(5):x=10;break; case(6):x=25;break; case(7):x=50;break; case(8):x=100;break; case(9):x=1000;break; } return x; } public void UpdateEverything(){ canvas.img=imago; canvas.list=List.getText(); canvas.bColour=colorOf(bColorMenu.getSelectedIndex()); canvas.vColour=colorOf(vColorMenu.getSelectedIndex()); canvas.stats=ShowStatsMenu.getSelectedIndex(); canvas.binSize=scalingOf(BinSizeMenu.getSelectedIndex()); canvas.distribution=DistributionMenu.getSelectedIndex(); } public void actionPerformed(ActionEvent e){ UpdateEverything(); Object object=e.getSource(); if (object==clearBtn){ clearList(); } if (object==example1Btn){ ex1(); } if (object==example2Btn){ ex2(); } repaint(); canvas.repaint(); } public void paint (Graphics g) { g.setColor(Color.red); g.drawString("This is here = ", 255 , 250); } } class myCanvas extends Canvas { private Font font = new Font("serif", Font.ITALIC + Font.BOLD, 14); public Image img = null; public String t; public double ax=50.0; public double ay=20.0; public double bx=40.0; public double by=-20.0; public String list; public Color bColour=Color.white, vColour=Color.blue; public int sop=11; //startOffPlace public double binSize=1; //scale factor used for user selected bin size public int stats=0; //whether to show stats public int cx, cy,n; //public int startx, starty; // holds starting coords public int distribution; // 0=none, 1=normal 2=geometric public int xMax=540; public int yMax=400; int MAX=500; int i=0, j=0; public String Title; String s; public String message; public double vx[] = new double[MAX];//hold the list of OBSERVATIONS to draw public int bar[] = new int[300]; // holds the frequency of each bar of the histogram public int barWidth, numberOfBars, unitHeight; public double min,max, mean,sd ; //hold min data value and max data value myCanvas(){ setBackground(bColour); } private void setString (String userInput) { i=0; //reset number of pairs n=0; message=null; //reset error message //convert new line chars from unix/win userInput.trim(); while (userInput.indexOf("\n") >=0) { userInput= userInput.substring(0, userInput.indexOf("\n")) + " ; " + userInput.substring(userInput.indexOf("\n")+1); } //convert return chars from macOS while (userInput.indexOf("\r") >=0) { userInput= userInput.substring(0, userInput.indexOf("\r")) + " ; " + userInput.substring(userInput.indexOf("\r")+1); } if ( !(userInput.endsWith(" ;"))) userInput = userInput+" ;"; /* //replace commas with spaces for parsing userInput=userInput.replace(',',' '); */ // Now that the lines are cleaned up read one number per line StringTokenizer t= new StringTokenizer(userInput, " ", false); while ((t.hasMoreTokens()) && (i")); //vy[i]=Float.valueOf(s).floatValue(); if (t.hasMoreTokens() ) s=t.nextToken();// the ; separator i++; }//end of if }// end of try catch(Exception e) { //message = e.toString(); message = "Huh? See Line "+(i+1)+" around \""+s+"\""; }//end of catch }//end of while loop n = i; } private int sX(double x){ return (int)(5*x); } private int sY(double y){ return 125-(int)(5*y); } public void tally(){ double sum, ss; int maxFreq=1; sum=0; ss=0; min=10000; max=-10000; for (i=0;imax) {max=vx[i];} sum+=vx[i]; ss+= vx[i]*vx[i]; } mean=sum/n; sd=Math.sqrt((ss-sum*sum/n)/(n-1)); // user selects binSize numberOfBars= (int)(Math.round((max-min)/binSize)+1); barWidth=Math.round(400/numberOfBars); if((barWidth<2)&&(n>0)) {message="There are more bars than can be displayed";} else {message=null;} for (j=0; j< numberOfBars; j++){ bar[j]=0; for (i=0; i= min+j*binSize)&&(vx[i]< min+(j+1)*binSize)) bar[j]++; } if (bar[j]>maxFreq) maxFreq=bar[j]; } unitHeight=(int)(380/maxFreq); if (unitHeight>100) unitHeight=100; } private Color coolColor(float f){ return new Color((int)(Math.random()*255), (int)(Math.random()*255), (int)(Math.random()*255), 180); } private double normalpdf(double x, double mu, double sigma){ return Math.exp(-1*(x-mu)*(x-mu)/(2*sigma*sigma))/(sigma*Math.sqrt(2*Math.PI)); } private double geompdf(double p, int n){ return Math.pow((1.0-p),(n-1))*p; } private int muteColor(int c){ c=(int)(c*100/255)+155; if (c>255) c=255; return c; } public void paint( Graphics g ){ int halfBar=(int)(barWidth/2); setString(list); tally(); setBackground(new Color(muteColor(bColour.getRed()), muteColor(bColour.getGreen()), muteColor(bColour.getBlue()), 255)); g.drawImage(img, 300, 0, this); g.setColor(vColour); g.setFont(font); g.drawString(n+" observations read ", 80, 20 ); if (stats>0) g.drawString("Mean of Data = "+mean, 310,150); if (stats>0) g.drawString("Std Dev = "+sd, 310,165); if (stats>1) g.drawString("Expected Mean = 5 ", 310,180); if (stats>1) g.drawString("Std dev = "+Math.sqrt(20), 310,195); g.drawLine (0, 400, 400, 400); for (j=0; j0) g.drawString( bar[j]+" ", halfBar+j*barWidth, 415-bar[j]*unitHeight); //freq g.setColor(coolColor(j/numberOfBars)); g.fillRect(j*barWidth,400-bar[j]*unitHeight,barWidth,bar[j]*unitHeight); // show normal or geometric dist g.setColor(Color.red); if (distribution==2) g.drawLine(j*barWidth, (int)(400-unitHeight*n*geompdf(.2,j)),(j+1)*barWidth,(int)( 400-unitHeight*n*geompdf(.2,j+1)) ); if (distribution==1) g.drawLine(j*barWidth,(int)(400-unitHeight*n*normalpdf(j*1.0,5.0,Math.sqrt(20))) ,(j+1)*barWidth,(int)(400-unitHeight*n*normalpdf(j+1.0,5.0,Math.sqrt(20))) ); } //Show data points for diag reasons: /* if (n>0) for (i=0; i