java画图中运用缓冲解决屏幕闪烁方案二
原创
©著作权归作者所有:来自51CTO博客作者zhy000的原创作品,请联系作者获取转载授权,否则将追究法律责任
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 p_w_picpath;
//声明作为缓冲区的可变图像对象
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
{
//获得图像文件
p_w_picpath = 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(p_w_picpath,getWidth()/2,getHeight()/2,graphics.HCENTER|graphics.VCENTER);
//释放缓冲区的Graphics对象
graphics=null;
}
protected void paint(Graphics g)
{
//将缓冲区的内容绘制到屏幕上
g.drawImage(buffer,getWidth()/2,getHeight()/2,g.HCENTER|g.VCENTER);
}
public void commandAction(Command command, Displayable displayable)
{
if (command == exit)
{
bufferDrawExample.exitMIDlet();
}
}
}
提问和评论都可以,用心的回复会被更多人看到
评论
发布评论
相关文章
-
java项目技术方案
Java项目技术方案书写教程,一目了然。
API Java java -
解决Frame闪烁问题(双缓冲)
与paint方法同级,加上这段代码就OK了private Image offScreenImage = null; public void update(G
Java Frame 闪烁 双缓冲 Image -
C#画图解决闪烁问题
导致画面闪烁的关键原因分析: 一、绘制窗口由于大小
双缓冲 控件 重绘 -
笔记本屏幕闪烁怎么解决(花屏)
大多数的屏幕闪烁问题是由于笔记本的屏幕的线缆接触不良导
处理方法 解决方法 杀毒软件