Mobile Application Development Using J2ME - AS Nida
Mobile Application Development Using J2ME - AS Nida Mobile Application Development Using J2ME - AS Nida
Mobile Application Development Using J2ME User Interface Pramote Kuacharoen MIDP Applications • Many MIDP applications are built to run on many different devices without modification • User interface is difficult – Devices have screens of all sizes, in grayscale and in color – Devices vary widely in their input capabilities, from numeric keypads to alphabetic keyboards, soft keys, and even touch screens • Minimum requirements – Screen size is 96x54 pixels, with at least one bit of color depth – Input is fairly open ended; devices are expected to have some type of keyboard, or a touch screen, or possibly both Copyright © 2007 Pramote Kuacharoen – All rights reserved. 2 1
- Page 2 and 3: Designing Application • Abstracti
- Page 4 and 5: Using Display • Display manages a
- Page 6 and 7: Responding to Commands • A listen
- Page 8 and 9: TextBox public TextBox(String title
- Page 10 and 11: Event Handling for IMPLICIT Lists
- Page 12 and 13: List Selections • We can find out
- Page 14 and 15: Items • All items that can be add
- Page 16 and 17: Spacer • Spacer represent empty s
- Page 18 and 19: Gauge • Gauge represents an integ
- Page 20 and 21: Custom Items • A CustomItem is cu
- Page 22 and 23: Appearance Consistency • Two meth
<strong>Mobile</strong> <strong>Application</strong> <strong>Development</strong><br />
<strong>Using</strong> <strong>J2ME</strong><br />
User Interface<br />
Pramote Kuacharoen<br />
MIDP <strong>Application</strong>s<br />
• Many MIDP applications are built to run on many<br />
different devices without modification<br />
• User interface is difficult<br />
– Devices have screens of all sizes, in grayscale and in<br />
color<br />
– Devices vary widely in their input capabilities, from<br />
numeric keypads to alphabetic keyboards, soft keys,<br />
and even touch screens<br />
• Minimum requirements<br />
– Screen size is 96x54 pixels, with at least one bit of<br />
color depth<br />
– Input is fairly open ended; devices are expected to<br />
have some type of keyboard, or a touch screen, or<br />
possibly both<br />
Copyright © 2007 Pramote Kuacharoen – All rights reserved.<br />
2<br />
1
Designing <strong>Application</strong><br />
• Abstraction<br />
– Specifying a user interface in abstract terms and<br />
replying on the MIDP implementation to create<br />
something concrete<br />
• Discovery<br />
– Obtaining the device information at runtime and<br />
tailoring the user interface programmatically<br />
Copyright © 2007 Pramote Kuacharoen – All rights reserved.<br />
User Interface Classes<br />
• MIDP contains user interface classes in the<br />
javax.microedition.lcdui and<br />
javax.microedition.lcdui.game packages<br />
• The device’s display is represented by an instance<br />
of the Display class which obtained by the<br />
getDisplay method<br />
• The Display’s main purpose is to keep track of what<br />
is currently shown, which is an instance of<br />
Displayable<br />
• Midlets can change the contents of the display by<br />
passing Displayable instances to the Display’s<br />
setCurrent method<br />
Copyright © 2007 Pramote Kuacharoen – All rights reserved.<br />
3<br />
4<br />
2
The Basic Function of a Typical MIDlet<br />
• Show a Displayable<br />
• Wait for input<br />
• Decide what Displayable should be next<br />
• Repeat<br />
Copyright © 2007 Pramote Kuacharoen – All rights reserved.<br />
javax.microeidtion.lcdui<br />
Screen<br />
Displayable Class Hierachy<br />
Displayable<br />
Canvas<br />
Alert List Form TextBox<br />
Copyright © 2007 Pramote Kuacharoen – All rights reserved.<br />
javax.microeidtion.lcdui.game<br />
GamesCanvas<br />
5<br />
6<br />
3
<strong>Using</strong> Display<br />
• Display manages a device’s screen<br />
• The getDisplay method obtains the reference to the device’s display<br />
• Once we have got a reference to a display’s Display, we can create an<br />
instance of Displayable and pass it to one of the Display’s setCurrent<br />
methods<br />
• The getCurrent method returns a reference to what’s currently being<br />
shown<br />
• Other methods: numColors, getColor, isColor, isShown,<br />
getBorderStyle, flashBacklight, vibrate<br />
public void startApp() {<br />
Display d = Display.getDisplay(this);<br />
// more code<br />
}<br />
// Requests that a different Displayable object be made visible<br />
// on the display<br />
public void setCurrentDisplay(Displayable nextDisplayable)<br />
// Requests that this Alert be made current, and that nextDisplayable<br />
// be made current after the Alert is dismissed<br />
public void setCurrentDisplay(Alert alert, Displayable nextDisplayable)<br />
Copyright © 2007 Pramote Kuacharoen – All rights reserved.<br />
Event Handling with Commands<br />
• A command is an action the user can invoke<br />
• Every displayable keeps a list of its Commands<br />
• We can add and remove a command from the Displayable<br />
// Adds a command to the Displayable<br />
public void addCommand(Command cmd)<br />
// Removes a command from the Displayable<br />
public void removeCommnad(Command cmd)<br />
Copyright © 2007 Pramote Kuacharoen – All rights reserved.<br />
7<br />
8<br />
4
Commands<br />
• In MIDP, commands are represented by instances of<br />
the Command class<br />
• To create a Command, just supply a name, a type,<br />
and a priority<br />
– The name is usually shown on the screen<br />
– The type is the type of the command<br />
– The priority is used to determines what command can<br />
be display when there are more commands than<br />
available screen space (0 is highest priority)<br />
• Commands are assigned to two soft buttons in the<br />
Sun <strong>J2ME</strong> Wireless Toolkit emulator<br />
• If there are more than two commands, the second<br />
soft buttons will be Menu<br />
Copyright © 2007 Pramote Kuacharoen – All rights reserved.<br />
Command Types<br />
Name Meaning<br />
OK<br />
CANCEL<br />
BACK<br />
STOP<br />
HELP<br />
SCREEN<br />
Creating Commands<br />
Confirms selection<br />
Cancels pending changes<br />
Moves the user back to a previous screen<br />
Stops a running operation<br />
Shows application instructions<br />
Indicates generic type of specific application commands<br />
Short Label Commands<br />
Command c = new Command(“OK”, Command.OK, 0);<br />
Command l = new Commnad(“Launch”, Command.SCREEN, 0);<br />
Long Label Command<br />
Command c = new Command(“Run”, “Run simulation”, Command.OK, 0);<br />
The Command class provides getLabel, getLongLabel, getCommandType methods<br />
Copyright © 2007 Pramote Kuacharoen – All rights reserved.<br />
9<br />
10<br />
5
Responding to Commands<br />
• A listener object is notified when the user invokes any<br />
command in a Displayable<br />
• The listener is an object that implements the CommandListener<br />
interface<br />
• The listener can be registered with a Displayable<br />
// Registers the listener<br />
public void setListener(CommandListener l)<br />
// Required method for classes which implement<br />
// the interface CommandListener<br />
public void commandAction(Command c, Displayable s)<br />
Copyright © 2007 Pramote Kuacharoen – All rights reserved.<br />
import javax.microedition.midlet.*;<br />
import javax.microedition.lcdui.*;<br />
Ex: A Simple Command<br />
public class Commander extends MIDlet {<br />
public void startApp() {<br />
Displayable d =<br />
new TextBox("Enter Text", "Commander",<br />
20, TextField.ANY);<br />
Command c = new Command("Exit", Command.EXIT, 0);<br />
d.addCommand(c);<br />
d.setCommandListener(new CommandListener() {<br />
public void commandAction(Command c,<br />
Displayable s){<br />
notifyDestroyed();<br />
}<br />
});<br />
Display.getDisplay(this).setCurrent(d);<br />
}<br />
public void pauseApp() {}<br />
public void destroyApp(boolean unconditional) {}<br />
}<br />
Copyright © 2007 Pramote Kuacharoen – All rights reserved.<br />
11<br />
12<br />
6
• All Displayables have a title<br />
and an optional tickers<br />
Titles & Tickers<br />
public void setTitle(String newTitle)<br />
public String getTitle()<br />
// Displayable d = ...<br />
Ticker ticker = new Ticker(“<strong>Mobile</strong> <strong>Application</strong> <strong>Development</strong>”);<br />
d.setTicker(ticker);<br />
Copyright © 2007 Pramote Kuacharoen – All rights reserved.<br />
Screens<br />
• Screen is the base class for all classes that<br />
represent generalized user interfaces<br />
– Alert<br />
– Form<br />
– List<br />
– TextBox<br />
• This class has no method of its own, but inherits all<br />
from Displayable<br />
Copyright © 2007 Pramote Kuacharoen – All rights reserved.<br />
13<br />
14<br />
7
TextBox<br />
public TextBox(String title, String text, int maxSize, int contstraints)<br />
TextBox class constaints<br />
ANY allows any type of input<br />
NUMERIC restricts the input to integers<br />
DECIMAL allows numbers with fractional parts<br />
PHONENUMBER requires a telephone number<br />
EMAILADDR must be an email address<br />
URL must be a URL<br />
Copyright © 2007 Pramote Kuacharoen – All rights reserved.<br />
TextBox class flags<br />
P<strong>AS</strong>SWORD<br />
UNEDITABLE<br />
SENSITIVE<br />
NON_PREDICTIVE<br />
INITIAL_CAPS_WORD<br />
INITIAL_CAPS_SENTENCE<br />
Displayable d = new TextBox(“Email”, “”, 64,<br />
TextField.EMAILADDR | TextField.P<strong>AS</strong>SWORD);<br />
Alerts<br />
• An alert is an informative message shown to the user<br />
– A timed alert is shown for a certain amount of time<br />
– A modal alert stays up until the user dismisses it<br />
• When dismissed<br />
– Advances to the next screen if called the two-argument<br />
setCurrent method of the Display<br />
– Back to the previous screen if called the regular oneargument<br />
setCurrent method<br />
public Alert(String title)<br />
public Alert(String title, String alertText, Image alertImage,<br />
AlertType alertType)<br />
Alert a = new Alert(“Sorry”, “I’m sorry. I’m afraid you can’t do that.”,<br />
null, null);<br />
a.setTimeout(5000); // timed alert 5 seconds<br />
a.setTimeout(Alert.FOREVER); // modal alert<br />
Copyright © 2007 Pramote Kuacharoen – All rights reserved.<br />
15<br />
16<br />
8
Copyright © 2007 Pramote Kuacharoen – All rights reserved.<br />
EXCLUSIVE<br />
ALARM<br />
CONFIRMATION<br />
ERROR<br />
INFO<br />
WARNING<br />
AlertType<br />
An alert to an event<br />
A hint to confirm user actions<br />
An erroneous operation<br />
A non-threatening information<br />
A warning<br />
Lists<br />
MULTIPLE<br />
Copyright © 2007 Pramote Kuacharoen – All rights reserved.<br />
IMPLICIT<br />
17<br />
18<br />
9
Event Handling for IMPLICIT Lists<br />
• When the user makes a selection in an IMPLICIT list, the<br />
commandAction method of the list’s CommandListener is<br />
invoked<br />
public void commandAction(Command c, Displayable s) {<br />
if (c == nextCommand)<br />
// ...<br />
else if (c == List.SELECTED_COMMAND)<br />
// ...<br />
}<br />
Copyright © 2007 Pramote Kuacharoen – All rights reserved.<br />
Creating Lists<br />
• List types<br />
– List.EXCLUSIVE<br />
– List.MULTIPLE<br />
– List.IMPLICIT<br />
• The stringElements cannot be null, but can have zero elements<br />
• The imageElements can be null<br />
public List(String title, int type)<br />
public List(String title int type,<br />
String[] stringElements, Image[] imageElements)<br />
Copyright © 2007 Pramote Kuacharoen – All rights reserved.<br />
19<br />
20<br />
10
Images<br />
• An Image object is an instance of the<br />
javax.microedition.lcdui.Image class<br />
• The image files must be in PNG format<br />
• The Image class has no constructors<br />
• Image objects may be mutable and immutable<br />
public static Image createImage(String name)<br />
public static Image createImage(byte[] imageData, int imageOffset,<br />
int imageLength)<br />
public static Image createImage(InputStream stream)<br />
public static Image createImage(Image image, int x, int y,<br />
int width, int height, int transform)<br />
public static Image createImage(Image source)<br />
public static Image createImage(int width, int height)<br />
Copyright © 2007 Pramote Kuacharoen – All rights reserved.<br />
Editing a List<br />
• List provides methods for adding items, removing elements,<br />
and examining elements<br />
• Appearance of a List can be controlled by using setFitPolicy<br />
method and setFont method<br />
public void set(int elementNum, String stringPart, Image imagePart)<br />
public void insert(int elementNum, String stringPart, Image imagePart)<br />
public int append(String stringPart, Image imagePart)<br />
public String getString(int elementNum)<br />
pubic Image getImage(int elementNum)<br />
public void delete(int elementNum)<br />
public void deleteAll()<br />
public int size()<br />
Copyright © 2007 Pramote Kuacharoen – All rights reserved.<br />
Controlling appearance<br />
setFitPolicy<br />
TEXT_WRAP_ON<br />
TEXT_WRAP_OFF<br />
TEXT_WRAP_DEFAULT<br />
setFont<br />
21<br />
22<br />
11
List Selections<br />
• We can find out if the element is selected<br />
• We can get the selected element’s index<br />
• We can change the current selection programmatically<br />
public boolean isSelected(int index)<br />
public int getSelectedIndex()<br />
public void setSelectedIndex(int index, boolean selected)<br />
public int getSelectedFlags(boolean[] selectedArray_return)<br />
public void setSelectedFlags(boolean[] selectedArray)<br />
Copyright © 2007 Pramote Kuacharoen – All rights reserved.<br />
Ex: an Exclusive List<br />
public class ExclusiveList extends MIDlet implements CommandListener {<br />
public ExclusiveList(){<br />
String[] sElements = { "Airplane", "Car", "Hotel" };<br />
mList = new List("Reservation type", List.EXCLUSIVE, sElements, null);<br />
mNextCommand = new Command("Next", Command.SCREEN, 0);<br />
mExitCommand = new Command("Exit", Command.EXIT, 0);<br />
mList.addCommand(mNextCommand);<br />
mList.addCommand(mExitCommand);<br />
mList.setCommandListener(this);<br />
}<br />
public void startApp() { Display.getDisplay(this).setCurrent(mList); }<br />
public void commandAction(Command c, Displayable s) {<br />
if (c == mNextCommand) {<br />
int index = mList.getSelectedIndex();<br />
Alert alert = new Alert("Your selection",<br />
"You chose " + mList.getString(index) + ".",<br />
null, AlertType.INFO);<br />
Display.getDisplay(this).setCurrent(alert, mList);<br />
}<br />
else if (c == mExitCommand) notifyDestroyed();<br />
}<br />
public void pauseApp() {}<br />
public void destroyApp(boolean unconditional) {}<br />
private List mList;<br />
private Command mExitCommand, mNextCommand;<br />
}<br />
Copyright © 2007 Pramote Kuacharoen – All rights reserved.<br />
23<br />
24<br />
12
Forms<br />
• Form is a screen that can include an arbitrary collection of<br />
user-interface controls, called items<br />
public Form(String title)<br />
public Form(String title, Item[] items)<br />
Copyright © 2007 Pramote Kuacharoen – All rights reserved.<br />
Managing Items<br />
• Items may be added and removed, even while the Form is<br />
showing<br />
• The append method adds an item to the bottom of the form<br />
• The set method replaces an item at a specific index<br />
• The insert method add an item somewhere in the middle of the<br />
form<br />
public int append(Item item)<br />
public int append(String str)<br />
public int append(Image image)<br />
public void set(int index, Item item)<br />
public void insert(int index, Item item)<br />
public void delete(int index)<br />
public void deleteAll()<br />
public int size()<br />
public Item get(int index)<br />
Copyright © 2007 Pramote Kuacharoen – All rights reserved.<br />
25<br />
26<br />
13
Items<br />
• All items that can be added to forms descend from the class<br />
javax.microedition.lcdui.Item<br />
• The class has getLabel and setLabel method<br />
• Items can also have commands, just like Displayables<br />
• When an Item is selected in a form, the Item’s commands are<br />
shown along with the commands in the form<br />
• We can manage the commands on an Item using<br />
addCommand and removeCommand<br />
• A command listener may be assigned using the<br />
setItemCommandListener<br />
public void commandAction(Command c, Item item)<br />
Copyright © 2007 Pramote Kuacharoen – All rights reserved.<br />
Ex: Forms<br />
Copyright © 2007 Pramote Kuacharoen – All rights reserved.<br />
27<br />
28<br />
14
Layouts<br />
• Items have a minimum size and a preferred size<br />
• The Item class includes a layout directive, accessed using<br />
getLayout and setLayout<br />
• LAYOUT_2 is a flag to the implementation that the item should<br />
be laid out using MIDP 2 rules<br />
The horizontal values:<br />
LAYOUT_LEFT<br />
The verticle values:<br />
LAYOUT_RIGHT<br />
LAYOUT_TOP<br />
LAYOUT_CENTER<br />
LAYOUT_BOTTOM Shrinking and expanding:<br />
LAYOUT_VCENTER LAYOUT_SHRINK (width)<br />
LAYOUT_EXPAND (width)<br />
LAYOUT_VSHRINK (height)<br />
LAYOUT_VEXPAND (height)<br />
Copyright © 2007 Pramote Kuacharoen – All rights reserved.<br />
StringItem<br />
LAYOUT_NEWLINE_BEFORE<br />
LAYOUT_NEWLINE_AFTER<br />
• StringItem represent a simple text label<br />
• The appearance of both StringItem and ImageItem can be<br />
controlled using appearance mode<br />
– PLAIN<br />
– HYPERLINK<br />
– BUTTON<br />
public StringItem(String label, String text)<br />
public StringItem(String label, String text, int appearanceMode)<br />
Form form = new Form(“StringItem Test”);<br />
StringItem stringItem = new StringItem(“Label:”, “Value”);<br />
form.append(stringItem)<br />
Copyright © 2007 Pramote Kuacharoen – All rights reserved.<br />
29<br />
30<br />
15
Spacer<br />
• Spacer represent empty space in a Form<br />
• Spacer cannot gain focus and cannot have commands<br />
public Spacer(int minWidth, int minHeight)<br />
Copyright © 2007 Pramote Kuacharoen – All rights reserved.<br />
TextField<br />
• TextField represents an editable string<br />
TextField class constaints<br />
ANY allows any type of input<br />
NUMERIC restricts the input to integers<br />
DECIMAL allows numbers with fractional parts<br />
PHONENUMBER requires a telephone number<br />
EMAILADDR must be an email address<br />
URL must be a URL<br />
public TextField(String label, String text, int maxSize, int constraints)<br />
Copyright © 2007 Pramote Kuacharoen – All rights reserved.<br />
31<br />
32<br />
16
• ItemsItem<br />
– A label<br />
– The layout<br />
– Alternate text<br />
Layout<br />
ImageItem<br />
LAYOUT_CENTER, LAYOUT_DEFAULT, LAYOUT_LEFT,<br />
LAYOUT_NEWLINE_AFTER, LAYOUT_NEWLINE_BEFORE,<br />
LAYOUT_RIGHT<br />
public ImageItem(String label, Image img, int layout, String altText)<br />
public ImageItem(String label, Image img, int layout, String altText,<br />
int appearanceMode)<br />
Copyright © 2007 Pramote Kuacharoen – All rights reserved.<br />
DateField<br />
• DateField is an mechanism by which user can enter dates,<br />
times, or both<br />
• The DateField is an editor for a java.util.Date<br />
public DateField(String label, int mode)<br />
public DateField(String label, int mode, TimeZone timeZone)<br />
public Date getDate()<br />
public void setDate(Date date)<br />
Copyright © 2007 Pramote Kuacharoen – All rights reserved.<br />
Modes:<br />
DATE displays an editable date<br />
TIME displays an editable time<br />
DATE_TIME displays both date and time<br />
33<br />
34<br />
17
Gauge<br />
• Gauge represents an integer value<br />
• The value of a Gauge instance can be retrieved and modified<br />
with getValue and setValue methods<br />
• The value runs from to a variable maximum value<br />
• The maximum for the gauge can be retrieved and modified with<br />
the getMaxValue and setMaxValue methods<br />
• The maximum value can be set to INDEFINITE<br />
• Types<br />
– Interactive<br />
– Noninteractive (incremental, continuous)<br />
public Gauge(String label, boolean interactive,<br />
int maxValue, int initialValue)<br />
Copyright © 2007 Pramote Kuacharoen – All rights reserved.<br />
Ex: Gauges<br />
Copyright © 2007 Pramote Kuacharoen – All rights reserved.<br />
Gauge value:<br />
INCREMENTAL_UPDATING<br />
INCREMENTAL_IDLE<br />
CONTINUOUS_RUNNING<br />
CONTINUOUS_IDLE<br />
35<br />
36<br />
18
ChoiceGroup<br />
• ChoiceGroup offers a list of choices<br />
• It is similar to javax.microedition.lcdui.List<br />
public ChoiceGroup(String label, int choiceType)<br />
public ChoiceGroup(String label, int choiceType, String[] stringElements,<br />
Image[] imageElements)<br />
Choice type:<br />
MULTIPLE<br />
EXCLUSIVE<br />
POPUP<br />
Copyright © 2007 Pramote Kuacharoen – All rights reserved.<br />
Responding to Item Changes<br />
• Most items in a Form fire events when the user changes them<br />
• The application can listen for these events by registering an<br />
ItemStateListener with the Form<br />
• ItemStateListener is an interface with a single method which is<br />
called every time an item in a Form is changed<br />
public void setItemStateListener(ItemStateListener iListener)<br />
// The method in the interface ItemStateListener<br />
public void itemStateChanged(Item item)<br />
Copyright © 2007 Pramote Kuacharoen – All rights reserved.<br />
37<br />
38<br />
19
Custom Items<br />
• A CustomItem is customized by subclassing to introduce new<br />
visual and interactive elements into Forms<br />
• Subclasses are responsible for their visual appearance<br />
including sizing, rendering, choice of colors, fonts, and<br />
graphics<br />
protected abstract int getPrefContentWidth(int height)<br />
protected abstract int getPrefContentHeight(int width)<br />
protected abstract int getMinContentWidth()<br />
protected abstract int getMinContentHeight()<br />
protected abstract void paint(Graphic g, int w, int h)<br />
Copyright © 2007 Pramote Kuacharoen – All rights reserved.<br />
Ex: Custom Item<br />
import javax.microedition.lcdui.*;<br />
public class SimpleItem extends CustomItem {<br />
public SimpleItem(String title) { super(title); }<br />
// CustomItem abstract methods.<br />
public int getMinContentWidth() { return 100; }<br />
public int getMinContentHeight() { return 60; }<br />
public int getPrefContentWidth(int height) {<br />
return getMinContentWidth();<br />
}<br />
public int getPrefContentHeight(int width) {<br />
return getMinContentHeight();<br />
}<br />
public void paint(Graphics g, int w, int h) {<br />
g.drawRect(0, 0, w - 1, h - 1);<br />
g.setColor(0x000000ff);<br />
int offset = 0;<br />
for (int y = 4; y < h; y += 12) {<br />
offset = (offset + 12) % 24;<br />
for (int x = 4; x < w; x += 24) {<br />
g.fillTriangle(x + offset, y,<br />
x + offset - 3, y + 6,<br />
x + offset + 3, y + 6);<br />
}<br />
}<br />
}<br />
}<br />
Copyright © 2007 Pramote Kuacharoen – All rights reserved.<br />
39<br />
40<br />
20
Custom Item Painting<br />
• A CustomItem is drawn on the screen in its paint<br />
method<br />
• The parameter of Graphic serves two purposes<br />
– The drawing surface of the CustomItem’s content area<br />
– Numerous methods for drawing shapes, images, and<br />
text<br />
• The paint method is called by the MIDP<br />
implementation whenever it needs to show the<br />
custom item on the screen<br />
• The MIDP implementation also calls other method to<br />
find out the minimum and preferred sizes when the<br />
form is being laid out<br />
Copyright © 2007 Pramote Kuacharoen – All rights reserved.<br />
Updating the Custom Item Appearance<br />
• If something needs to change in the custom item’s appearance,<br />
we can request a refresh by calling the repaint method<br />
• The MIDP implementation will soon call the paint method again<br />
protected void repaint()<br />
protected void repaint(int x, int y, int width, int height)<br />
Copyright © 2007 Pramote Kuacharoen – All rights reserved.<br />
41<br />
42<br />
21
Appearance Consistency<br />
• Two methods return information that can help we make our<br />
item’s appearance consistent with the device’s look and feel<br />
– The getColor method in the Display class<br />
– The getFont method in the Font class<br />
public void paint(Graphic g, int w, int h) {<br />
// Display d = ...<br />
int fhc = d.getColor(Display.COLOR_HIGHLIGHT_FOREGROUND);<br />
g.setColor(fhc);<br />
// Draw stuff<br />
}<br />
public int getColor(int colorSpecifier)<br />
public static Font getFont(int fontSpecifier)<br />
FONT_STATIC_TEXT<br />
FONT_INPUT_TEXT<br />
Copyright © 2007 Pramote Kuacharoen – All rights reserved.<br />
COLOR_BACKGROUND, COLOR_BORDER,<br />
COLOR_FOREGROUND,<br />
COLOR_HIGHLIGHTED_BACKGROUND,<br />
COLOR_HIGHLIGHTED_BORDER,<br />
COLOR_HIGHLIGHTED_FORGROUND<br />
Showing, Hiding, and Sizing<br />
• When a CustomItem is made visible, even partially<br />
visible, its showNotify method is called<br />
• The subsequent call to the paint method to render<br />
the item<br />
• The hideNotify is called when the item is no longer<br />
visible<br />
• The sizeChanged method is called with the new<br />
width and height of the content area when the<br />
containing Form gets laid out again<br />
• If the custom item need a different size, it should call<br />
the invalidate method<br />
Copyright © 2007 Pramote Kuacharoen – All rights reserved.<br />
43<br />
44<br />
22
Handling Events<br />
• A CustomItem can respond to keyboard and pointer events by<br />
overriding the related methods<br />
• Devices have varying capabilities and some may not be able to<br />
deliver certain types of events to CustomItems<br />
• Custom items can use the getInteractionModes method to find<br />
out the capabilities of the device at runtime<br />
protected void keyPressed(int keyCode)<br />
protected void keyReleased(int keyCode)<br />
protected void keyRepeated(int keyCode)<br />
protected void pointerPressed(int x, int y)<br />
protected void pointerReleased(int x, int y)<br />
protected void pointerDragged(int x, int y)<br />
Copyright © 2007 Pramote Kuacharoen – All rights reserved.<br />
Item Traversal<br />
KEY_NUM0 – KEY_NUM9<br />
KEY_POUND<br />
KEY_STAR<br />
• Forms support a concept of focus (one item in the form is<br />
currently selected)<br />
• Traversal refers to the user being able to shift focus from one<br />
item to another<br />
protected boolean traverse(int dir, int viewportWidth, int viewportHeight,<br />
int [] visRect_inout)<br />
protected void traverseOut()<br />
Copyright © 2007 Pramote Kuacharoen – All rights reserved.<br />
45<br />
46<br />
23