Java Tutorial/J2ME/Gauge

Материал из Java эксперт
Версия от 17:44, 31 мая 2010; (обсуждение)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

Add Gauge to a form

import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.Gauge;
import javax.microedition.midlet.MIDlet;
public class GaugeMIDlet extends MIDlet {
  protected Display display;
  protected void startApp() {
    display = Display.getDisplay(this);
    Form form = new Form("Demo");
    form.append("line");
    form.append(new Gauge(null, true, 100, 50));
    display.setCurrent(form);
  }
  protected void pauseApp() {
  }
  protected void destroyApp(boolean unconditional) {
  }
}





Gauge Interactive

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 J2MEGaugeInteractive extends MIDlet implements CommandListener {
  private Display display;
  private Form form;
  private Command exit;
  private Command vote;
  private Gauge gauge;
  public J2MEGaugeInteractive() {
    display = Display.getDisplay(this);
    gauge = new Gauge("Rate the movie: ", true, 5, 1);
    exit = new Command("Exit", Command.EXIT, 1);
    vote = new Command("Vote", Command.SCREEN, 1);
    form = new Form("");
    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

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 J2MEGaugeNonInteractive extends MIDlet implements CommandListener {
  private Display display;
  private Form form;
  private Command exit;
  private Command start;
  private Gauge gauge;
  private boolean isSafeToExit;
  public J2MEGaugeNonInteractive() {
    display = Display.getDisplay(this);
    gauge = new Gauge("Progress Tracking", false, 100, 0);
    exit = new Command("Exit", Command.EXIT, 1);
    start = new Command("Start", Command.SCREEN, 1);
    form = new Form("");
    form.append(gauge);
    form.addCommand(start);
    form.addCommand(exit);
    form.setCommandListener(this);
    isSafeToExit = true;
  }
  public void startApp() {
    display.setCurrent(form);
  }
  public void pauseApp() {
  }
  public void destroyApp(boolean unconditional) throws 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) {
      form.removeCommand(start);
      new Thread(new GaugeUpdater()).start();
    }
  }
  class GaugeUpdater implements Runnable {
    public void run() {
      isSafeToExit = false;
      try {
        while (gauge.getValue() < gauge.getMaxValue()) {
          Thread.sleep(1000);
          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 J2MEGaugeTracker extends MIDlet implements ItemStateListener, CommandListener {
  private Gauge mGauge = new Gauge("GaugeTitle", true, 5, 3);
  private StringItem mStringItem = new StringItem(null, "[value]");
  public J2MEGaugeTracker() {
    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);
    form.append(mGauge);
    form.append(mStringItem);
    form.setItemStateListener(this);
    Display.getDisplay(this).setCurrent(form);
  }
  public void pauseApp() {
  }
  public void destroyApp(boolean unconditional) {
  }
}





new Gauge(null, false,

import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.Gauge;
import javax.microedition.midlet.MIDlet;
public class GaugeFalseMIDlet extends MIDlet {
  protected Display display;
  protected void startApp() {
    display = Display.getDisplay(this);
    Form form = new Form("Demo");
    form.append("line");
    form.append(new Gauge(null, false, 100, 50));
    display.setCurrent(form);
  }
  protected void pauseApp() {
  }
  protected void destroyApp(boolean unconditional) {
  }
}





Use active and inactive Gauge

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 J2MEActiveInactiveGaugeMIDlet extends MIDlet implements CommandListener {
  private Command exitCommand = new Command("exit", Command.EXIT, 1);
  private Gauge activeGauge = new Gauge("active:", true, 10, 2);
  private Gauge inactiveGauge = new Gauge("inactive:", false, 10, 4);
  private Display display = Display.getDisplay(this);
  public void startApp() {
    Form aForm = new Form("Gauge");
    aForm.append(activeGauge);
    aForm.append(inactiveGauge);
    aForm.addCommand(exitCommand);
    aForm.setCommandListener(this);
    display.setCurrent(aForm);
  }
  public void pauseApp() {
  }
  public void destroyApp(boolean unconditional) {
  }
  public void commandAction(Command c, Displayable s) {
    if (c == exitCommand) {
      destroyApp(false);
      notifyDestroyed();
    }
  }
}