2013-04-20 68 views
0

我想将画布的当前状态保存为图像,因为我将在下一个pointerevent中使用它。如果我使用重绘,它将清除画布,并且我无法获取画布的以前状态。所以,我想将它保存为图像,然后反复迭代,以便最终我可以确定我想要的。 最后一个问题是如何将画布保存为图像? 或者是否有任何将图形对象转换为字节数组的可能性?如何在j2me中将画布另存为图像?

回答

1

您无法将画布另存为图像。

您必须首先创建图像,然后才可以在该图像上绘制图像。

基本上,这意味着您的midlet将会做更多的工作,因为您首先必须绘制到图像上,然后必须将该图像绘制到画布上。但这是你能做到你想做的唯一方法。

1

创建一个具有相同大小(宽度和高度)的屏幕Image。当你想保存画布状态调用Canvas.paint传递图像Graphics

class MyCanvas extends Canvas { 
    private Image lastScreen; 

    protected void sizeChanged(int w, int h) { 
    if (lastScreen == null || w != lastScreen.getWidth() 
     || h != lastScreen.getHeight) { 
     lastScreen = Image.createImage(w, h); 
    } 
    } 

    protected void paint(Graphics g) { 
    // paint the whole screen 
    } 

    protected void pointerReleased(int x, int y) { 
    paint(lastScreen.getGraphics()); 
    } 
} 
相关问题