/* ballz.java - Produces different colored balls which
   move around the screen and change size.
   Created by Randy McGirr, 2011
*/
import java.awt.*; 
import java.applet.*; 

public class ballz extends Applet implements Runnable{ 
Dimension d; 
Image offI; 
Thread tt; 
int xDIR[]={4,3,5,8,6,7,8,9,2,1,3,4}; 
int yDIR[]={3,5,3,2,8,4,6,7,2,5,4,3}; 
int xLOC[]={20,20,20,20,20,20,20,20,20,20,20,20}; 
int yLOC[]={5,5,5,5,5,5,5,5,5,5,5,5}; 
int SIZE[]={50,50,50,50,50,30,30,50,50,50,30,30};
int grow[]={1,-1,1,-1,1,-1,1,1,-1,-1,1,-1};
Color colors[] = { Color.blue, Color.red, Color.cyan, Color.green, Color.magenta, Color.yellow, new Color(122,50,200),
   new Color(200,40,100), new Color(250,10,50), new Color(50,250,150), new Color(250,200,200), new Color(200,200,250) }; 
public void init(){ 
d = getSize(); 
offI=createImage(d.width,d.height); 
tt = new Thread(this); 
tt.start(); 
} 

public void run(){ 
while(true){ 
for(int i=0; i<xDIR.length; i++){ 
if(xDIR[i]>0 && xLOC[i]>d.width-15 || xDIR[i]<0 && xLOC[i]<0 ) xDIR[i]*=-1; 
if(yDIR[i]>0 && yLOC[i]>d.height-15 || yDIR[i]<0 && yLOC[i]<0)  yDIR[i]*=-1; 
xLOC[i]+=xDIR[i]; 
yLOC[i]+=yDIR[i];
if(SIZE[i]>100) grow[i]=-1;
if(SIZE[i]<20) grow[i]=1; 
SIZE[i]+=grow[i];
} 
try{ Thread.sleep(50); }catch(InterruptedException ie){ } 
repaint(); 
} 
} 

public void update(Graphics g){ 
paint(g); 
} 

public void paint(Graphics g){ 
Graphics offG = offI.getGraphics(); 
offG.setColor(Color.black); 
offG.fillRect(0,0,d.width,d.height); 
for(int i = 0; i<colors.length; i++){ 
offG.setColor(colors[i]); 
offG.fillOval(xLOC[i],yLOC[i],SIZE[i],SIZE[i]); 
} 
g.drawImage(offI,0,0,this); 
offG.dispose(); 
} 
} 
//<applet code=ballz.class height=480 width=860></applet>
