Java Embedded Development lecture (Lecture 3)

Source: Internet
Author: User

No matter what new things you want to learn, you must follow a step-by-step approach. I want to introduce how to develop a small application on the Palm by using j2's, and then explore how to develop more complex applications, let me start.

Section 1 obtain

You can download the j2ecldc file from the Sun website. The first file is the j2me_cldc_-1_0-src.winsol.zip, which includes all the class libraries needed to develop CLDC applications. In addition to class libraries, it also includes the source code and executable code in Windows and UNIX for the Java Virtual Machine reference implementation specified by MIDP. Readers who have read the previous articles must know that the Java Virtual Machine mentioned here is the K Virtual Machine (KVM). With this file, during the development process, you can debug programs on your PC without downloading these programs to your handheld devices.

The second file is the j2me_cldc-1_0-src-palm_overlay.zip, which includes the implementation of KVM for a device that runs the Palm operating system, and a tool that generates executable Palm files through a Java compiler. Note that this is an individual CLDC implementation for the Palm operating system. If you use the implementation for other devices, your Java application can also run on other devices.

To develop applications for Palm OS devices, you must install these two Zip files on your computer. In this article, I assume that you have installed the CLDC folder on drive C. Note: To develop CLDC applications, you do not need a specific Java Compilation Program. You can use the J2SE Compilation Program. The only difference is that you must replace it with the path of the J2SE class library during compilation. You also need to use the software included in the downloaded CLDC to process the generated. class file.

Section 2 Application Examples attached to the running document

The files you downloaded from the Sun website contain the source programs and compiled versions of many sample applications. You can use KVM to run these sample applications on your desktop. Running these applications gives you a perceptual knowledge of a CLDC application. Before running these programs, you must start a command line interface and enter the following commands:

Set cldc_classpath = c :;.
Set bin = % bin %; c:
Cd

Now that you have set the environment variables and enter the directory for saving the sample application, you can start running them. For example, to run the UITest program, enter:
Kvm-classpath % cldc_classpath % UITest

You can also check the Pong game program. Enter the following command in the command line: kvm-classpath % cldc_classpath % Pong

Is it fun? You can try to test each sample application. Later, we will use the same method to run the application we developed on the desktop.

Section 3 construct a Palm Application

Well, now you know a little bit about the j2's program, right? Now let's do some practical work. I will introduce how to build a Java application running on the Palm OS device. First of all, although Java is a Java technology, it does not mean that you can use existing Java applications, recompile them, and then run them on new devices. As mentioned in the previous chapters, many of the key features of Java virtual machines used by J2SE and J2EE have been deleted from the J2SE virtual machines because of limited device resources.

The Java Virtual Machine (KVM) with j2m's lack of floating point support; Another important difference between the J2SE and J2EE applications is the lack of priority-type multitasking, although it is not a necessary condition described in the Java specification, almost all machines running your J2SE and J2EE applications support priority-type multitasking. This means that the CPU is shared among multiple Java threads without having to add any special code to your program, while the devices used by j2s often do not have this function. To ensure that multithreading can work, you must call Thread. yield () method.

Another noteworthy aspect of using the CLDC simple table is that the J2SE class library lacks many classes in the J2SE class library. You must use this restricted class library when building an application. When downloading a CLDC file, you can carefully examine the file you obtained and find out which class is included in the CLDC specification. The last problem you have to deal with is that the CLDC class library lacks access to user interface elements-buttons, menus, and graphics. If you do not use the Palm user interface, you cannot develop applications for the Palm OS device. Sun has solved this problem by using a separate class library com. sun. kjava provides you with the permission to access the base-level Palm user interface. The following example uses the class library to create a window, button, and dialog box. However, note that the kjava class library is neither MIDP configuration nor the official part of the CLDC simple table, and may change or disappear in future versions. This means that you use any program written in the kjava class library. Once you release a new j2_version, You have to modify this program to adapt to the new j2_version. But now, it is the only option for developing applications using the Palm user interface.

Import lang .*;
Import com. sun. kjava .*;

Public class HelloWorld extends Spotlet

