Hi !
Featured games (90)
games approved by the League of Dukes
Games in Showcase (780)
Games in Android Showcase (233)
games submitted by our members
Games in WIP (856)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  How paint sprites in MIDP2 ?  (Read 4492 times)
0 Members and 1 Guest are viewing this topic.
Offline DamnGzM

Junior Newbie

Java games rock!

« Posted 2002-12-04 12:33:00 »

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 ( 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!
Offline Herkules

Senior Devvie

Friendly fire isn't friendly!

« Reply #1 - Posted 2002-12-04 12:37:44 »

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...

HARDCODE    --     DRTS/FlyingGuns/JPilot/JXInput  --    skype me: joerg.plewe
Offline davidaprice

Junior Devvie

« Reply #2 - Posted 2002-12-04 13:48:39 »

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.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline DamnGzM

Junior Newbie

Java games rock!

« Reply #3 - Posted 2002-12-04 15:53:50 »

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?!  Huh

So I figured I can use GameCanvas by importing* 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?  :-/

Offline DamnGzM

Junior Newbie

Java games rock!

« Reply #4 - Posted 2002-12-04 17:35:55 »

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.
Pages: [1]
  ignore  |  Print  

hadezbladez (703 views)
2018-11-16 13:46:03

hadezbladez (347 views)
2018-11-16 13:41:33

hadezbladez (688 views)
2018-11-16 13:35:35

hadezbladez (171 views)
2018-11-16 13:32:03

EgonOlsen (2362 views)
2018-06-10 19:43:48

EgonOlsen (2474 views)
2018-06-10 19:43:44

EgonOlsen (1463 views)
2018-06-10 19:43:20

DesertCoockie (2128 views)
2018-05-13 18:23:11

nelsongames (1904 views)
2018-04-24 18:15:36

nelsongames (2572 views)
2018-04-24 18:14:32
Deployment and Packaging
by mudlee
2018-08-22 18:09:50

Java Gaming Resources
by gouessej
2018-08-22 08:19:41

Deployment and Packaging
by gouessej
2018-08-22 08:04:08

Deployment and Packaging
by gouessej
2018-08-22 08:03:45

Deployment and Packaging
by philfrei
2018-08-20 02:33:38

Deployment and Packaging
by philfrei
2018-08-20 02:29:55

Deployment and Packaging
by philfrei
2018-08-19 23:56:20

Deployment and Packaging
by philfrei
2018-08-19 23:54:46 is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!