You Are Here:

Community: Developer Discussion Boards

#1 Old Canvas doesn't show when getWidth/getHeight is used - 2003-10-24, 22:31

Join Date: Oct 2003
Posts: 2
claudio4j
Offline
Registered User
Hi, I started to develop some games, and my first deployment on a real device (Nokia 3650/firmware: 2.54) doesn't works.
I cut and paste the "Your First Micro Java Game (http://www.onjava.com/pub/a/onjava/2.../javagame.html)", tested on latest emulators (Nokia NDS and J2ME WTK 2.0), works well. When transfered to 3650, launch the MIDlet, a blank screen is showed and got back to Applications folder. An interesting thing is if I change the getWidth/getHeight in SpaceCanvas by 176 and 144 (canvas size) all works well. I don't think is a Canvas bug, but this problem are taking hard for me to solve it.
The code is really simple but this problem is shaking my head. I think JAD, manifest and .java files are fine. Below is the code:

package claudius;

import javax.microedition.lcdui.*;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;

public class SpaceGame extends MIDlet implements CommandListener {

// The main display elements
private Display theDisplay;
private SpaceCanvas canvas;

// The exit command, so we can end the game
static final Command exitCommand = new Command("Exit", Command.STOP, 1);

public SpaceGame() {
// Create the main Display
theDisplay = Display.getDisplay(this);
}

class SpaceCanvas extends Canvas {
private Sprite shipSprite;
private int width;
private int height;

SpaceCanvas() {
//width = getWidth();
//height = getHeight();
width = 176;
height = 144;
// Load the graphics
Image spaceship = null;
try {
spaceship = Image.createImage("/spaceship.png");
shipSprite = new Sprite(spaceship);
// Put the sprite at (0,50)
shipSprite.setX(0);
shipSprite.setY(50);
} catch (Exception ioe) {
System.err.println("Problem loading image " + ioe);
}
}

public void paint(Graphics g) {

// Paint a white background
g.setColor(255, 255, 255);
g.fillRect(0, 0, width, height);
g.setColor(255, 255, 255);
// Paint the sprite
shipSprite.paint(g);
}

public void keyPressed(int keyCode) {
int key = getGameAction(keyCode);
// Move the sprite left or right.
if (key == LEFT) {
int newx = shipSprite.getX() - 5;
if (newx < 0)
newx = 0;
shipSprite.setX(newx);
} else if (key == RIGHT) {
int newx = shipSprite.getX() + 5;
if (newx > (width - 10))
newx = (width - 10);
shipSprite.setX(newx);
}
// Repaint the canvas!
repaint();
}
}

protected void startApp() throws MIDletStateChangeException {
canvas = new SpaceCanvas();

// Add the exit command
canvas.addCommand(exitCommand);
canvas.setCommandListener(this);

theDisplay.setCurrent(canvas);
}

protected void pauseApp() {
}

public void destroyApp(boolean unconditional) {
}

// Handle events
public void commandAction(Command c, Displayable d) {
if (c == exitCommand) {
destroyApp(false);
notifyDestroyed();
}
}
}

////////

Sprite paint:

public void paint(Graphics g) {
g.drawImage(image, x, y, Graphics.TOP | Graphics.LEFT);
}



If you have any tip, I will apreciate.

Thanks

Claudio
Reply With Quote

#2 Old getWidth() and getHeight() used at the wrong time - 2003-11-05, 01:14

Join Date: Mar 2003
Posts: 87
dmford
Offline
Regular Contributor
I think the clue was when you said that you replaced the getHeight() and getWidth() methods with constants and it worked.

Those methods can't be used in the constructor cos the Canvas hasn't been initialised yeat, you'll find the same problem with Applets and Frames in JavaSE.

A solution would be to set them outside the canvas class.

SpaceCanvas myCanvas = new SpaceCanvas();
myCanvas.width = myCanvas.getWidth();
myCanvas.height= myCanvas.getHeight();

or have a method in SpaceCanvas that sets them afterwards.

Hope that helps.
Reply With Quote

#3 Old 2003-11-05, 09:41

Join Date: Mar 2003
Posts: 2,280
Location: Israel
shmoove
Offline
Forum Nokia Champion
I see you are using an MIDP 2.0 development environment. I have had problems with getWidth() when compiling apps with the WTK 2.0 and trying to run them on a MIDP 1.0 phone. Try compiling the application with WTK1.0.4 or one of the Nokia SDKs.
(As far as I can remember there shouldn't be a problem using those methods in the contructor).

shmoove
Reply With Quote
Reply « Previous Thread | Next Thread »
Display Modes
Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules

You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Forum Jump

Rate This

 
Bookmark this page: DeliciousDiggFacebookGoogleYahooStumbleUponRedditDiigoTechnocratiTwitter  Share this page Share this page Print this Page Print this page Invite a friend Invite a friend
京ICP备05048969号    Email Newsletters Press Terms & Conditions Privacy Policy Sitemap Contact Us © 2009 Nokia 
RDF Facets: qdcZidentifierQSxhttpE3aE2fE2fdiscussionE2eforumE2enokiaE2ecomhttpE3aE2fE2fdiscussionE2eforumE2enokiaE2ecomE2fforumE2fshowthreadE2ephpE3ftE3d18645X qdcZtypeQUqfnZE45E78cludedFromGeneralE4cistingsQ qdcZtypeQUqfntypeZCommunityContentQ qdcZtypeQUqfntypeZE44iscussionQ qdcZtypeQUqfntypeZE44iscussionContentQ qdcZtypeQUqfntypeZE52esourceQ qdcZtypeQUqfntypeZWebpageQ qdcZtypeQUqmarsZManagedE52esourceQ qdcZtypeQUqwebZInformationE52esourceQ qdcZtypeQUqwebZPageQ qdcZtypeQUqwebZE52esourceQ qdcZtypeQUqrdfsZE52esourceQ qfnZtopicQUqfnTopicZentertainmentQ qfnZtopicQUqfnTopicZj2meQ qfnZtopicQUqfnTopicZjavaQ qfnZtopicQUqfnTopicZmediaQ qfnZtypeQUqfntypeZCommunityContentQ qfnZtypeQUqfntypeZE44iscussionQ qfnZtypeQUqfntypeZE44iscussionContentQ qfnZtypeQUqfntypeZE52esourceQ qfnZtypeQUqfntypeZWebpageQ qmarsZlanguageQUxhttpE3aE2fE2fswE2enokiaE2ecomE2flanguageE2d1E2fenX qrdfZtypeQUqfnZE45E78cludedFromGeneralE4cistingsQ qrdfZtypeQUqfntypeZCommunityContentQ qrdfZtypeQUqfntypeZE44iscussionQ qrdfZtypeQUqfntypeZE44iscussionContentQ qrdfZtypeQUqfntypeZE52esourceQ qrdfZtypeQUqfntypeZWebpageQ qrdfZtypeQUqmarsZManagedE52esourceQ qrdfZtypeQUqwebZInformationE52esourceQ qrdfZtypeQUqwebZPageQ qrdfZtypeQUqwebZE52esourceQ qrdfZtypeQUqrdfsZE52esourceQ