Implementing UI classes

ImageCanvas class

This class is used to display an image on the screen. The background has previously been set to black and the image is displayed in the center. The image is drawn with its original dimensions and no attempt is made to scale it if it is bigger than the screen. ImageCanvas listens for any key presses and returns the control to the MIDlet.

To create this class:

  1. Create the ImageCanvas class file.

  2. Import the required classes.

    import java.io.*;
    import javax.microedition.io.*;
    import javax.microedition.io.file.*;
    import javax.microedition.lcdui.*;
  3. Implement the class ImageCanvas.

    // This class displays a selected image centered on the screen
    class ImageCanvas
      extends Canvas
    {
      private final ImageViewerMIDlet midlet;
      private static final int CHUNK_SIZE = 1024;
      private Image currentImage = null;
    
      ImageCanvas(ImageViewerMIDlet midlet)
      {
        this.midlet = midlet;
      }
  4. Use the method displayImage() to display the image on screen. In order to access the file system and to load the image into memory and then to display the image on screen, also create an instance of the FileConnection interface, provided by the FileConnection API.

    Call the fileConn.close() method to close the connection. Calling fileConn.close() after the file has been accessed is important to ensure that it is available for other applications. For more information on file connections and file connection security, see the FileConnection interface.

      public void displayImage(String imgName)
      {
        try
        {
          FileConnection fileConn =
            (FileConnection)Connector.open(imgName, Connector.READ);
          // load the image data in memory
          // Read data in CHUNK_SIZE chunks
          InputStream fis = fileConn.openInputStream();
          long overallSize = fileConn.fileSize();
    
          int length = 0;
          byte[] imageData = new byte[0];
          while (length < overallSize)
          {
            byte[] data = new byte[CHUNK_SIZE];
            int readAmount = fis.read(data, 0, CHUNK_SIZE);
            byte[] newImageData = new byte[imageData.length + CHUNK_SIZE];
            System.arraycopy(imageData, 0, newImageData, 0, length);
            System.arraycopy(data, 0, newImageData, length, readAmount);
            imageData = newImageData;
            length += readAmount;
          }
    
          fis.close();
          fileConn.close();
          if (length > 0)
          {
            currentImage = Image.createImage(imageData, 0, length);
          }
          repaint();
        }
        catch (IOException e)
        {
          midlet.showError(e);
        }
        catch (SecurityException e)
        {
          midlet.showError(e);
        }
        catch (IllegalArgumentException e)
        {
          // thrown in case the file format is not understood
          midlet.showError(e);
        }
      }
      protected void paint(Graphics g)
      {
        int w = getWidth();
        int h = getHeight();
        // Set background color to black
        g.setColor(0x00000000);
        g.fillRect(0, 0, w, h);
        if (currentImage != null)
        {
          g.drawImage(currentImage,
            w / 2,
            h / 2,
            Graphics.HCENTER | Graphics.VCENTER);
        }
        else
        {
          // If no image is available, display a message
          g.setColor(0x00FFFFFF);
          g.drawString("No image",
            w / 2,
            h / 2,
            Graphics.HCENTER | Graphics.BASELINE);
        }
      }
      protected void keyReleased(int keyCode)
      {
        // Exit with any key
        midlet.displayFileBrowser();
      }
    }

InputScreen class

InputScreen is a simple screen used to enter text. It is used in this application to enter text for creating new directories and renaming files. The screen sets an input text field and returns the results to the MIDlet.

To create this class:

  1. Create the InputScreen class file.

  2. Import the required classes.

    import javax.microedition.lcdui.*;
  3. Create the implementation of the input screen.

    // This class displays an input field on the screen
    // and returns the value entered to the MIDlet
    class InputScreen
      extends Form
      implements CommandListener
    {
      private final ImageViewerMIDlet midlet;
      private final TextField inputField =
            new TextField("Input", "", 32, TextField.ANY);
      private final Command okCommand =
            new Command("OK", Command.OK, 1);
      private final Command cancelCommand =
            new Command("Cancel", Command.OK, 1);
      InputScreen(ImageViewerMIDlet midlet)
      {
        super("Input");
        this.midlet = midlet;
        append(inputField);
        addCommand(okCommand);
        addCommand(cancelCommand);
        setCommandListener(this);
      }
      public void setQuestion(String question, String text)
      {
        inputField.setLabel(question);
        inputField.setString(text);
      }
      public String getInputText()
      {
        return inputField.getString();
      }
      public void commandAction(Command command, Displayable d)
      {
        if (command == okCommand)
        {
          midlet.input(inputField.getString());
        }
        else if (command == cancelCommand)
        {
          midlet.cancelInput();
        }
      }
    }

ErrorScreen class

ErrorScreen is a simple class used for reporting errors to the end user.

To create this class:

  1. Create the ErrorScreen class file.

  2. Import the required classes.

    import javax.microedition.lcdui.*;
  3. Create the implementation of the error screen.

    class ErrorScreen
      extends Alert
    {
      private static Image image;
      private static Display display;
      private static ErrorScreen instance = null;
    
      private ErrorScreen()
      {
        super("Error");
        setType(AlertType.ERROR);
        setTimeout(5000);
        setImage(image);
      }
    
      static void init(Image img, Display disp)
      {
        image = img;
        display = disp;
      }
    
      static void showError(String message, Displayable next)
      {
        if (instance == null)
        {
          instance = new ErrorScreen();
        }
        instance.setTitle("Error");
        instance.setString(message);
        display.setCurrent(instance, next);
      }
    }