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 (); }}
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.