import java.awt.*;
public class Rebound{
public static void main(String args[]) {
Frame w=new Frame();
w.setSize(800, 800);
MyRebound p=new MyRebound();
w.add(p);
My b=new My();
w.add(b);
Thread t1=new Thread(p);
Thread t2=new Thread(b);
t1.start();
t2.start();
w.show();
}
}
class MyRebound extends Panel implements Runnable{
int x=(int)(Math.random()*800);
int y=(int)(Math.random()*800);
int att=0;
public void paint(Graphics g) {
g.fillOval(x, y, 50, 50);
}
public void run(){
while(true) {
//定义飞行姿态
if(att==0) {
x++;
y++;
}
if(att==1) {
x--;
y++;
}
if(att==2) {
x--;
y--;
}
if(att==3) {
x++;
y--;
}
//改变飞行姿态
if(x>730) {
if (att==0) {
att=1;
}else {
att=2;
}
}
if(y>700) {
if(att==1) {
att=2;
}else {
att=3;
}
}
if(x<0) {
if(att==2) {
att=3;
}else {
att=0;
}
}
if(y<0) {
if(att==3) {
att=0;
}else {
att=1;
}
}
try {
Thread.sleep(5); //线程休眠5毫秒
}catch(Exception e) {} //异常处理
repaint();
}
}
}
//.........................................................................................
class My extends Panel implements Runnable{
int i=(int)(Math.random()*800);
int j=(int)(Math.random()*800);
int att=0;
public void paint(Graphics g) {
g.setColor(Color.yellow);
g.drawOval(i ,j, 50, 50);
}
public void run(){
while(true) {
//定义飞行姿态
if(att==0) {
i++;
j++;
}
if(att==1) {
i--;
j++;
}
if(att==2) {
i--;
j--;
}
if(att==3) {
i++;
j--;
}
//改变飞行姿态
if(i>730) {
if (att
==0) {
att=1;
}else {
att=2;
}
}
if(j>700) {
if(att==1) {
att=2;
}else {
att=3;
}
}
if(i<0) {
if(att==2) {
att=3;
}else {
att=0;
}
}
if(j<0) {
if(att==3) {
att=0;
}else {
att=1;
}
}
try {
Thread.sleep(5); //线程休眠5毫秒
}catch(Exception e) {} //异常处理
repaint();
}
}
}
牙牙学语1
qq_快乐
qq_快乐
相关分类