Java Reference
In-Depth Information
The createImage() methods described previously return immutable Images . To create a
mutable Image , use the following method:
public static Image createImage(int width, int height)
Typically you would create a mutable Image for off-screen drawing, perhaps for an anima-
tion or to reduce flicker if the device's display is not double buffered.
Any Image you pass to Alert , ChoiceGroup , ImageItem , or List should be immutable. To
create an immutable Image from a mutable one, use the following method:
public static Image createImage(Image image)
You can also create an Image from a portion of another Image using the following method:
public static Image createImage(Image image,
int x, int y, int width, int height, int transform)
This method takes the part of the original image described by x , y , width , and height ; applies
the specified transformation; and returns the result as an immutable Image . The possible trans-
formations are described by constants in the javax.microedition.lcdui.game.Sprite class
and include things like mirroring and 90-degree rotation.
Image also includes methods that handle image data as an int array. We'll talk about these
methods later in Chapter 13.
How do you figure out what size Images you need? Actually, Display provides methods that
return information about the optimal width and height for various types of images:
public int getBestImageHeight(int imageType);
public int getBestImageWidth(int imageType);
The imageType parameter should be one of Display 's constants: LIST_ELEMENT , ALERT , or
CHOICE_GROUP_ELEMENT . (You'll learn all about ChoiceGroup later in this chapter.) If you were
building a List , you could query Display to find the best size for element images. Assuming
you had packaged icons of various sizes in your application, you could select the best-sized
images at runtime.
Editing a List
List provides methods for adding items, removing elements, and examining elements. Each
element in the List has an index. The first element is at index 0, the next at index 1, and so forth.
You can replace an element with set() or add an element to the end of the list with append() . The
insert() method adds a new element to the list at the given index; this bumps all elements at
that position and higher up by one.
public void set(int elementNum, String stringPart, Image imagePart)
public void insert(int elementNum, String stringPart, Image imagePart)
public int append(String stringPart, Image imagePart)
You can examine the string or image for a given element by supplying its index. Similarly,
you can use delete() to remove an element from the List .
Search WWH ::




Custom Search