import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
/*
MIDlet to demonstrate the functionality supported by javax.microedition.lcdui package.
Make changes inside the constructor (SimpleMIDlet() method) and inside the startApp()
method to see all the different functionality in this package.
*** High-level APIs
---------------
1. testForm() method demonstrates a variety of formElements which are individually
instantiated from inside the addItemsToForm() method
2. testAlert() method throws up an Alert on top of an existing form when the SCREEN
button on the form is clicked
3. testList() and testBox() methods demonstrate the use of IMPLICIT List and TextBox
respectively
*** Low-level APIs
--------------
testCanvas()method demonstrates the use of low-level APIs
*/
public class SimpleMIDlet extends MIDlet implements CommandListener, ItemStateListener
{
Form simpleForm;
List simpleList;
TextBox simpleTextBox;
Alert simpleAlert;
SimpleCanvas simpleCanvas;
Ticker t;
Command c1 = new Command("Back", Command.BACK, 1);
Command c2 = new Command("Screen", Command.SCREEN, 1);
Command c3 = new Command("OK", Command.OK, 1);
public SimpleMIDlet (){
// High-level API examples
// 1. Form and form elements:
// a. Uncomment corresponding line in startApp() also
// b. Uncomment following line and comment out all other lines
testForm();
// 2. Alert example; click on the SCREEN button to see the Alert
// a. Uncomment corresponding line in startApp() also
// b. Uncomment following line and comment out all other lines
// testAlert();
// 3. IMPLICIT List example
// a. Uncomment corresponding line in startApp() also
// b. Uncomment following line and comment out all other lines
// testList();
// 4. TextBox example
// a. Uncomment corresponding line in startApp() also
// b. Uncomment following line and comment out all other lines
// testTextBox();
// Low-level API examples
// a. Uncomment corresponding line in startApp() also
// b. Uncomment following line and comment out all other lines
// testCanvas();
}
protected void destroyApp(boolean unconditional) {
}
protected void pauseApp() {
}
private void testForm() {
simpleForm = new Form("Simple Form");
// Create and add a new ticker to the Form
t = new Ticker("Tick tock tick tock");
simpleForm.setTicker(t);
// Add a few Commands to the Form
simpleForm.addCommand(c1);
simpleForm.addCommand(c2);
simpleForm.addCommand(c3);
addItemsToForm();
}
private void addItemsToForm() {
addChoiceGroup();
addDateField();
addGauge();
addImageItem();
addTextField();
}
private void addChoiceGroup() {
Image icon = null;
// Load image
try {
icon = Image.createImage("/midp/simpleMIDlet/Icon.png");
}
catch (java.io.IOException e) {
System.out.println("Could not load image. Exception: " + e);
}
// Image choices
Image[] imageArray = new Image[]{ icon, icon, icon };
String[] stringArray = { "Choice 1",
"Choice 2",
"Choice 3" };
// Set up an exclusive choice group
ChoiceGroup cGroup1 = new ChoiceGroup("Exclusive",
ChoiceGroup.EXCLUSIVE,
stringArray,
imageArray);
// Set up a multiple choice choice group
ChoiceGroup cGroup2 = new ChoiceGroup("Multiple",
ChoiceGroup.MULTIPLE,
stringArray,
imageArray);
simpleForm.append( cGroup1 );
simpleForm.append( cGroup2 );
simpleForm.setItemStateListener(this);
}
public void itemStateChanged (Item item) {
if( item.getLabel().equals("Exclusive")) {
int choice = ((ChoiceGroup)item).getSelectedIndex();
switch (choice) {
case 0:
simpleForm.append("\nSelection: 0\n");
break;
case 1:
simpleForm.append("\nSelection: 1\n");
break;
case 2:
simpleForm.append("\nSelection: 2\n");
break;
}
}
}
private void addDateField() {
simpleForm.append(new DateField("Date", DateField.DATE));
simpleForm.append(new DateField("Date & Time", DateField.DATE_TIME));
}
private void addGauge() {
// Add an interactive gauge with the maximum value of 100
// and an initial value of 50
simpleForm.append(new Gauge("Interactive", true, 100, 50));
// Add a non-interactive gauge
simpleForm.append(new Gauge("Non-interactive", false, 100, 50));
}
private void addImageItem() {
Image image = null;
try {
image = Image.createImage("/midp/SimpleMIDlet/JavaPowered-8.png");
}
catch(java.io.IOException e) {
System.out.println("Could not load image. Exception: " + e);
}
simpleForm.append(
new ImageItem("Default layout",
image,
ImageItem.LAYOUT_LEFT + ImageItem.LAYOUT_NEWLINE_BEFORE,
"Image not visible"));
}
private void addTextField() {
simpleForm.append(new TextField("Any character", "", 15, TextField.ANY));
simpleForm.append(new TextField("Email address", "", 20, TextField.EMAILADDR));
simpleForm.append(new TextField("Numeric", "", 10, TextField.NUMERIC));
simpleForm.append(new TextField("Phone", "", 12, TextField.PHONENUMBER));
simpleForm.append(new TextField("Password", "", 6, TextField.PASSWORD));
simpleForm.append(new TextField("URL", "", 30, TextField.URL));
}
private void testList() {
String[] listElems = {
"Element 1",
"Element 2",
"Element 3",
"Element 4",
"Element 5",
"Element 6",
"Element 7",
"Element 8",
"Element 9"
};
simpleList = new List("List", List.IMPLICIT, listElems, null);
}
private void testTextBox() {
simpleTextBox = new TextBox("Textbox", "4154", 10, TextField.PASSWORD);
}
private void testAlert() {
testForm();
simpleAlert = new Alert("Alert");
simpleAlert.setTimeout(Alert.FOREVER);
simpleAlert.setString("This is a test Alert");
simpleAlert.setType(AlertType.INFO);
}
private void testCanvas() {
simpleCanvas = new SimpleCanvas();
}
public void commandAction(Command c, Displayable d) {
if ( c.getCommandType() == Command.BACK ) {
// Go back
}
if ( c.getCommandType() == Command.SCREEN ) {
Display.getDisplay(this).setCurrent(simpleAlert, simpleForm);
}
}
protected void startApp() {
SimpleMIDlet simpleMIDlet = new SimpleMIDlet();
// High-level API examples
// 1. Uncomment the following two lines for Form and Alert examples and comment all others
Display.getDisplay(this).setCurrent(simpleMIDlet.simpleForm);
simpleMIDlet.simpleForm.setCommandListener(this);
// 2. Uncomment following line for IMPLICIT List example and comment all others
// Display.getDisplay(this).setCurrent(simpleMIDlet.simpleList);
// 3. Uncomment following line for TextBox examples and comment all others
// Display.getDisplay(this).setCurrent(simpleMIDlet.simpleTextBox);
// Low-level API examples
// Uncomment following line and comment all others
// Display.getDisplay(this).setCurrent(simpleMIDlet.simpleCanvas);
}
}
class SimpleCanvas extends Canvas {
int width, height;
int widthShift, heightShift;
String displayString;
public SimpleCanvas( ){
super();
width = getWidth() - 1;
height = getHeight() - 1;
widthShift = 0;
heightShift = 0;
displayString = "Hello World";
}
protected void paint( Graphics g ){
g.setColor( 0xffffff );
g.fillRect( 0, 0, width, height );
g.setColor( 0 );
g.drawString( displayString, width/2 + widthShift, height/2 + heightShift,
g.TOP | g.HCENTER );
}
public void keyPressed( int keyCode) {
int action = getGameAction(keyCode);
switch (action) {
case LEFT:
widthShift -= 2;
break;
case RIGHT:
widthShift += 2;
break;
case UP:
heightShift -= 2;
break;
case DOWN:
heightShift += 2;
break;
case FIRE:
displayString = "Boom!";
break;
}
repaint();
}
}
|