January 17, 2004

使用Image.getGraphics需要注意!

Categories:  ItTech

为了解决昨天Image.createImage的问题,在网上狂搜索了一通,没有找到解决方法,倒找到了另外一个相关的注意事项:

在新版本的Nokia Series 60的机器上,Image.getGraphics的实现和以前不同。在老版本中,对一个Image实例多次调用getGraphics方法返回的是同一个Graphics实例;而在新版本中则每次返回不同的实例。因此,如果使用下面的代码:

class Test {
    private Image img; 
    ...
    public void draw() {
        Graphics g = img.getGraphics();
        ... // 使用g进行绘制
    }
};

如果多次调用Test.draw(),则会出现Memory Full错误,最后退出。正确的方法应该是:

class Test {
    private Image img;
    private Graphics g;
    ...
    public void init() {
        g = img.getGraphics();
    }
    public void draw() {
        ... // 使用g进行绘制
    }
};

在调用draw前,调用一次init,然后再多次调用draw就没有什么问题了。

Posted by dumm at January 17, 2004 06:01 PM
Comments

我也遇到相同的问题,s60上出现内存已满,而模拟器上垃圾收集一切正常,你说的这点注意事项我以前没听说过,现在马上去试试看。另外请问消息来源是那里,能给个地址吗?谢谢.
另外有j2me方面的讨论可以发我邮箱,交个朋友

Posted by: danieljill at June 11, 2005 04:25 PM

原始的地址早忘记了,呵呵。

Posted by: dumm at June 13, 2005 01:19 AM
Post a comment









Remember personal info?




  
Please enter the code as seen in the image above to post your comment.