Thursday, August 15, 2013

Create an Application using Java

You have a project now, you can't run it until it contains an application. In the world of CLDC/MIDP, applications are called MIDlets. Control-click on the project and choose New > MIDlet... from the popup menu.



Fill in values for MIDlet Name, which will be visible to the user, MIDP Class Name, and Package. In this example, the name is Swoosh, the class name is SwooshMIDlet, and the package is awesome.

Click on Finish. The Java ME SDK creates the new file, adds it to your project, and shows it to you in the source code editor.

The source code you get will build, but it doesn't do anything.

Replace the source code with the following example:
package awesome;

import javax.microedition.midlet.*;

import com.sun.lwuit.*;
import com.sun.lwuit.animations.*;
import com.sun.lwuit.events.*;
import com.sun.lwuit.layouts.BoxLayout;
import com.sun.lwuit.plaf.*;
import java.util.Hashtable;

public class SwooshMIDlet extends MIDlet implements ActionListener {
    Form mHomeForm;
    Form mAwayForm;

    Command mExitCommand;

    Button mSlideButton;
    Button mFadeButton;
    Button mCubeButton;
    Button mRotateButton;

    Command mBackCommand;

    public void startApp() {
      Display.init(this);

      installTheme();

      createUI();

      mHomeForm.show();
    }

    public void pauseApp() {
    }

    public void destroyApp(boolean unconditional) {
    }

    public void actionPerformed(ActionEvent ae) {
        if (ae.getSource() == mSlideButton) {
            mAwayForm.setTransitionInAnimator(
                CommonTransitions.createSlide(
                    CommonTransitions.SLIDE_HORIZONTAL, false, 200));
            mAwayForm.setTransitionOutAnimator(
                CommonTransitions.createSlide(
                    CommonTransitions.SLIDE_HORIZONTAL, true, 200));
            mAwayForm.show();
        }
        else if (ae.getSource() == mFadeButton) {
            mAwayForm.setTransitionInAnimator(
                CommonTransitions.createFade(1000));
            mAwayForm.setTransitionOutAnimator(
                CommonTransitions.createFade(200));
            mAwayForm.show();
        }
        else if (ae.getSource() == mCubeButton) {
            mAwayForm.setTransitionInAnimator(
                Transition3D.createCube(400, false));

            mAwayForm.setTransitionOutAnimator(
                Transition3D.createCube(400, true));

            mAwayForm.show();
        }
        else if (ae.getSource() == mRotateButton) {
            mAwayForm.setTransitionInAnimator(
                Transition3D.createRotation(400, true));

            mAwayForm.setTransitionOutAnimator(
                Transition3D.createRotation(400, false));

            mAwayForm.show();
        }
        else if (ae.getSource() == mBackCommand) {
            mHomeForm.show();
        }
        else if (ae.getCommand() == mExitCommand)
          notifyDestroyed();
    }

    private void installTheme() {
        // This is not the normal way to do this.
        // Usually you load a theme from a file.
        UIManager uim = UIManager.getInstance();
        Hashtable ht = new Hashtable();
        ht.put("sel#" + Style.BG_COLOR, "660099");
        ht.put(Style.BG_COLOR, "330066");
        ht.put(Style.FG_COLOR, "cccccc");
        uim.setThemeProps(ht);
    }

    private void createUI() {
      // Set up screen for transitions.
      mAwayForm = new Form("Away");
      mAwayForm.addComponent(new Label("Choose Back to return to the home screen."));

      mBackCommand = new Command("Back");
      mAwayForm.addCommand(mBackCommand);
      mAwayForm.addCommandListener(this); // Use setCommandListener() with LWUIT 1.3 or earlier.

      // Set up main screen.
      mHomeForm = new Form("Swoosh!");
      mHomeForm.setLayout(new BoxLayout(BoxLayout.Y_AXIS));

      mSlideButton = new Button("Slide");
      mSlideButton.addActionListener(this);
      mHomeForm.addComponent(mSlideButton);

      mFadeButton = new Button("Fade");
      mFadeButton.addActionListener(this);
      mHomeForm.addComponent(mFadeButton);

      mCubeButton = new Button("Cube");
      mCubeButton.addActionListener(this);
      mHomeForm.addComponent(mCubeButton);

      mRotateButton = new Button("Rotate");
      mRotateButton.addActionListener(this);
      mHomeForm.addComponent(mRotateButton);

      mExitCommand = new Command("Exit");
      mHomeForm.addCommand(mExitCommand);
      mHomeForm.addCommandListener(this); // Use setCommandListener() with LWUIT 1.3 or earlier.
    }
}
The MIDlet contains methods that get called by the Java platform on the device. For example, when a user chooses your application from a menu, the Java platform calls your MIDlet's startApp() method to get things rolling.

SwooshMIDlet demonstrates a few of LWUIT's animated transitions. It consists of the following methods:
  • startApp(), pauseApp(), and destroyApp() are methods that all MIDlets must have.
  • actionPerformed() is called when the user presses one of the buttons or chooses a command.
  • installTheme() gives LWUIT some hints about how to draw components.
  • createUI() constructs the forms and components of the application.

No comments:

Post a Comment