// Random Marble Grabber by Exner Enterprises, C.1997-99 // December 8, 1999 9:30-11:30AM Central Time // Author: Nicholas Exner // http://www.uiuc.edu/ph/www/exner // Free to redistribute/publish/modify pending that // the above comments are retained. import java.applet.*; import java.awt.*; import java.awt.event.*; import java.net.URL; import java.util.*; public class Marble extends Applet { Image offscreenImage; Graphics h; int appSizeX, appSizeY; Vector objVect; public void init(){ appSizeX=this.size().width; appSizeY=this.size().height; setBackground(Color.black); Back.loadImages(this); offscreenImage = createImage(appSizeX, appSizeY); h = offscreenImage.getGraphics(); objVect = new Vector(); objVect.addElement(new Back()); repaint(); } public void paint(Graphics g) { Enumeration e = objVect.elements(); while(e.hasMoreElements()) { ShowableObject c = (ShowableObject)e.nextElement(); c.show(h); } g.drawImage(offscreenImage,0,0,this); } public boolean mouseDown(Event evt, int x, int y) { System.out.println("x: "+(x)+"y: "+(y)); // Debug position Enumeration e = objVect.elements(); while(e.hasMoreElements()) { ShowableObject c = (ShowableObject)e.nextElement(); c.setMousePoint(x,y); } repaint(); return true; } public boolean mouseUp(Event evt, int x, int y) { return true; } public boolean mouseDrag(Event evt, int x, int y) { Enumeration e = objVect.elements(); while(e.hasMoreElements()) { ShowableObject c = (ShowableObject)e.nextElement(); c.mouseDrag(x,y); } repaint(); return true; } public void update(Graphics g) { paint(g); } } // End class Marble /////////////////// ShowableObject Class //////////////////////////// class ShowableObject extends Applet { static boolean drawBlue=false; public void show (Graphics g) { return; } public void keyPress(Event evt, int key) { return; } public void mouseDrag(int x, int y) { return; } public void setMousePoint(int x ,int y) { return; } } /////////////////// Back Class /////////////////////////////// class Back extends ShowableObject { static Image backImg; int count=0,numMarbles=10; int marbleArray []; public Back() { //Constructor marbleArray = new int[numMarbles]; reset(); } public static void loadImages(Applet parent){ backImg = parent.getImage(parent.getCodeBase(),"back.jpg"); } public void show(Graphics g) { g.drawImage(backImg,0,0,this); g.setColor(new Color(255,150,150)); g.drawString(""+count,351,63); if (count>0) g.drawString(""+chiSquareTest(),315,99); else g.drawString("0",315,99); for (int i=0;i< numMarbles;i++) { g.drawString(""+marbleArray[i],58,187+(19*i)); } } public void dropMarble() { count++; marbleArray[(int)(Math.random()*numMarbles)]++; } public void reset() { count=0; for (int i=0;i< numMarbles;i++) { marbleArray[i]=0; } } public double chiSquareTest() { double expected = (count*1.0)/numMarbles; double temp = 0.0; for (int i=0;i< numMarbles;i++) { temp += ((marbleArray[i]-expected)*(marbleArray[i]-expected))/expected; } return cutDouble(temp); } public double cutDouble(double value) { int dog=(int)Math.round(value*1000); return dog/1000.0; } public void setMousePoint(int x ,int y) { if (x > 248 && x < 382) { if (y > 124 && y < 177) { // Drop 1 marble dropMarble(); } if (y > 178 && y < 228) { // Drop 10 marbles for (int i = 0; i< 10;i++) dropMarble(); } if (y > 229 && y < 278) { // Drop 100 marbles for (int i = 0; i< 100;i++) dropMarble(); } if (y > 317 && y < 365) { // Reset reset(); } } } } // End Class Back