Indish
Indish

Reputation: 749

How to save canvas as an image in j2me?

I want to save the current state of the canvas as an image as I will use it in the next pointerevent. If I use repaint it will clear the canvas and I can't get the previous state of canvas. So, I want to save it as image and then iterate it over and over so that finally I can make out what I want. The final question is how to save the canvas as image ?. Or Is there any possibility of converting the Graphics object to a byte array ?

Upvotes: 0

Views: 360

Answers (2)

Telmo Pimentel Mota
Telmo Pimentel Mota

Reputation: 4043

Create an Image with same size (width and height) of the screen. When you want to save the canvas state call Canvas.paint passing the image 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());
  }
}

Upvotes: 1

mr_lou
mr_lou

Reputation: 1920

You can't save the canvas as an image.

You have to create an image first, and then you can paint onto that image.

Basically that means that your midlet will be doing more work, since you first have to draw onto your image, and then you have to draw that image onto the canvas. But it's the only way you can do what you want.

Upvotes: 1

Related Questions