Thursday, August 10, 2006

About Screen Source Code for J2ME

Example source code for a typical About screen for your J2ME applications using alerts. It allows you to load an image color and greyscale of your logo and determines if the device is capable of displaying it in color otherwise displays the greyscale version. This class can easily be extended to have the text and image to be passed as parameters or in a resource file.


public class X extends javax.microedition.midlet.MIDlet
implements CommandListener, Runnable {


...
/** command about */
private Command cmdAbout = null;

...


...
cmdAbout = new Command("About", Command.HELP, 30);
anyForm.addCommand(cmdAbout);
...


public void commandAction(Command cmd, Displayable d) {
...

if (cmd == cmdAbout) {
About.showAbout(display);
}
....


/*
* @(#)About.java
* (C) Copyright Serkan Azmi. 2001
* All rights reserved
* The material(s) may be used and/or copied only with the written
* permission of Serkan Azmi. or in accordance with the terms and
* conditions stipulated in any agreement/contract under which
* the material(s) have been supplied.
*
* Created on 18 September 2001, 08:44
*/

import javax.microedition.lcdui.*;

/**
* Typical about box with a string and an image.
*/

public class About {

private static String copyright =
"\251 Serkan Azmi 2002";

private Displayable previous;
// the previous screen to go back to

private About() {};
// no instances

/**
* Put up the About box and when the use click ok return
* to the previous screen.
*/
public static void showAbout(Display display) {

Alert alert = new Alert("About");
alert.setTimeout(Alert.FOREVER);

if (display.numColors() > 2) {
String icon = (display.isColor()) ?
"/icons/NAME_OF_APPLICATION_ICON.png" :
"/icons/NAME_OF_APPLICATION_ICON_IN_GREYSCALE.png";

try {
Image image = Image.createImage(icon);
alert.setImage(image);
} catch (java.io.IOException x) {
// just don't append the image.
}
}
Runtime runtime = Runtime.getRuntime();

runtime.gc();
alert.setString(copyright);

display.setCurrent(alert);
}

}

No comments: