How paint sprites in MIDP2 ?

Hi, I’m really stuck here and any help is much appreciated! With the recently released MIDP2 I’ve created a layerManager and appended a sprite to it. But I can’t manage to display the sprite. This is the code:


..
Image theImage;
Sprite theSprite;..

void someMethod() {
      try {
               LayerManager lm = new LayerManager();
               theImage = Image.createImage("/images/someImage.png");
               theSprite = new Sprite(theImage, (theImage.getWidth()/2), (theImage.getHeight()/2));
               lm.append( theSprite );               lm.setViewWindow( 0, 0, 96, 100 );
               theSprite.paint (graphicObject);
      } catch (java.io.IOException ex) {
      }
}

In this code-snippet, what should the graphicObject be onto which the sprite is to be painted? Any examples?

I also tried creating a public class “TheSprite” extending Sprite and defining a method paint() in it. But the paint-method of Sprite cannot be overrided and a constructor cannot be resolved.

Please help!

Without having any idea of the whole MIDP topic…

You are sure not to run into the IOException part? I’m sure you are, but I’ve seen so much in my life…

First point: the LayerManager is doing nothing for you in this example: you could remove it. If you plan to use it for something (e.g. appending some more Sprites and/or TiledLayers) then instead of calling theSprite.paint you should call lm.paint - it will call paint on your Sprites and TiledLayers in the right order for you.

You need to use either a Canvas or a GameCanvas as the screen you draw your Sprite on. If you’re using a Canvas, the system will call your Canvas’s ‘paint’ method each time it’s needed, and will pass in a Graphics object. That’s the object you pass to mySprite.paint. It’s only valid for the duration of that call to your Canvas’s paint method, so don’t go storing it and using it later.

If you’re using a GameCanvas, you can just call its method ‘getGraphics’, and pass the returned object to mySprite.paint. Look at the code example in the JavaDocs for GameCanvas.

Important note: I assume you’re just experimenting here, but in your real MIDlet you want to create the Image object in your Canvas/GameCanvas’s constructor, not each time you paint.

I’d better confess that I haven’t yet had time to play with MIDP 2.0, but I’ve been using MIDP 1.0 for a couple of years. The notes above come from my reading of the MIDP 2.0 API docs.

Thank you for your explanation!

Thruth is I’ve just started using MIDP a few weeks ago. And I’m not much of an experienced java-programmer.

The JavaDocs say “protected GameCanvas(boolean suppressKeyEvents) Creates a new instance of a GameCanvas. A new buffer is also created for the GameCanvas and is initially filled with white pixels.”

But I’ve tried to use a GameCanvas by defining public class MyCanvas extends GameCanvas { … } and doing MyCanvas myCanvas = new MyCanvas(false); or just by doing GameCanvas myCanvas = new GameCanvas(false); . All of this didn’t work, probable reasen is that GameCanvas is an abstract class and cannot be instantiated, or am I wrong here? Then how can I invoke GameCanvas’ constructor as told in the JavaDocs?! ???

So I figured I can use GameCanvas by importing …lcdui.game.* in my class MyCanvas which extends Canvas. And then just invoking GameCanvas’ methods. Plz correct me if I’m wrong (I’m still new to java).

Unfortunately the game examples that came with the MIDP2 package are written in MIDP1.
More or less, I’m desperately looking for a plain code-sample which reflects the basics of creating a game with MIDP2 (i.e. creating a MIDlet, creating a GameCanvas, LayerManager, Layers and Sprites and displaying the sprites in a viewwindow by the LayerManager). The ones I found don’t seem to work, or consist of small code-snippets without keeping track of the bigger picture.

HMM… as a java-rookie I’m really lost here aint I? :-/

Hmm… didn’t do my homework well i guess, just discovered that the pushpuzzle example IS actually created with MIDP2 using all of its fancy features. Thnx tho.