/* JavaBallsBall.java * Copyright 1997 Joel Peterson. * All rights reserved. * * Permission to use and distribute unmodified copies of this file is freely * granted, provided that this notice is included and credit is given. * * The following HTML source is an example of acceptable credit. It should * be included on your web page in close proximity to your use of this code: * * (APPLICATION NAME) makes use of Java classes provided by * * Joel Peterson. * * Permission to modify, use and distribute copies of this file is granted * provided that the above terms are followed and a disclaimer of the changes * made is included. * * Because the program is licensed free of charge, there is no warranty * for the program, to the extent permitted by applicable law. Except when * otherwise stated in writing the I provide the program "as is" without * warranty of any kind, either expressed or implied, including, but not * limited to, the implied warranties of merchantability and fitness for * a particular purpose. The entire risk as to the quality and performance * of the program is with you. Should the program prove defective, you * assume the cost of all necessary servicing, repair or correction. * * In no event unless required by applicable law or agreed to in writing will * I be liable to you for damages, including any general, special, incidental * or consequential damages arising out of the use or inability to use the * program (including but not limited to loss of data or data being rendered * inaccurate or losses sustained by you or third parties or a failure of the * program to operate with any other programs), even if such holder or other * party has been advised of the possibility of such damages. */ import java.awt.*; import java.util.*; public class JavaBallsBall { private static Random r=new Random(); private JavaBallsBall next; private double x,y,radius; private double dx,dy; private double accel; private static double prob; private boolean collided; JavaBallsBall(JavaBallsBall Next,Dimension Size) { radius=2.0+12.0*r.nextDouble(); next=Next; x=(double)Size.width/2.0; y=(double)Size.height/2.0; if (r.nextDouble()<0.5) dx=-radius*r.nextDouble()-.00001; else dx=radius*r.nextDouble()+.00001; if (r.nextDouble()<0.5) dy=-radius*r.nextDouble()-.00001; else dy=radius*r.nextDouble()+.00001; accel=1.0; collided=false; } public static final void setProb(double Prob) { prob=Prob; } private void collide() { JavaBallsBall check; double dist,circledist; for (check=next;check!=null;check=check.next) { dist=(x-check.x)*(x-check.x)+(y-check.y)*(y-check.y); circledist=radius+check.radius; if (dist(double)Size.width) { // choose new dx; dx=-radius*r.nextDouble()-.00001; // choose a new accel //accel=Math.sqrt(Math.abs(r.nextGaussian())+1.0); x=Size.width-radius; } else if (x(double)Size.height) { dy=-radius*r.nextDouble()-.00001; //accel=Math.sqrt(Math.abs(r.nextGaussian())+1.0); y=Size.height-radius; } else if (yprob) { double xr,yr; g.setColor(Color.yellow); if (r.nextDouble()>0.5) { xr=2*radius; yr=radius; } else { xr=radius; yr=2*radius; } g.fillOval((int)(x-xr),(int)(y-yr), 2*(int)xr,2*(int)yr); next=new JavaBallsBall(next,parent.size()); next.x=x+next.radius; next.y=y+next.radius; x=x-radius; y=y-radius; next.paint(g,this,parent); } else { g.setColor(Color.red); g.drawLine((int)(x-radius),(int)y,(int)(x+radius),(int)y); g.drawLine((int)x,(int)(y-radius),(int)x,(int)(y+radius)); if (prev!=null) prev.next=next; else parent.setBalls(next); if (next!=null) next.paint(g,prev,parent); } } else { g.setColor(Color.blue); g.fillOval((int)(x-radius),(int)(y-radius), 2*(int)radius,2*(int)radius); if (next!=null) next.paint(g,this,parent); } } }