都市

线程控制动画呢

线程是程序的一部分,是系统调度的基本单位。线程是控制动画的理想选择。将动画的工作放程上,可以释放出程序的其他部分来处理别的任务。

线程的现实是通过ng中的Thread类,要使某一个类能使用线程,必须实现Runnable接口,该接口包含了唯一一个方法run()。run()方法是线程类的核心,--------动画程序中产生运动。通过调用线程的start ()方法,致使run()方法被调用。下边这个程序描绘了一个运动中的圆。

import t.*;

import ent.*;

import ing.*;

public class Cartoon extends JApplet implements Runnable

{

Graphics screenBuffer = null;//创建图形缓冲区

Image screenImage = null;

private Thread runner;

private int x = 5;

private int move = 1;

public void init ( )

{

screenImage = createImage ( 230, 160 );

screenBuffer = tGraphics ( );

}

public void start ( )

{

if (runner == null)

{

runner = new Thread( this );

art();

}

}

public void run( )

并缓解逾50年来最严重的干旱。 {

Thread circle = rrentThread ( );

while ( runner == circle )//指向同一对象,便开始运行

{

x += move;

if ( ( x 105 ) || ( x 5 ))

move *= -1;

repaint ( );

}

}

public void drawCircle( Graphics gc )

{

Graphics2D g2D = ( Graphics2D ) gc;

tColor ( ue );

llRect ( 0, 0, 100, 100 );

tColor ( llow );

llRect ( 100, 0, 100, 100 );

tColor ( d );

llOval ( x, 5, 90, 90 );

}

public void paint( Graphics g )

{

tColor ( ite );

llRect (0,0,96,60);

drawCircle ( screenBuffer );

//将缓冲区的图像复制到主缓冲区中

awImage ( screenImage, 130, 100, this );

}

}

查看本文来源

薏芽健脾凝胶
柳州白癜风医院哪家医院好
天津男科医院哪家好
友情链接