2

In my j2me App I have tried canvas which works great on Nokia phone but doesn't run on samsung. For that I have to switch to some FORM which in both cases works but only issue is of size, if I create smaller image to fit for both phone screens, one (samsung) shows that ok but other (nokia) leaves a lot more space and vice versa.

I need to have code that could stretch my image and just fix if to the screen size which I basically get by form.getHeight() and form.getWidth() property. I wonder if there is property of Image.createImage(width, height) then why doesn't it stretch it to the value I provide?

my code for that is below

try {
        System.out.println("Height: " + displayForm.getHeight());
        System.out.println("Width: " + displayForm.getWidth());
        Image img1 = Image.createImage("/bur/splashScreen1.PNG");
        img1.createImage(displayForm.getHeight(), displayForm.getWidth()); 
        displayForm.append(new ImageItem(null, img1, Item.LAYOUT_CENTER, null));
    } catch (Exception ex) {
    }

Image enter image description here

Saqib
  • 1,120
  • 5
  • 22
  • 40

2 Answers2

3

A single image will not fit all screens. But more will do.
The smaller logo image should be less than 96x54, as this is the smallest screen resolution. This image can be used up to the resolution of 128x128 without problems. With bigger resolutions it will look tiny, though.
The bigger logo image should be a bit bigger than 128x128 and can be used up to 240x320. The code bellow gives as example of how to implement this.

import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.Graphics;

class Splash extends javax.microedition.lcdui.Canvas {

  Image logo;

  Splash () {
    if (getWidth() <= 128) {
      // sl stands for Small Logo and does not need to have a file extension
      // this will use less space on the jar file
      logo = Image.createImage("/sl");
    } else {
      // bl stands for Big Logo
      logo = Image.createImage("/bl");
    }
  }

  protected void paint (Graphics g) {
    // With these anchors your logo image will be drawn on the center of the screen.
    g.drawImage(logo, getWidth()/2, getHeight()/2, Graphics.HCENTER | Graphics.VCENTER);
  }
}

As seen in http://smallandadaptive.blogspot.com.br/2008/10/showing-splash.html

Telmo Pimentel Mota
  • 4,033
  • 16
  • 22
  • well thats a closer and possible solution, I want to know one more thing, what does Image.createImage(height, width); has to do with the image height and width when it doesn't create the image/stretch it to the values we give? – Saqib Dec 05 '12 at 04:45
  • I have issue with Canvas if you read my question once again, so that won't work for all phone I am using for this application! – Saqib Dec 05 '12 at 11:02
  • createImage(int, int) = "Creates a new, mutable image for off-screen drawing. Every pixel within the newly created image is white. The width and height of the image must both be greater than zero." http://docs.oracle.com/javame/config/cldc/ref-impl/midp2.0/jsr118/javax/microedition/lcdui/Image.html It does not perform stretching. – Telmo Pimentel Mota Dec 05 '12 at 12:18
  • The important thing is the if/else block. Pick an imagem based on screen width. – Telmo Pimentel Mota Dec 05 '12 at 12:20
0

wonder if there is property of Image.createImage(width, height) then why doesn't it stretch it to the value I provide?

Parameters in this method have nothing to do with stretching, see API javadocs:

 public static Image createImage(int width, int height)

    Creates a new, mutable image for off-screen drawing.
        Every pixel within the newly created image is white.
        The width and height of the image must both be greater than zero.

    Parameters:
        width - the width of the new image, in pixels
        height - the height of the new image, in pixels

Image class (API javadocs) has two more createImage methods that use parameters called "width" and "height" - one with six, another with four arguments but none of these has anything to do with stretching.

  • In createImage with six arguments, width and height specify size of the region to be copied (without stretching) from source image.

  • In method with four arguments, width and height specify how to interpret source ARGB array, without these it would be impossible to find out if, say, array of 12 values represents 3x4 image or 4x3. Again, this has nothing to do with stretching.

gnat
  • 6,213
  • 108
  • 53
  • 73
  • 1
    I have added image showing Image.createImage(width, height) method I was talking about, in my question. Whereas about stretch image I meant that doesn't it create an image with that width and height or it just gives it spaces of that value regardless of the size of the image? And the link you provided there is that method included if you see that once again! – Saqib Dec 05 '12 at 08:53
  • @Saqib good catch, thanks - I corrected the answer. Unfortunately, no _stretching_ even with correction... – gnat Dec 05 '12 at 08:59