Question

I am Creating a J2ME game and i've noticed strange behavior of getHeight() function.

Instead of Giving the Full Screen's Height in Pixels,it seems to be returning less than that

My Code:

Gamecanvas.java :

package game1;

import java.io.IOException;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.game.GameCanvas;

public class Gamecanvas extends GameCanvas implements Runnable{

    private int W=getWidth(),H=getHeight();

    public Gamecanvas(){    
    super(false);
    }
//First called by the Midlet
    public void start(){
        setFullScreenMode(true);


        Thread thread=new Thread(this);
        thread.start();
    }

    public void run() {

        System.out.println("Running Thread!");
        Graphics(getGraphics());

    }

    private void Graphics(Graphics g) {
        Colours(g);

    }

    private void Colours(Graphics g) {
        int w=getWidth(),h=getHeight();

        System.out.println("Correct height="+h);
            System.out.println("Wrong Height="+H);

        g.setColor(0xFFFF00);
        g.fillRect(0, 0, w, h);
        //If i write g.fillRect(0,0,W,H); the screen is not fully filled

        flushGraphics();
    }


}

When placing int h=getHeight(); inside methods it returns 309 which is correct and When placing int H=getHeight(); outside methods it returns 291 which is wrong

getWidth has no problem.Both w and W returns 240.

So,What is causing this abnormal behavior of getHeight()?

Was it helpful?

Solution

It's generally a bad idea to call getHeight() (or any function for that matter) before the constructor has been called. Only when the constructor has been called, has the object been instantiated.

You call getHeight() before the constructor has been called, and thus get the value 291, which I'm guessing is the available height in non-fullscreen mode. Then the constructor is called, in which you setFullScreenMode(true), and then the value becomes 309.

A few other things to notice: It's actually also a bad idea to call setFullScreenMode(true) in the constructor (according to some people at least), because the object hasn't been created yet. Best practise is to call it inside the run() method. It will still run on some devices, if you call it inside the constructor, but some devices may complain about it. This seems to only go for JavaME though, because when you look at Android development, you'll find many situations where you're actually forced to code exactly the opposite way... meaning, calling functions on the object in the constructor. Go figure.

Anyway, another thing to keep in mind is that getHeight() will return the amount of available pixels there are. Just because the phone has a resolution of 240x320 pixels, doesn't mean you get 320 pixels to work with. Some devices "steal" some of the pixels for silly things we don't really want, but we can't do anything about it.

EDIT: Regarding the "Don't call functions on the object inside its constructor": It was discussed a lot at j2meforums.org back when it existed. A lot of devs had problems getting setFullScreenMode(true) working, because they called it inside the constructor. It makes sense when you think about it. You cannot call functions on an object that doesn't yet exist, and it doesn't exist before the constructor has been called. (This is why some J2ME devs are confused by Android development, because in Android development you're actually forced to call functions on various objects inside their own constructor. Coming from J2ME, that way of doing things takes a little getting used to).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top