-- 作者:admin
-- 发布时间: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编辑过]
|