Implements DialogOwner {

Private Button quitButton;
Private Button aboutButton;
Private TextBox welcomeTextBox;

Private final String welcomeString = "Hello World ";
Private final String aboutTitleString = "About Hello World ";
Private final String aboutString = "HelloWorld j2-cldc Application ";

Public HelloWorld (){
Register (WANT_SYSTEM_KEYS );

WelcomeTextBox = new TextBox (welcomeString, 10, 10,140,130 );
AboutButton = new Button ("About", 75,140 );
AboutButton. setEnabled (true );
QuitButton = new Button ("Quit", 125,140 );
QuitButton. setEnabled (true );
Paint ();
}

Public void dialogDismissed (String title ){
Graphics. clearScreen ();
Register (WANT_SYSTEM_KEYS );
Paint ();
}

Public void paint (){
WelcomeTextBox. paint ();
AboutButton. paint ();
QuitButton. paint ();
}

Public void penDown (int x, int y ){
If (aboutButton. pressed (x, y )){
Dialog aboutDialog = new Dialog (this, aboutTitleString, aboutString, "OK ");
AboutDialog. showDialog ();
}
If (quitButton. pressed (x, y ))
System. exit (0 );
}

Public static void main (String args []) {
New HelloWorld ();
}
}

Analyze the HelloWorld Application

The preceding HelloWorld application has the basic features of CLDC applications on devices running Palm OS. When you carefully examine this code, you will find many features of compiling applications using the kjava class library. First, this program extends com. sun. kjava. spotlet class, using kjava, A spotlet is any class that can get the focus, that is, receiving events from the system, such as the touch of a pen. In our example, when the pen clicks The About and Quit buttons, The HelloWorld application needs to respond, so it needs to get the focus. Your application can include any number of classes derived from spotlet, although only one of them can get the focus at any time. In fact, the application uses focus by calling the register method. We use register in both the beginning part of the HelloWorld constructor and the dialogDismissed method.

The HelloWorld class implements the DialogOwner interface. The class that implements the DailogOwner interface is used to create a dialog box. Our application has created a dialog box to respond to the event when the pen clicks The About button. To implement the DialogOwner interface, your class must contain a dialogDismissed member method. In order to implement the DialogOwner interface, your class must contain a dialogDismissed member method, which is called at the time when a dialog box is closed.

The HelloWorld constructor creates three interface elements on the screen: a text box and two buttons. You will immediately find an important difference between kjava and J2SE AWT: kjava is not equivalent to AWT's Automatic Layout mechanism. You must encode it to determine the position of each interface element on the screen. In our program, we place the text box between (140,130) and (75,140). The two buttons are placed in (125,140) and ).

Whenever a user clicks a pen on the device, the HelloWorld penDown method is called. We call the pressed method of each button to determine whether this click is within the button's boundary. If the button's pressed method returns a true value, it means that the trigger click event occurs within the button's boundary, the corresponding action of this button will occur. For example, if you click the "About" button, you can create a Dialog box by creating a new Dialog object. If you click the Quit button, we will terminate the program.

Finally, when you close the dialog box, call the dialogDismissed method of HelloWorld. In our application, this method is used to restore the focus by calling the register Method and calling the paint method to refresh the screen.

Compile

Compiling a CLDC application requires two steps: first, you must compile and run them through the standard J2SE, but use the CLDC class library instead of the standard J2SE class library. Then, use the preverify program to convert the obtained. class file into a KVM-readable Java application.

The preceding sample program is compiled as follows:

Javac-g: none-classpath % cldc_classpath %-d % temp % HelloWorld. java

Preverify-classpath % cldc_classpath %-s % temp %-d.

Once compiled, you can run the program on your desktop using the following command:

Kvm-classpath % cldc_classpath % HelloWorld

Since this CLDC application can run on a computer, you can also try to convert it into a Palm executable file.

Run on Palm

To convert a. class file developed using CLDC to a file that can be run on the Palm OS, you must use many programs in j2me_cldc-1_0-src-palm_overlay.zip. First, you must convert your. class file to a Palm OS application. All Palm OS applications have the extension. prc. By using palm. database. MakePalm

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.