ImageItem

ImageItem is used to display an image on a Form. ImageItems can be used as links or buttons to help the user navigate the MIDlet. If an image cannot fit to the width of the screen, it is drawn starting from the top left corner and excess is dropped. Each ImageItem object contains a reference to an Image object.

Figure: An example ImageItem

Source code for the example:

import java.io.IOException;
import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;

public class ExampleForm3 extends MIDlet implements CommandListener {

    private Display display;
    private Form form;
    private Command exit;

    public ExampleForm3() {
        form = new Form("Form example 3");
        try {
            Image Logo = Image.createImage(this.getClass().getResourceAsStream("logo.png"));
            // ImageItem(label,Image,layout,alternative text)
            ImageItem imageitem = new ImageItem("ImageItem Example", Logo, ImageItem.LAYOUT_CENTER,"");
            form.append(imageitem);
        } catch (IOException ex) {
            System.out.println("Exception occurred: "+ex);
        }
        exit = new Command("Exit", Command.EXIT, 1);
        form.addCommand(exit);
        form.setCommandListener(this);
    }

    public void startApp() {
        display = Display.getDisplay(this);
        display.setCurrent(form);
    }

    public void pauseApp() {
    }

    public void destroyApp(boolean unconditional) {
    }

    public void commandAction(Command command, Displayable displayable) {
        if (command == exit) {
            destroyApp(false);
            notifyDestroyed();
        }
    }

}

ImageItems are not resized. If an ImageItem’s width extends beyond the screen width, the ImageItem is horizontally cropped. The method of cropping depends on the layout directive provided:

Table: ImageItem cropping

Layout Directive

Cropping Method

LAYOUT_LEFT

Crop from the right

LAYOUT_RIGHT

Crop from the left

LAYOUT_CENTRE

Crop from both sides

If an ImageItem has a label, it is placed above the rendered image on its own line.