O'Reilly logo

J2ME in a Nutshell by Kim Topley

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required