j2me双缓冲经典分析

public class ImageCanvas extends Canvas
{

    private Image buffer;//可变图像,作为绘制缓冲
    private Image image;//不变图像,用来加载图片文件

    public ImageCanvas()
    {

        try
        {

            image = Image.createImage( "/monitor.png" ); //加载图片文件

        } catch( java.io.IOException e )
        {

            System.out.println( e.getMessage() );      //处理I/O异常

        }

        buffer = Image.createImage( this.getWidth(), this.getHeight() );

        //用一个可变图像作为绘制缓冲

        Graphics bg = buffer.getGraphics();        //获取缓冲的Graphics对象

        bg.setColor( 0xFFFFFF );

        bg.fillRect( 0, 0, getWidth(), getHeight() );    //填充整个屏幕

        bg.drawImage( image, this.getWidth() / 2, this.getHeight() / 2, Graphics.VCENTER | Graphics.HCENTER );

    }

    public void paint( Graphics g )
    {

        g.drawImage( buffer, 0, 0, g.TOP | g.LEFT );      //将缓冲区上的内容绘制到屏幕上

    }
}

以上是网上的例子,有一点不明白的就是这里:
bg.drawImage( image, this.getWidth() / 2, this.getHeight() / 2, Graphics.VCENTER | Graphics.HCENTER );

g.drawImage( buffer, 0, 0, g.TOP | g.LEFT );
为什么同一幅图片,两次画的位置都不同?  Graphics.VCENTER | Graphics.HCENTER  和 g.TOP | g.LEFT 
双缓冲不是指两个缓冲层...只是说有一个在内存中存在的,供操作的缓冲层,等到操作结束后,复制到屏幕上,一个Image对象已经足够了
Java代码 复制代码
  1. 在学习j2me的过程中,可能很多学员对屏幕缓冲理解方面有很多的误解,其实,屏幕缓冲简单的可以说成:就是使用自己创建的屏幕画笔在创建画笔的屏幕上作画,然后再将画出的屏幕做为Image的对象画到改屏幕上,这样就解决了屏幕闪烁的问题:   
  2.   
  3. 屏幕缓冲的代码贴上以供大家理解:  
  在学习j2me的过程中,可能很多学员对屏幕缓冲理解方面有很多的误解,其实,屏幕缓冲简单的可以说成:就是使用自己创建的屏幕画笔在创建画笔的屏幕上作画,然后再将画出的屏幕做为Image的对象画到改屏幕上,这样就解决了屏幕闪烁的问题:

现将屏幕缓冲的代码贴上以供大家理解:

 

Java代码 复制代码
  1.   
  2.   
  3. package com.redarmy.tes;   
  4.   
  5. import javax.microedition.lcdui.Canvas;   
  6. import javax.microedition.lcdui.Graphics;   
  7. import javax.microedition.lcdui.Image;   
  8.   
  9. public class ExCanvas extends Canvas{   
  10.     public Image offScreen;//离屏缓冲区   
  11.     public Graphics offg;  //离屏画笔   
  12.        
  13.     public ExCanvas() {   
  14.         //绘制和屏幕一样大小的缓冲区   
  15.         offScreen = Image.createImage(this.getWidth(), this.getHeight());   
  16.         //得到离屏缓冲区的画笔   
  17.         offg = offScreen.getGraphics();        
  18.     }   
  19.   
  20.     protected void paint(Graphics arg0) {   
  21.         //绘制离屏缓冲区   
  22.         arg0.drawImage(offScreen, 00, Graphics.LEFT|Graphics.TOP);   
  23.     }   
  24.   
  25. }   
  26. //以上内容均有本人亲自撰写,不要随意转载。如有问题请发送邮件到;RedArmy.Chen@gmail.com  

 

 

 

http://developers.sun.com/mobility/midp/articles/gameapi/(这是原文参考)

 MIDP applications often use double-buffering to make animation smoother. In this approach, you do not draw directly to the display, but instead to a copy of the display, an off-screen buffer that's maintained in memory. When you are done drawing to the buffer, you then copy its contents to the display. The rationale here is that drawing using primitives takes longer than a single memory-copy operation; the same drawing operations are performed, but in the background. The user sees only the net change in the display each time, so the animation flows more smoothly. To implement double-buffering, first create a mutable image the same size as the screen: ...
int width = getWidth();
int height = getHeight();
Image buffer = Image.createImage(width, height);
...

 

Then obtain a graphics context for the buffer: ...
Graphics g = buffer.getGraphics();
...

 

Now draw to the buffer: ...
// animate
// ...
g.drawRexr(20, 20, 25, 30);
...

 

When you need to copy the buffer to the screen, you can override the paint() method to draw the buffer to the device's display: public void paint(Graphics g)
   g.drawImage(buffer, 0, 0, 0);
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值