GUI (graphic) programming in MIDlets
1. Overview
MIDP defines an API for the MIDP application running in the MIDP container, which is based on the CLDC API. The Java class design of the MIDP User Interface API is not based on the Java Abstract Window Toolkit (AWT) class, but is specifically designed for mobile phones. This type of equipment has only limited screen sizes and keyboard performance. When programmers write graphics applications with MIDP, they can only use the MIDP or CLDC API.
2. MIDP GUI class
1) All MIDP GUI classes are part of the javax.microedition.lcdui package.
2) The basic abstract graphics of the MIDP user interface is the screen, and the Screen class encapsulates the graphics and users of the device. You can only display one screen each time your application.
3) The MIDP API has "High-Level" and "low-level" UI classes.
Advanced UI ----- For example, Form, List, TextBox, TextField, Alert, and Ticker have a device adaptation feature, which supports images, text, text domain, and radio buttons.
Low-level UI -----, for example, the Canvas class allows the operator to draw any painting.
3. Main class in MIDP GUI
1) Graphics class ----- provides a Graphics object for painting 2D geometric objects.
The javax.microedition.lcdui.graphics class is not created in the MIDLET, which provides a PAINT () method in the MIDP GUI programming in the MIDP GUI programming, using this parameter. Implement the functionality of the GRAPHICS class in the MIDlet.
2) DisplayAble class ----- is an abstract class, Displayable object handles the MIDlet's GUI output. It has two derived class javax.microedition.lcdui.canvas and javax.microedition.lcdui.screen.
3) CANVAS class ----- CANVAS class allows the operator to draw any painting, with the Graphics class is a low-level UI. The method provided by the Graphics class class is always used in the derived class of the Canvas class.
4) Screen class ----- is a high-level UI, Form, List, TextBox, a Alert class is its derived class.
4. Below is a piece of code for the Graphics class:
Import javax.microedition.midlet. *;
Import javax.microedition.lcdui. *;
Public Class Midpgraphics Extends MIDlet ImmancellListener
{
Display display;
Testcanvas Canvas;
Public midpgraphics ()
{
Display = display.getdisplay (this);
CANVAS = New Testcanvas ();
}
Public void startapp () throws MidletStateChangeException
{
Display.Setcurrent (Canvas) and DISPLAY.SETCURRENT
}
Public void pauseApp ()
{
}
Public Void DestroyApp (Boolean Unconditional) Throws MidletStateChangeException
{
}
Public void CommandAction (Command C, Displayable D)
{
}
}
Class Testcanvas Extends Canvas
{
Public void Paint (Graphics G)
{
// Create a font object
Font font = font.getfont
(Font.face_system, font.style_bold, font.size_medium);
// Set font object
G.SetFont (font);
// Painting text
g.drawstring ("Hello Midp", getWidth () / 2, GetHeight () / 2, graphics.hcenter|graphics.top;
Try
{
// Painting Image
Image image = image.createImage ("MyImage.png");
G.DrawImage (image, 0,0, graphics.hcenter | graphics.top);
}
Catch (Exception E)
{
}
}
}
---------------------------- The operation is to display text "Hello MIDP" in the center of the screen.
5. Program the MIDlet to implement different graphical user interface instance code for the Savemymoney bank app:
1) Task statement: The first screen display contains the menu named Balance Enquiry, Fixed Deposit Enquiry (Regular Savings), Check Status Enquiry item
When the user chooses Balance Enquiry to display the progress indicator
When the user selects Fixed Deposit ENQUIRY to display the progress indicator
When the user selects Check Status Enquiry, you should display the TEXTBOX to the check number.
When the user is inserted into the password, the progress indicator should be displayed.
2. code show as below:
// Import LCDUI and MIDlet package
Import javax.microedition.lcdui. *;
Import javax.microedition.midlet. *;
/ / Define the MIDlet class that extends the Midpgui class
Public Class Midpgui Extends Midlet ImmancelliStener
{
// Create a display manager by the Display class
Display display;
// Define form objects
Form form = new form ("Container Form");
// Screen class is derived
// Define a list (main menu)
List menu;
// Define text box
Textbox INPUT;
// Item class component (Gauge class represents a bar chart on the screen)
Gauge Gauge = New Gauge ("Your Enquiry Is Being Processed", False, 100, 30);
// Define commands from the Command class
Static Final Command Okcommand = New Command ("OK", Command.ok, 1);
Static final command backcommand = new command ("back", command.back, 0);
Static Final Command EXITCOMMAND = New Command ("EXIT", Command.Stop, 2);
String currentMenu; / / Define the string variable to identify the current form name
Public midpgui ()
{
}
/ **
* The startapp () StartApp () Starts the MIDlet, Creates A List of items and
* Uses the EXIT Command * /
Public void startapp () throws midletStateChangeException {
// Get the display object
Display = display.getdisplay (this);
// Create an initial menu and join the item
Menu = New List ("enquiries", choice.implicit;
Menu.Append ("Current Balance", NULL;
Menu.Append ("Fixed Deposit", NULL;
Menu.Append ("Check Status", NULL;
// Add an exit command to the form
Menu.addCommand (EXITCOMMAND);
Menu.setCommandListener (this);
// Call the MainMenu method, set the initial screen
Mainmenu ();
/ / Add metering components to the form
Form.Append (gauge);
}
// mainMenu method
Void mainmenu ()
{
Display.SetCurrent (MENU);
CurrentMenu = "main";
}
Public void pauseApp () {
Form = null;
Display = NULL;
Menu = NULL;
INPUT = NULL;
Gauge = NULL;
}
// When the midlet is revoked is called
Public Void DestroyApp (Boolean Unconditional) Throws MidletStateChangeException
{
NotifyDestroyed ();
}
/ / Display TextBox components
Public void showtextbox ()
{
Input = New TextBox ("Enter the Check Number:", ", 20, TextField.Any);
Input.addcommand (backcommand);
Input.addcommand (OkCommand);
Input.setCommandListener (this);
Input.setstring ("");
Display.setcurrent (Input);
CurrentMenu = "Input";
}
// Display progress indicator screen
Public void showform ()
{
form.addcommand (backcommand);
Form.setCommandListener (this);
Display.SetCurrent (Form);
CurrentMenu = "form";
}
// Command activation
Public void CommandAction (Command C, Displayable D)
{
String label = c.getlabel ();
Label.equals ("exit"))
{
Try {
DESTROYAPP (TRUE);
Catch (Exception E) {}
}
ELSE IF (Label.equals ("back"))
{
IF (currentMenu.equals ("infut") || currentMenu.equals ("form")))
{
// go back to menu
Mainmenu ();
}
}
Else
{
IF (label.equals ("ok"))
{
Showform ();
}
Else
{
List down = (list) display.getcurrent ();
Switch (Down.getSelectedIndex ())
{
Case 0: showForm (); Break;
Case 1: Showform (); Break;
Case 2: showTextBox (); Break;
}
}
}
}
}
Note: Instructions for all classes and methods can view J2ME DOC. Path is x: / WTK104 / DOCS / API