以文本方式查看主题

-  课外天地 李树青  (http://njcie.com/bbs/index.asp)
--  JavaME移动开发课件  (http://njcie.com/bbs/list.asp?boardid=18)
----  程序代码——采用多线程制作的变色跳跃小球  (http://njcie.com/bbs/dispbbs.asp?boardid=18&id=555)

--  作者:admin
--  发布时间:2008/10/8 22:50:59
--  程序代码——采用多线程制作的变色跳跃小球

import java.util.Random;

import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Graphics;
import javax.microedition.midlet.MIDlet;

public class Exec extends MIDlet {
        private Display display;

        public Exec() {
                display = Display.getDisplay(this);
        }

        public void startApp() {

                display.setCurrent(new DrawPanel());
        }

        public void pauseApp() {
        }

        public void destroyApp(boolean unconditional) {
        }
}

class DrawPanel extends Canvas {
        final int size = 40;
        int previousX = getWidth() / 2 - size / 2;
        int previousY = getHeight() / 2 - size / 2;
        int currentX = previousX;
        int currentY = previousY;
        int xStep = 1;
        int yStep = 1;
        int rColor = 0;
        int gColor = 0;
        int bColor = 0;

        Thread thread = new Thread(new ActionThread());

        public DrawPanel() {
                thread.start();
        }

        public void paint(Graphics g) {
                g.setColor(255, 255, 255);
                g.fillArc(previousX, previousY, size, size, 0, 360);

                g.setColor(rColor, gColor, bColor);
                g.fillArc(currentX, currentY, size, size, 0, 360);
        }

        class ActionThread implements Runnable {
                public void run() {
                        while (true) {
                                Random rand = new Random();

                                // 获取一个标识变量,如为0,则将当前红色部分加变暗,如为1,则将当前红色部分加亮
                                int rFlag = (int) (rand.nextFloat() * 2);

                                // 设置红色部分的颜色值,每次减少或者增加的颜色值为5,同时程序还能防止颜色值出界
                                if (rFlag == 0) {
                                        rColor -= 5;
                                        rColor = (rColor < 0) ? (rColor + 10) : rColor;
                                } else {
                                        rColor += 5;
                                        rColor = (rColor > 255) ? (rColor - 10) : rColor;
                                }

                                // 获取一个标识变量,如为0,则将当前绿色部分加变暗,如为1,则将当前绿色部分加亮
                                int gFlag = (int) (rand.nextFloat() * 2);

                                // 设置绿色部分的颜色值,每次减少或者增加的颜色值为5,同时程序还能防止颜色值出界
                                if (gFlag == 0) {
                                        gColor -= 5;
                                        gColor = (gColor < 0) ? (gColor + 10) : gColor;
                                } else {
                                        gColor += 5;
                                        gColor = (gColor > 255) ? (gColor - 10) : gColor;
                                }

                                // 获取一个标识变量,如为0,则将当前蓝色部分加变暗,如为1,则将当前蓝色部分加亮
                                int bFlag = (int) (rand.nextFloat() * 2);

                                // 设置前色部分的颜色值,每次减少或者增加的颜色值为5,同时程序还能防止颜色值出界
                                if (bFlag == 0) {
                                        bColor -= 5;
                                        bColor = (bColor < 0) ? (bColor + 10) : bColor;
                                } else {
                                        bColor += 5;
                                        bColor = (bColor > 255) ? (bColor - 10) : bColor;
                                }

                                previousX = currentX;
                                previousY = currentY;

                                // 如果当前跳动小球已经超出绘制区域的右边界,则生成一个新的随机移动步长,开始向反方向移动小球
                                if (currentX > getWidth() - size) {
                                        xStep = -(int) (rand.nextFloat() * 5) - 1;
                                }

                                // 如果当前跳动小球已经超出绘制区域的下边界,则生成一个新的随机移动步长,开始向反方向移动小球
                                if (currentY > getHeight() - size) {
                                        yStep = -(int) (rand.nextFloat() * 5) - 1;
                                }

                                // 如果当前跳动小球已经超出绘制区域的左边界,则生成一个新的随机移动步长,开始向反方向移动小球
                                if (currentX < 0) {
                                        xStep = (int) (rand.nextFloat() * 5) + 1;
                                }

                                // 如果当前跳动小球已经超出绘制区域的上边界,则生成一个新的随机移动步长,开始向反方向移动小球
                                if (currentY < 0) {
                                        yStep = (int) (rand.nextFloat() * 5) + 1;
                                }

                                // 调整跳动小球的位置
                                currentX += xStep;
                                currentY += yStep;

                                repaint();

                                try {
                                        Thread.sleep(10);
                                } catch (Exception e) {
                                        System.out.println(e.getMessage());
                                }
                        }
                }
        }
}

[此贴子已经被作者于2010-12-12 18:34:36编辑过]