郭克华手机编程教学视频----我的练习源码(20)案例2:绘制一个红色的小球,让其慢慢下落,然后又弹起来

来源:互联网 发布:sql group by sum 编辑:程序博客网 时间:2024/04/29 00:06

 /*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package lession14;

import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Graphics;
import javax.microedition.midlet.*;

/**
 * 案例2:绘制一个红色的小球,让其慢慢下落,然后又弹起来
 * 额外要求:增加暂停和继续功能
 * @author mouyong
 */
public class BallDownMidlet extends MIDlet {
    private BallCanvas myCan=new BallCanvas();
    private Display dis=Display.getDisplay(this);
    private Thread thGame=null;
   
    Command cmdPause=new Command("暂停", Command.SCREEN, 1);
    Command cmdContinue=new Command("继续", Command.SCREEN, 1);
    public void startApp() {
        thGame=new Thread(myCan);
        thGame.start();//启动线程
        dis.setCurrent(myCan);
        //默认先加载暂停按钮
        myCan.addCommand(cmdPause);

        //监听
        myCan.setCommandListener(new CommandListener(){

            public void commandAction(Command c, Displayable d) {
                //如果是暂停按钮
                if(c==cmdPause){
                    //替换按钮为继续
                    myCan.removeCommand(cmdPause);
                    myCan.addCommand(cmdContinue);
                  
                        //暂停线程
                        myCan.pause();
                  
                }else{
                     //否则替换按钮为暂停
                    myCan.removeCommand(cmdContinue);
                    myCan.addCommand(cmdPause);
                    //重新启动线程
                    myCan.restart(myCan);
                }
            }
        });
    }

    public void pauseApp() {
    }

    public void destroyApp(boolean unconditional) {
    }

    //实现Runnable接口,使用多线程,让小球不断掉下
    class BallCanvas extends Canvas implements Runnable{
        private int x=100;
        private int y=0;
        private int radius=50;//半径
        private static final int JUMP_UP=-10;//指示向上运动
        private static final int JUMP_DOWN=10;//指示向下运动
        private int aspact=JUMP_DOWN;//默认的运动方向为向下
        private boolean run=true;//使用这个变量来控制线程的停止与启动

        public void setRun(boolean run) {
            this.run = run;
        }
        public void pause(){
            run=false;
        }

        public void restart(BallCanvas myCan){
            myCan.setRun(true);
            thGame=new Thread(myCan);
            thGame.start();
        }
        protected void paint(Graphics g) {
            //清空面板
            g.setColor(0xffffff);
            g.fillRect(0, 0, this.getWidth(), this.getHeight());
             //绘制小球阴影,有点立体感
            g.setColor(0xcccccc);
            g.fillArc(x+2, y+2, radius, radius, 0, 360);
            //绘制小球
            g.setColor(0xff0000);
            g.fillArc(x, y, radius, radius, 0, 360);
          
        }

        public void run() {
            while(run){//死循环

                //如果下落超出了下边界,就改为弹起
                if((y+radius)>=this.getHeight()){
                    aspact=JUMP_UP;
                }else if(y<=0){
                     aspact=JUMP_DOWN;
                }
                y+=aspact;
                try {
                    Thread.sleep(100);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                repaint();//不断重绘
            }
        }

    }
}

原创粉丝点击