Multi-threaded examples in MIDP

xiaoxiao2021-03-06  109

The MIDP itself has many threads examples, such as event listeners in the user interface. Threads can be used in the MIDP application design. For example, design animations, may require multiple threads to operate at the Canvas object. Another typical application is that in the network program design, such as an application connection network, obtaining data may take a long time, then multi-thread technology can be utilized to allow users to operate at the same time. Or design an animation screen, prompt is performing networking operations, which is important, otherwise the user does not know the working status of the application, this is an important aspect of application availability. Threads can also be used in background, such as setting up a thread monitoring a state (can be stock information from the network, or mail information, etc.), when a certain condition is triggered, the user is warned in a ALERT. This example, the prototype of the example is the last example of 7.8.9 Section 7.8.9 in Chapter 7, "Advanced User Interface", but here is implemented using a low-level user interface API. (Author Note: This example is an example of "J2ME Wireless Communication Application Technology Development" (Beijing Hope Electronics Press 2002 Edition) AnimatorCanvas class implements a user-defined Canvas object, and the Runnable interface is implemented. The Image array holds different frames in the animation. Its PAINT method first refreshes the screen, then draw the current frame at the specified position and set the current frame to the next element in the IAMGE array, and set to the first element when the last element is reached. Image array is acquired from outside through constructor, and the interval parameter specifies the time interval between animation frames. Its RUN method is to circulate the Repaint method, and since each heavy draw is the next frame, the animation effect is achieved.

Code: // name of the program AnimatorCanvas.java, project Animator // use threading to achieve animation import javax.microedition.midlet *; import javax.microedition.lcdui *; public class AnimatorCanvas extends Canvas implements Runnable {Image [] frames;.. int left, top; boolean alive; int interval; int currentFrame; int numFrames; MIDlet midlet; Thread thread; public AnimatorCanvas (MIDlet midlet, Image [] frames, int left, int top, int interval) {this.frames = frames; THIS.TOP = TOP; this.TOP = TOP; this.alive = interval; this.alive = true; this.currentframe = 0; this.numframes = frames.length; this.midlet = MIDlet; thread = new thread (this ); thread.start (); public Animatorcanvas (MIDlet MIDLET, Image [] frames, int interval {this (MIDlet, frames, 0, 0, interval); this.Left = (this.Getwidth () - frames [ 0] .Getwidth ()) / 2; this.top = (this.getHeight () - frames [0] .getHeight ()) / 2;} public void Paint (graphics g) {g.setcolor (0x00000000); g .fillRect (0, 0, getWidth (), getHeight ()); g.drawImage (Frames [CurrentFrame ], Left, Top, Graphics.Left | graphics.top); IF (Cur rentFrame> = numFrames) {currentFrame = 0;}} public void keyPressed (int keyCode) {if (getGameAction (keyCode) == FIRE) {alive = alive;!} else {thread = null; ((Animator) midlet). Exit ();}} public void Run () {while {if (alive) {repaint (); try {thread.sleep (interval);} catch (interruptedExcection e) {}}}}} in RUN method In the While loop, use the Alive parameter to control it.

转载请注明原文地址:https://www.9cbs.com/read-100573.html

New Post(0)