课外天地 李树青学习天地JavaME移动开发课件 → 程序代码——钟表


  共有15249人关注过本帖树形打印复制链接

主题:程序代码——钟表

帅哥哟,离线,有人找我吗?
admin
  1楼 博客 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信 管理员
等级:管理员 帖子:1949 积分:26803 威望:0 精华:34 注册:2003/12/30 16:34:32
程序代码——钟表  发帖心情 Post By:2008/10/23 22:17:40 [显示全部帖子]

import java.util.Calendar;

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 {
        int hour;

        int minute;

        int second;

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

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

        public void paint(Graphics g) {
                int width = getWidth();
                int height = getHeight();
                int centerX = width / 2;
                int centerY = height / 2;
                int radiusX = centerX / 2;
                int radiusY = centerY / 2;
                g.setColor(255, 255, 0);
                g.fillArc(width / 6, height / 6, width * 2 / 3, height * 2 / 3, 0, 360);
                for (int i = 0; i < 12; i++) {
                        g.setColor(0, 0, 0);
                        g.fillArc(width / 5, height / 5, width * 3 / 5, height * 3 / 5,
                                        i * 30 - 3, 6);
                        g.setColor(255, 255, 0);
                        g.fillArc(width / 4, height / 4, width * 1 / 2, height * 1 / 2,
                                        i * 30 - 5, 10);
                }
                g.setColor(0, 0, 0);
                g.drawLine(centerX, centerY, centerX
                                + (int) (radiusX * 2 / 3 * Math.cos(Math.toRadians(90 - hour
                                                * 30 - minute / 60.0 * 30))), centerY
                                - (int) (radiusY * 2 / 3 * Math.sin(Math.toRadians(90 - hour
                                                * 30 - minute / 60.0 * 30))));
                g.drawLine(centerX, centerY, centerX
                                + (int) (radiusX * Math.cos(Math.toRadians(90 - minute * 6))),
                                centerY
                                                - (int) (radiusY * Math.sin(Math
                                                                .toRadians(90 - minute * 6))));
                g.setColor(255, 0, 0);
                g.drawLine(centerX, centerY, centerX
                                + (int) (radiusX * Math.cos(Math.toRadians(90 - second * 6))),
                                centerY
                                                - (int) (radiusY * Math.sin(Math
                                                                .toRadians(90 - second * 6))));

                // System.out.println(Math.cos(Math.toRadians(90 - hour * 30)));

        }

        class ActionThread implements Runnable {
                final int beijingZone = 8;

                public void run() {
                        while (true) {
                                Calendar rightNow = Calendar.getInstance();
                                hour = (rightNow.get(Calendar.HOUR_OF_DAY) + beijingZone) % 12;
                                minute = rightNow.get(Calendar.MINUTE);
                                second = rightNow.get(Calendar.SECOND);

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

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

 回到顶部