/* Written by Nick Exner on 7/29/98 * * Free to distribute */ import java.applet.Applet; import java.awt.*; import java.awt.event.*; import java.applet.AudioClip; public class EstPi extends Applet { Image backCliff; Image volume; Image offscreenImage; Graphics offscreenGraphics; int maxPoints=500; int xspots[] = new int[maxPoints]; int yspots[] = new int[maxPoints]; int currspots=0, volPosition=244,prevX=0, prevY=0; int volLevel=250,numInCircle=0; double piEstimate=0.0; public void init(){ backCliff= getImage(getCodeBase(),"back.jpg"); volume = getImage(getCodeBase(),"volume.gif"); offscreenImage = createImage(this.size().width, this.size().height); offscreenGraphics = offscreenImage.getGraphics(); repaint(); } public void paint(Graphics g) { numInCircle=0; offscreenGraphics.drawImage(backCliff,0,0,this); offscreenGraphics.drawImage(volume,volPosition,125,this); offscreenGraphics.setColor(Color.black); // offscreenGraphics.drawRect(40,42,135,135); offscreenGraphics.drawOval(40,42,135,135); for (int i=0; i < currspots; i++) { offscreenGraphics.fillOval(xspots[i]-2,yspots[i]-2,4,4); if (isCircle(xspots[i], yspots[i])) numInCircle++; } //area of square = 18225 // r*r = 4556.25 //(r*r)/squareA = .25 if (currspots > 0) piEstimate=roundDouble((4.0*numInCircle/currspots),5); else piEstimate=0.0; offscreenGraphics.setColor(Color.green); offscreenGraphics.drawString(""+volLevel,293,96); offscreenGraphics.drawString(""+piEstimate,254,51); offscreenGraphics.drawString(""+numInCircle,229,74); offscreenGraphics.drawString(""+(currspots-numInCircle),293,74); g.drawImage(offscreenImage,0,0,this); } // Paint public double roundDouble(double x, int place) { double times=1.0; for (int j=0;j Math.sqrt((107.5-x)*(107.5-x) + (109.5-y)*(109.5-y))) return true; else return false; } public boolean mouseDown(Event evt, int x, int y) { if (x>=40 && x <=175) if (y>=42 && y<=177) addspot(x,y); if (x>=221 && x <=256) // Fire Pressed if (y>=166 && y<=183) firePoints(); if (x>=270 && x <=300) // Clear Pressed if (y>=166 && y<=183) clearPoints(); prevX=x; prevY=y; System.out.println("x: "+(x)+"y: "+(y)); // Debug position return true; } public boolean mouseDrag(Event evt, int x, int y) { if (x>=202 && x <=318){ if (y>=121 && y<=145) { if (x>=volPosition && x<=(volPosition+25)) if (prevX < x && volLevel < 500) { volPosition=x-10; for(int j=0; j<(x-prevX);j++) volLevel+=5; } if (prevX >= x && volLevel > 0) { volPosition=x-10; for(int j=0; j<(prevX-x);j++) volLevel-=5; } prevX=x; prevY=y; if (x>=308) volLevel=500; if (x<=212) volLevel=0; if (x == 293) volLevel=250; repaint(); } } return true; } public void update(Graphics g) { paint(g); } public void addspot(int x, int y) { if (currspots