java线程中runnable

Shine_180418 2017-06-18 02:43:56
按照书上的代码,编写这个程序,但是运行之后显示 启动未初始化的小应用程序
package pack8_3;

import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;

public class example8_3 extends Applet implements Runnable{
Thread redBall,blueBall;
Graphics redPen,bluePen;
int blueSeta=0,redSeta=0;
public void init(){
setSize(250,200);
redBall=new Thread(this);
blueBall=new Thread(this);
redPen=getGraphics();
bluePen=getGraphics();
redPen.setColor(Color.red);
bluePen.setColor(Color.blue);
setBackground(Color.gray);
}
public void start(){
redBall.start();
blueBall.start();
}
public void run(){
int x,y;
while(true){
if(Thread.currentThread()==redBall){
x=(int)(80.0*Math.cos(3.1415926/180.0*redSeta));
y=(int)(80.0*Math.sin(3.1415926/180.0*redSeta));
redPen.setColor(Color.gray);
redPen.fillOval(100+x,100+y,10,10);
redSeta+=3;
if(redSeta>=360)
redSeta=0;
x=(int)(80.0*Math.cos(3.1415926/180.0*redSeta));
y=(int)(80.0*Math.sin(3.1415926/180.0*redSeta));
redPen.setColor(Color.red);
redPen.fillOval(100+x,100+y,10,10);
try{
redBall.sleep(20);
}
catch(InterruptedException e){}
}
else if(Thread.currentThread()==blueBall){
x=(int)(80.0*Math.cos(3.1415926/180.0*blueSeta));
y=(int)(80.0*Math.sin(3.1415926/180.0*blueSeta));
redPen.setColor(Color.gray);
redPen.fillOval(100+x,100+y,10,10);
blueSeta-=3;
if(blueSeta<=360)
blueSeta=0;
x=(int)(80.0*Math.cos(3.1415926/180.0*blueSeta));
y=(int)(80.0*Math.sin(3.1415926/180.0*blueSeta));
redPen.setColor(Color.blue);
redPen.fillOval(100+x,100+y,10,10);
try{
redBall.sleep(40);
}
catch(InterruptedException e){}
}
}
}
}

如图所示:

运行结果:

求高手指点?这是为什么?
...全文
170 回复 打赏 收藏 转发到动态 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

58,454

社区成员

发帖
与我相关
我的任务
社区描述
Java Eclipse
社区管理员
  • Eclipse
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