java画图中运用缓冲解决屏幕闪烁方案二
来源:技术人生 责任编辑:栏目编辑 发表时间:2013-07-01 22:05 点击:次
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
public class BufferDrawExample extends MIDlet
{
private Display display;
private MyCanvas canvas;
public BufferDrawExample()
{
//获取MIDlet的Display对象实例
display = Display.getDisplay(this);
//声明Canvas屏幕对象
canvas = new MyCanvas (this);
}
protected void startApp()
{
//显示屏幕对象
display.setCurrent(canvas);
}
protected void pauseApp()
{
}
protected void destroyApp( boolean unconditional )
{
}
public void exitMIDlet()
{
destroyApp(true);
notifyDestroyed();
}
}
class MyCanvas extends Canvas implements CommandListener
{
private Command exit;
//声明图片对象
private Image image;
//声明作为缓冲区的可变图像对象
private Image buffer;
private BufferDrawExample bufferDrawExample;
public MyCanvas (BufferDrawExample bufferDrawExample)
{
this.bufferDrawExample = bufferDrawExample;
exit = new Command("Exit", Command.EXIT, 1);
addCommand(exit);
setCommandListener(this);
try
{
//获得图像文件
image = Image.createImage("/12.png");
}
catch (Exception e)
{
System.out.println(e);
}
//创建可变图像
buffer=Image.createImage(getWidth(),getHeight());
//获取缓冲区的Graphics对象
Graphics graphics=buffer.getGraphics();
//设置颜色
graphics.setColor(255,255,255);
//填充屏幕
graphics.fillRect(0, 0, getWidth(), getHeight());
//向缓冲区内绘制图像
graphics.drawImage(image,getWidth()/2,getHeight()/2,graphics.HCENTER|graphics.VCENTER);
//释放缓冲区的Graphics对象
graphics=null;
}
 
相关新闻>>
- 发表评论
-
- 最新评论 更多>>