"Introduction to Java Mobile/PDA Programming" Reading notes 8--lcdaui thread time control of low-level APIs

Source: Internet
Author: User
Tags diff thread
Notes | procedures | control | design
Use thread to resolve problems that paint () and user input responses will do at the same time.

public class MyCanvas extends Canvas implements runnable{int r=0, public mycanvas () {thread t=new thread (this); T.start ();   public void Run () {while (true) {r++;   if (r>10) r=0;  Repaint ();  } public void Paint (Graphics g) {clear (g); Paintanimation (G,100,10,R);    } protected void keypressed (int keycode) {switch (getgameaction (keycode)) {case canvas.up:y=y-2;   Break ...... } }}

J2ME has removed the Stop () method (J2SE is no longer advocated), we use the identity (flag) to solve the problem of thread end Boolean conti=false;......if (Cmd.equals ("Stop")) {conti=false; Removecommand (stop); AddCommand (start); else if (cmd.equals ("start")) {Removecommand (start); AddCommand (stop); conti=true; Thread T=new thread (this); T.start ();}  ... public void run () {while (conti) {r++;  if (r>10) r=0; Repaint (); }}

Time control different machine processing speed is different, in order to maintain the game, the picture consistency, through the time control process to solve ... int rate=50 (50 millisecond =1/20 seconds screen redraw) public void run () {long s1=0; long s2=0; long diff=0;  while (Conti) {s1=systen.currenttimemillis ();  Repaint ();  Servicerepaints ();  S2=systen.currenttimemillis ();  DIFF=S2-S1;  System.out.rpintln (diff);   if (diff<rate) {try {thread.sleep (Rate-diff); }catch (Exception exc) {}} repaint (); }}


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.