Java Reference
In-Depth Information
The Graphics object can be used to draw lines, shapes, text, and images on the content
area of the item. The Graphics class is fully covered in Chapter 13; for now, we'll just use a few
simple methods to demonstrate how to draw custom items. The w and h parameters indicate
the current width and height of the content area.
Armed with this knowledge, you can create a simple CustomItem by implementing the five
abstract methods described previously and providing a constructor. Listing 7-1 shows one
such class, SimpleItem . This class returns hard-coded values for the minimum and preferred
content dimensions and provides a paint() method that draws a simple pattern of triangles.
Listing 7-1. A Simple Custom Item
import javax.microedition.lcdui.*;
public class SimpleItem
extends CustomItem {
public SimpleItem(String title) { super(title); }
// CustomItem abstract methods.
public int getMinContentWidth() { return 100; }
public int getMinContentHeight() { return 60; }
public int getPrefContentWidth(int width) {
return getMinContentWidth();
}
public int getPrefContentHeight(int height) {
return getMinContentHeight();
}
public void paint(Graphics g, int w, int h) {
g.drawRect(0, 0, w - 1, h - 1);
g.setColor(0x000000ff);
int offset = 0;
for (int y = 4; y < h; y += 12) {
offset = (offset + 12) % 24;
for (int x = 4; x < w; x += 24) {
g.fillTriangle(x + offset, y,
x + offset - 3, y + 6,
x + offset + 3, y + 6);
}
}
}
}
We won't make you write your own MIDlet to see your new item. Listing 7-2 shows a MIDlet
that uses SimpleItem .
Search WWH ::




Custom Search