import java.awt.*;
import java.applet.*;
import javax.swing.*;
public class TiaoDong extends Applet implements Runnable{
static int a=5,b=2; //a是小球上下移动的速度,b是小球左右移动的速度.
static int x=10,y=10; //x,y是绘制小球的初始坐标.
static Thread xianchen;
static JFrame jf;
public static void main(String args[]){
TiaoDong jj=new TiaoDong();
jf=new JFrame("跳动的小球");
jf.setLocation(300,300);
jf.setSize(400,350);
jf.getContentPane().add(jj);
xianchen=new Thread(jj);
xianchen.start();
jf.show();
}
public void paint(Graphics g){
g.setColor(new Color(255,0,0));
g.fillOval(x,y,50,50);
}
public void run(){
while(true){
if (y<=5 || y>=jf.getHeight()-80)
a=-a;
if (x<=5 || x>=jf.getWidth()-55)
b=-b;
x=x+b;y=y+a;
repaint();
try{
xianchen.sleep(100);
}catch(Exception e) {}
}
}
}