时钟

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

leeshuqing

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值