- import java.util.*;
- import javax.microedition.lcdui.*;
- import javax.microedition.midlet.MIDlet;
- public class Exec extends MIDlet {
- private Display display;
- DrawPanel dp = new DrawPanel();
- public Exec() {
- display = Display.getDisplay(this);
- }
- public void startApp() {
- display.setCurrent(dp);
- }
- public void pauseApp() {
- }
- public void destroyApp(boolean unconditional) {
- }
- }
- class DrawPanel extends Canvas {
- String str = "";
- int xm, ym, xh, yh, xs, ys;
- int lastxs, lastys, lastxm, lastym, lastxh, lastyh;
- public DrawPanel() {
- xm = ym = xh = yh = xs = ys = 0;
- }
- public void paint(Graphics g) {
- g.setColor(255, 255, 255);
- g.fillRect(0, 0, getWidth(), getHeight());
- g.setColor(0, 0, 0);
- Calendar calendar = Calendar.getInstance(TimeZone.getDefault());
- calendar.set(Calendar.HOUR_OF_DAY, (calendar.get(Calendar.HOUR_OF_DAY) + 8) % 24);
- int hour = calendar.get(Calendar.HOUR);
- int min = calendar.get(Calendar.MINUTE);
- int se = calendar.get(Calendar.SECOND) + 1;
- xs = (int) (Math.cos(se * 3.14f / 30 - 3.14f / 2) * 45 + 117);
- ys = (int) (Math.sin(se * 3.14f / 30 - 3.14f / 2) * 45 + 93);
- xm = (int) (Math.cos(min * 3.14f / 30 - 3.14f / 2) * 40 + 117);
- ym = (int) (Math.sin(min * 3.14f / 30 - 3.14f / 2) * 40 + 93);
- xh = (int) (Math.cos((hour * 30 + min / 2) * 3.14f / 180 - 3.14f / 2) * 30 + 117);
- yh = (int) (Math.sin((hour * 30 + min / 2) * 3.14f / 180 - 3.14f / 2) * 30 + 93);
- str = hour + " " + ":" + " " + min + " " + ":" + " " + se;
- Font font = Font.getFont(Font.FACE_SYSTEM, Font.STYLE_BOLD, Font.SIZE_LARGE);
- g.setFont(font);
- g.drawArc(54, 30, 126, 126, 0, 360);
- g.drawString(str, getWidth() / 2 - 32, getHeight() * 2 / 3, Graphics.TOP | Graphics.LEFT);
- g.drawString("9", 61, 89, Graphics.TOP | Graphics.LEFT);
- g.drawString("3", 170, 89, Graphics.TOP | Graphics.LEFT);
- g.drawString("12", 112, 33, Graphics.TOP | Graphics.LEFT);
- g.drawString("6", 113, 140, Graphics.TOP | Graphics.LEFT);
- g.setColor(255, 0, 0);
- g.drawLine(117, 93, xs, ys);
- g.setColor(100, 0, 255);
- g.drawLine(117, 93, xm, ym);
- g.drawLine(117, 92, xh, yh);
- g.drawLine(116, 93, xh, yh);
- try {
- Thread.sleep(1000);
- } catch (InterruptedException e) {
- }
- repaint();
- }
- }
手机时钟(加了表盘)代码
最新推荐文章于 2021-05-26 22:37:59 发布