The ImageViewerMIDlet
class is the MIDlet main
class. When the MIDlet is launched, the ImageViewerMIDlet
class is loaded first.
The ImageViewerMIDlet
class handles the following
tasks:
Common startup events such as setting the default Display
Displaying the splash screen as the MIDlet launches
Common pauseApp
and destroyApp
tasks
Transitions between different MIDlet views, such as launching
the FileSelector
To implement the ImageViewerMIDlet
class:
Create the ImageViewerMIDlet.java
class file.
Import the required classes.
import javax.microedition.lcdui.*; import javax.microedition.midlet.*;
Implement the ImageViewerMIDlet
class functionality.
public class ImageViewerMIDlet extends MIDlet { private final Image logo; private final ImageCanvas imageCanvas; private FileSelector fileSelector; private final InputScreen inputScreen; private int operationCode = -1; public ImageViewerMIDlet() { // init basic parameters logo = makeImage("/logo1.png"); ErrorScreen.init(logo, Display.getDisplay(this)); imageCanvas = new ImageCanvas(this); fileSelector = new FileSelector(this); inputScreen = new InputScreen(this); } public void startApp() { Displayable current = Display.getDisplay(this).getCurrent(); if (current == null) { // Checks whether the API is available boolean isAPIAvailable = System.getProperty( "microedition.io.file.FileConnection.version") != null; // shows splash screen if (!isAPIAvailable) { String text = getAppProperty("MIDlet-Name") + "\n" + getAppProperty("MIDlet-Vendor") + "\nFile Connection API is not available"; Alert splashScreen = new Alert(null, text, logo, AlertType.INFO); Display.getDisplay(this).setCurrent(splashScreen); } else { Display.getDisplay(this).setCurrent(fileSelector); fileSelector.initialize(); } } else { Display.getDisplay(this).setCurrent(current); } } public void pauseApp() { } public void destroyApp(boolean unconditional) { // stop the commands queue thread fileSelector.stop(); notifyDestroyed(); } void fileSelectorExit() { destroyApp(false); } void cancelInput() { Display.getDisplay(this).setCurrent(fileSelector); } void input(String input) { fileSelector.inputReceived(input, operationCode); Display.getDisplay(this).setCurrent(fileSelector); } void displayImage(String imageName) { if (imageCanvas.displayImage(imageName) == true) { Display.getDisplay(this).setCurrent(imageCanvas); } } void displayFileBrowser() { Display.getDisplay(this).setCurrent(fileSelector); } void showError(String errMsg) { ErrorScreen.showError(errMsg, fileSelector); } void showError(Exception e) { ErrorScreen.showError(e.getMessage(), fileSelector); } void showMsg(String text) { Alert infoScreen = new Alert(null, text, logo, AlertType.INFO); infoScreen.setTimeout(3000); Display.getDisplay(this).setCurrent(infoScreen, fileSelector); } Image getLogo() { return logo; } void requestInput(String text, String label, int operationCode) { inputScreen.setQuestion(text, label); this.operationCode = operationCode; Display.getDisplay(this).setCurrent(inputScreen); } // loads a given image by name static Image makeImage(String filename) { Image image = null; try { image = Image.createImage(filename); } catch (Exception e) { // use a null image instead } return image; } }
Now that you have implemented the MIDlet main class, implement an operations queue for more effective user action handling.