Sunday, August 23, 2009

Fullscreen Landscape

Most handset screens are portrait: higher than wider. What if you want to show content in landscape mode? Since MIDP 2.0 we can use an Sprite to rotate an image in 90 degrees.


First thing we need is an Image of the correct size. Following code could be inside the constructor of a class that extends Canvas - considering an Sprite and an Image attributes:



int width = Math.max(super.getWidth(), super.getHeight());
int height = Math.min(super.getWidth(), super.getHeight());
screen = Image.createImage(width, height);
sprite = new Sprite(screen);
if (super.getWidth() < super.getHeight()) { // portrait screen
sprite.setTransform(Sprite.TRANS_ROT90);
sprite.setPosition(0, 0);
}

When painting your content use the mutable Image Graphics, then update the sprite with the image like bellow.



protected void paint(Graphics g1) {
Graphics g = screen.getGraphics();
// ... do your drawing
this.sprite.setImage(screen, screen.getWidth(), screen.getHeight());
sprite.paint(g1);
}

How can you use the biggest possible area on the handset display?



  1. Do not setTitle on your Canvas

  2. Do not addCommand to your Canvas

  3. Call setFullScreenMode(true) before calling super.getWidth() and super.getHeight()

No comments: