ボール遊び

import java.awt.*;
import java.awt.event.*;

class MyFrame extends Frame
	implements Runnable{// t
	int w=600, h=600;
	int sleepTime=10;// t
	Thread thread=null;// t
	Ball[] ball=new Ball[200];
	
	MyFrame(String s){
		super(s);
		setSize(w, h);
		setBackground(Color.GRAY);
		setForeground(Color.WHITE);
		for(int i=0; i < ball.length; i=i+1){
			int x=(int)(Math.random()*(double)w);
			int y=(int)(Math.random()*(double)h);
			int r=(int)(Math.random()*255.0);
			int g=(int)(Math.random()*255.0);
			int b=(int)(Math.random()*255.0);
			int vx=(int)(0.02*(Math.random()-0.5)*(double)w);// t
			int vy=(int)(0.02*(Math.random()-0.5)*(double)h);// t
			ball[i]=new Ball(x, y, 5, new Color(r, g, b));
			ball[i].setVelocity(vx, vy);
			ball[i].setMyFrame(this);
		}
	}
	public void paint(Graphics g){
		for(int i=0; i < ball.length; i=i+1){
			ball[i].paint(g);
		}
	}
	public void start(){// t
		if(thread==null){
			thread=new Thread(this);
			thread.start();
		}
	}
	public void stop(){// t
		thread=null;
	}
	public void run(){// t
		while(Thread.currentThread()==thread){
			for(int i=0; i < ball.length; i=i+1){
				ball[i].move();
			}
			repaint();
			try{
				Thread.sleep(sleepTime);
			}catch(InterruptedException e){
			}
		}
	}
	public boolean checkLimit(int x, int y){// c
		return((x < 0)||(w < x)||(y < 0)||(h < y));
	}
}//end of class MyFrame
class Ball{
	MyFrame myFrame;// c
	int x, y, r;
	int vx, vy;// t
	Color c;
	
	public Ball(int x, int y, int r, Color c){
		this.x=x;
		this.y=y;
		this.r=r;
		this.c=c;
	}
	public void paint(Graphics g){
		Color gc=g.getColor();
		g.setColor(c);
		g.fillOval(x-r, y-r, 2*r, 2*r);
		g.setColor(gc);
	}
	public void setVelocity(int vx, int vy){//t
		this.vx=vx;
		this.vy=vy;
	}
	public void setMyFrame(MyFrame myFrame){// c
		this.myFrame=myFrame;
	}
	public void move(){
		x=x+vx;
		if(myFrame.checkLimit(x,y)){// c
			vx=-vx;
			x=x+vx;
		}
		y=y+vy;
		if(myFrame.checkLimit(x, y)){// c
			vy=-vy;
			y=y+vy;
		}
	}
}
public class ballplay{
	public static void main(String[] args){
		MyFrame myFrame=new MyFrame("BallTest");
		myFrame.addWindowListener(
				new WindowAdapter(){
					public void windowClosing(WindowEvent e){
						System.exit(0);
					}
				});
		myFrame.setVisible(true);
		myFrame.start();// t
	}
}// end of class BallTest