Java/J2ME/Gauge
Содержание
Gauge Interactive
//jad file (please verify the jar size)
/*
MIDlet-Name: GaugeInteractive
MIDlet-Version: 1.0
MIDlet-Vendor: MyCompany
MIDlet-Jar-URL: GaugeInteractive.jar
MIDlet-1: GaugeInteractive, , GaugeInteractive
MicroEdition-Configuration: CLDC-1.0
MicroEdition-Profile: MIDP-1.0
MIDlet-JAR-SIZE: 100
*/
import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.AlertType;
import javax.microedition.lcdui.rumand;
import javax.microedition.lcdui.rumandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.Gauge;
import javax.microedition.midlet.MIDlet;
public class GaugeInteractive extends MIDlet implements CommandListener {
private Display display;
private Form form = new Form("");
private Command exit = new Command("Exit", Command.EXIT, 1);
private Command vote = new Command("Vote", Command.SCREEN, 1);
private Gauge gauge = new Gauge("Rate the movie: ", true, 5, 1);
public GaugeInteractive() {
display = Display.getDisplay(this);
form.addCommand(exit);
form.addCommand(vote);
form.append(gauge);
form.setCommandListener(this);
}
public void startApp() {
display.setCurrent(form);
}
public void pauseApp() {
}
public void destroyApp(boolean unconditional) {
}
public void commandAction(Command command, Displayable displayable) {
if (command == exit) {
destroyApp(false);
notifyDestroyed();
} else if (command == vote) {
String msg = String.valueOf(gauge.getValue());
Alert alert = new Alert("Ranking", msg, null, null);
alert.setTimeout(Alert.FOREVER);
alert.setType(AlertType.INFO);
display.setCurrent(alert);
}
}
}
Gauge Non-Interactive
//jad file (please verify the jar size)
/*
* MIDlet-Name: GaugeNonInteractive MIDlet-Version: 1.0 MIDlet-Vendor: MyCompany
* MIDlet-Jar-URL: GaugeNonInteractive.jar MIDlet-1: GaugeNonInteractive, ,
* GaugeNonInteractive MicroEdition-Configuration: CLDC-1.0
* MicroEdition-Profile: MIDP-1.0 MIDlet-JAR-SIZE: 100
*
*/
import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.AlertType;
import javax.microedition.lcdui.rumand;
import javax.microedition.lcdui.rumandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.Gauge;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;
public class GaugeNonInteractive extends MIDlet implements CommandListener {
private Display display;
private Form form = new Form("");
private Command exit = new Command("Exit", Command.EXIT, 1);
private Command start = new Command("Start", Command.SCREEN, 1);
private Gauge gauge = new Gauge("Progress Tracking", false, 100, 0);
private boolean isSafeToExit = true;
public GaugeNonInteractive() {
display = Display.getDisplay(this);
form.append(gauge);
form.addCommand(start);
form.addCommand(exit);
form.setCommandListener(this);
}
public void startApp() {
display.setCurrent(form);
}
public void pauseApp() {
}
public void destroyApp(boolean unconditional) throws MIDletStateChangeException {
if (!unconditional) {
throw new MIDletStateChangeException();
}
}
public void commandAction(Command command, Displayable displayable) {
if (command == exit) {
try {
destroyApp(isSafeToExit);
notifyDestroyed();
} catch (MIDletStateChangeException Error) {
Alert alert = new Alert("Busy", "Please try again.", null, AlertType.WARNING);
alert.setTimeout(1500);
display.setCurrent(alert, form);
}
} else if (command == start) {
new Thread(new GaugeUpdater()).start();
}
}
class GaugeUpdater implements Runnable {
public void run() {
isSafeToExit = false;
try {
while (gauge.getValue() < gauge.getMaxValue()) {
Thread.sleep(500);
gauge.setValue(gauge.getValue() + 1);
}
isSafeToExit = true;
gauge.setLabel("Process Completed.");
} catch (InterruptedException Error) {
throw new RuntimeException(Error.getMessage());
}
}
}
}
Gauge Tracker
import javax.microedition.lcdui.rumand;
import javax.microedition.lcdui.rumandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.Gauge;
import javax.microedition.lcdui.Item;
import javax.microedition.lcdui.ItemStateListener;
import javax.microedition.lcdui.StringItem;
import javax.microedition.midlet.MIDlet;
public class GaugeTracker extends MIDlet implements ItemStateListener, CommandListener {
private Gauge mGauge = new Gauge("GaugeTitle", true, 5, 3);
private StringItem mStringItem = new StringItem(null, "[value]");
public GaugeTracker() {
itemStateChanged(mGauge);
}
public void itemStateChanged(Item item) {
if (item == mGauge)
mStringItem.setText("Value = " + mGauge.getValue());
}
public void commandAction(Command c, Displayable s) {
if (c.getCommandType() == Command.EXIT)
notifyDestroyed();
}
public void startApp() {
Form form = new Form("GaugeTracker");
form.addCommand(new Command("Exit", Command.EXIT, 0));
form.setCommandListener(this);
// Now add the selected items.
form.append(mGauge);
form.append(mStringItem);
form.setItemStateListener(this);
Display.getDisplay(this).setCurrent(form);
}
public void pauseApp() {
}
public void destroyApp(boolean unconditional) {
}
}
Interactive Gauge
/*--------------------------------------------------
* InteractiveGauge.java
*
* Example from the book: Core J2ME Technology
* Copyright John W. Muchow http://www.CoreJ2ME.ru
* You may use/modify for any non-commercial purpose
*-------------------------------------------------*/
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
public class InteractiveGauge extends MIDlet implements CommandListener
{
private Display display; // Reference to display object
private Form fmMain; // The main form
private Command cmExit; // Exit the form
private Gauge gaVolume; // Volume adjustment
public InteractiveGauge()
{
display = Display.getDisplay(this);
// Create the gauge and exit command
gaVolume = new Gauge("Sound Level", true, 30, 4);
cmExit = new Command("Exit", Command.EXIT, 1);
// Create form, add commands, listen for events
fmMain = new Form("");
fmMain.addCommand(cmExit);
fmMain.append(gaVolume);
fmMain.setCommandListener(this);
}
// Called by application manager to start the MIDlet.
public void startApp()
{
display.setCurrent(fmMain);
}
public void pauseApp()
{ }
public void destroyApp(boolean unconditional)
{ }
public void commandAction(Command c, Displayable s)
{
if (c == cmExit)
{
destroyApp(false);
notifyDestroyed();
}
}
}
Non Interactive Gauge
/*--------------------------------------------------
* NonInteractiveGauge.java
*
* Example from the book: Core J2ME Technology
* Copyright John W. Muchow http://www.CoreJ2ME.ru
* You may use/modify for any non-commercial purpose
*-------------------------------------------------*/
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import java.util.Timer;
import java.util.TimerTask;
public class NonInteractiveGauge extends MIDlet implements CommandListener
{
private Display display; // Reference to display object
private Form fmMain; // The main form
private Command cmExit; // Exit the form
private Command cmStop; // Stop the download
private Gauge gaProgress; // Progress indicator
private Timer tm; // The Timer
private DownloadTimer tt; // The task to run
public NonInteractiveGauge()
{
display = Display.getDisplay(this);
// Create the gauge, exit and stop command
gaProgress = new Gauge("Download Progress", false, 20, 1);
cmExit = new Command("Exit", Command.EXIT, 1);
cmStop = new Command("Stop", Command.STOP, 1);
// Create the form, add gauge & stop command, listen for events
fmMain = new Form("");
fmMain.append(gaProgress);
fmMain.addCommand(cmStop);
fmMain.setCommandListener(this);
}
// Called by application manager to start the MIDlet.
public void startApp()
{
display.setCurrent(fmMain);
// Create a timer that fires off every 1000 milliseconds
tm = new Timer();
tt = new DownloadTimer();
tm.scheduleAtFixedRate(tt, 0, 1000);
}
public void pauseApp()
{ }
public void destroyApp(boolean unconditional)
{ }
public void commandAction(Command c, Displayable s)
{
if (c == cmExit)
{
destroyApp(false);
notifyDestroyed();
}
else if (c == cmStop)
{
tm.cancel();
fmMain.removeCommand(cmStop);
fmMain.addCommand(cmExit);
gaProgress.setLabel("Download Cancelled!");
}
}
/*--------------------------------------------------
* Process the Timer Task
*-------------------------------------------------*/
private class DownloadTimer extends TimerTask
{
public final void run()
{
// Is current value of gauge less than the max?
if (gaProgress.getValue() < gaProgress.getMaxValue())
gaProgress.setValue(gaProgress.getValue() + 1);
else
{
// Remove stop command and replace with Exit
fmMain.removeCommand(cmStop);
fmMain.addCommand(cmExit);
// Change the gauge label
gaProgress.setLabel("Download Complete!");
// Stop the timer
cancel();
}
}
}
}