How to use the threads in J2ME

Source: Internet
Author: User
Tags constant definition netbeans

Threads are an integral part of J2ME development, J2ME inherits Runnable interfaces in J2se about Java.lang, and thread classes. However, due to the particularity of J2ME application, there is no concept of thread group and no daemon thread in the J2ME program except for some APIs.

Today, we start with an example to learn the concepts of threads in J2ME. The example we chose is Tetris. First, there are a few things to note:

1. Note that you should note that you do not use floating-point numbers in J2ME, so that you can compile them, but you cannot pass the pre validation. Because the general handheld device can not afford the high load of floating-point operations.

2. In the J2ME program, the vast majority of space for the image of possession, we can see that today our example does not have any pictures, only 5k, if it is the development of products, the inevitable use of pictures,

But try to use a PNG image with high compression rates, and don't be too complicated, because complex pictures can make the picture big.

3. Use constants, especially positional information, as much as possible in your program, so that when you modify it, you can change it, and when you migrate to other platforms, you will reduce a lot of work. and the color.

Information, and so on. Do not use each memory, reconstruct, because the color in J2ME and J2SE is not the same. There is no constant definition.

4. Game products often need to protect copyright, and many of today's decompile tools can easily decompile the contents of the jar file, so you can obfuscate the program, so that cannot decompile

Or it cannot be understood after the decompile. You can right-click the item and select build| in the attribute obfuscating, select the level of ambiguity.

5. We all use NetBeans as a development platform in the tutorial, please visit www.netbeans.org for installation.

All right, let's get started.

A. First, create a new mobile application project, named Tetris, do not automatically create Hello program, select MIDP1.0 and CLDC1.0.

B. Create a new package by right-clicking the item and selecting new| Java Package, named Tetris.

C. Create a new MIDlet, ditto, select new| Java Midlet, named Tetrismidlet.

D. We need a canvas that can display the game, so create a new class called Tetriscanvas, and in Tetrismidlet.java will be tetriscanvas as the currently visible element:

Now the Tetrismidlet.java are as follows:

package Tetris;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
/**
*
* @author lin
* @version
*/
public class TetrisMidlet extends MIDlet {
public void startApp() {
Display display = Display.getDisplay( this );
// TetrisCanvas extends Canvas which extends Displayable so it can
// be displayed directly
display.setCurrent( new TetrisCanvas());
}
public void pauseApp() {
}
public void destroyApp(boolean unconditional) {
}
}

Because Tetriscanvas inherits canvas, it can be displayed by Tetrismidlet.

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.