Images

You have already seen that some of the components provided by the high-level user interface API allow you to display images. You can create a suitable Image object by loading it from a resource in a MIDlet suite’s JAR file encoded in PNG format. This section looks at other ways to create Image objects and discuss how you can use Images with the low-level API.

Creating Images

The Image class has four static methods that can be used to create an Image:

public static Image createImage(String name);
public static Image createImage(byte[] data, int offset, int length);
public static Image createImage(int width, int height);
public static Image createImage(Image source);

The first and second methods build an Image from data stored either in a named resource within the MIDlet’s JAR file (as described in Section 4.2.11) or as part of a byte array in memory. The image data must be in an encoding format that is both self-identifying (typically because it begins with a well-known sequence of bytes, such as “GIF” or “PNG”) and supported by the platform. At the present time, the only encoding format that MIDP devices are required to support is Portable Network Graphics (PNG), which is a public domain replacement for the popular GIF format.

The first of these methods is normally used to load images that are included as part of the MIDlet installed on the device. The second is useful for creating an Image from data read into a byte array from the network or data stored in and retrieved from ...

Get J2ME in a Nutshell now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.