×

Discussion Board

Results 1 to 3 of 3
  1. #1
    Registered User
    Join Date
    Oct 2003
    Posts
    2

    Canvas doesn't show when getWidth/getHeight is used

    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

  2. #2
    Regular Contributor
    Join Date
    Mar 2003
    Posts
    87

    getWidth() and getHeight() used at the wrong time

    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.

  3. #3
    Super Contributor
    Join Date
    Mar 2003
    Location
    Israel
    Posts
    2,280
    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

Posting Permissions

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