Wednesday, July 12, 2006

MIDlet About Command Button Example

Here is some code I wrote back in 2002 which will display a copyright character \251 , specified image located in the resoures, checkes for device capabilitities ie. if it is able to render in color or black and white.

You can modify the About class to have parameters for the copyright string and icon variables.

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: