对于线程的一些理解
2009-02-05 09:00
375 查看
#isubb#package untitled1;
import javax.microedition.lcdui.*;
public class tetriscanvasdemo extends canvas implements runnable{
int drawx,drawy;
image source;
image copy;
tetriscanvasdemo()
{
drawx=this.getwidth()/2;
drawy=0;
copy=image.createimage(this.getwidth(),this.getheight());
}
protected void paint(graphics g)
{
graphics offscreen=copy.getgraphics();
offscreen.setcolor(255,255,255);
offscreen.fillrect(0,0,this.getwidth(),this.getheight());
offscreen.setcolor(0,0,0);
offscreen.fillrect(drawx,drawy,10,10);
g.drawimage(copy,0,0,graphics.left |graphics.top);
}
public void run()
{
while(true)
{
this.down();
repaint();
try
{
thread.sleep(50);;
}
catch(interruptedexception e)
{
system.out.println("sleep error:"+e.getmessage());
}
}
}
public void start()
{
thread t=new thread(this);
t.start();
}
protected void down()
{
if(drawy {
drawy=drawy+1;
}
else
{
drawy=0;
}
}
}
在这个类中,如果没有implemnt runable那就是一般的canvas界面,执行完构造函数再paint函数显示就完了。加了runable,在start里,[color=#ff0000]thread t=new thread(this)就将此canvas定义为一个线程了,[/color]t.start()启动线程,在run里就可以写这个线程中做些什么了。
import javax.microedition.lcdui.*;
public class tetriscanvasdemo extends canvas implements runnable{
int drawx,drawy;
image source;
image copy;
tetriscanvasdemo()
{
drawx=this.getwidth()/2;
drawy=0;
copy=image.createimage(this.getwidth(),this.getheight());
}
protected void paint(graphics g)
{
graphics offscreen=copy.getgraphics();
offscreen.setcolor(255,255,255);
offscreen.fillrect(0,0,this.getwidth(),this.getheight());
offscreen.setcolor(0,0,0);
offscreen.fillrect(drawx,drawy,10,10);
g.drawimage(copy,0,0,graphics.left |graphics.top);
}
public void run()
{
while(true)
{
this.down();
repaint();
try
{
thread.sleep(50);;
}
catch(interruptedexception e)
{
system.out.println("sleep error:"+e.getmessage());
}
}
}
public void start()
{
thread t=new thread(this);
t.start();
}
protected void down()
{
if(drawy {
drawy=drawy+1;
}
else
{
drawy=0;
}
}
}
在这个类中,如果没有implemnt runable那就是一般的canvas界面,执行完构造函数再paint函数显示就完了。加了runable,在start里,[color=#ff0000]thread t=new thread(this)就将此canvas定义为一个线程了,[/color]t.start()启动线程,在run里就可以写这个线程中做些什么了。
相关文章推荐
- 对于java线程的一些理解
- IOS对于线程队列的一些理解
- iOS 视图在不同View之间的切换(对于convertRect:函数的一些理解)
- 对于产品中“赞”的一些理解
- 对于MVC应该理解的一些问题
- 对于在一个线程中调用一个新窗口的理解
- 对于RSA算法的一些理解
- 对于nginx $request_time的一些理解
- 搞清楚局部和全部,搞清楚逻辑和物理,搞清楚这些对于理解一些组件的概念是非常有意义的:
- 线程调用中的一些理解
- 对于interrupt,interrupted 和 isInterrupted的一些理解
- 对于AOP切面编程的一些个人理解
- [NN] 对于BackPropagation(BP, 误差反向传播)的一些理解
- 讲一些对于页面布局以及html的理解
- 个人对于OO的一些理解
- 对于Promise的一些理解
- 对于linux文件结构的一些理解与总结
- 关于线程和父子进程的一些个人理解
- 对于json多维解析的一些理解
- 对于String类中的"abc"与new String("abc")的一些理解