This is the main class of the MIDlet. It controls the display and handles the transitions between the different screens.
On the startApp
call, ImageViewerMIDlet
checks that the FileConnection API is supported by the device. If the API
is not supported, ImageViewerMIDlet
informs the
user accordingly. If the API is supported, ImageViewerMIDlet
creates a FileSelector
and assigns it to the Display
.
The requestInput
and input
methods are used to show the InputScreen
and indicate the input result,
respectively. The displayImage
method is used
to show the ImageCanvas
displaying the selected image.
To create this class:
Create the ImageViewerMIDlet
class file.
Import the required packages.
import javax.microedition.lcdui.*; import javax.microedition.midlet.*;
Implement the ImageViewerMIDlet
class.
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 the MIDlet functionality.