import java.util.Calendar;
import java.util.TimeZone;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
public class Clock extends MIDlet {
public static Clock instance;
public Clock() {
instance = this;
}
public void startApp() {
Display.getDisplay(this).setCurrent(new MainScreen());
}
public void pauseApp() {
}
public void destroyApp(boolean unconditional) {
}
}
class MainScreen extends Canvas {
GetTime t = new GetTime();
int out_r = 80;
int in_r = 70;
int centure = 5;
int halfWidth = getWidth() / 2;
int halfHeight = getHeight() / 2;
public String getTime = "";
int getSecond, getMinute, getHour;
int secondX = 0, secondY = 0, minuteX, minuteY, hourX, hourY;
float[] sin = {0, 0.10453F, 0.2079F, 0.309017F, 0.4067366F, 0.5F, 0.587785F, 0.669F,
0.743145F, 0.80902F, 0.886F, 0.913545F, 0.9511F, 0.978148F, 0.99452F, 1
};
int sin0_r = 0;
int cos90_r = 0;
int sin30_r = (int) (sin[5] * 70);
int cos60_r = (int) (sin[10] * 70);
int sin60_r = (int) (sin[10] * 70);
int cos30_r = (int) (sin[5] * 70);
int sin90_r = in_r;
int cos0_r = in_r;
public MainScreen() {
Thread thread = new Thread(t);
thread.start();
}
protected void paint(Graphics g) {
Clear(g);
//模拟时钟,时钟表面
g.setColor(0, 0, 255);
g.fillArc(halfWidth - out_r, halfHeight - out_r, out_r * 2, out_r * 2, 0, 360);
//时钟表面,时刻点
g.setColor(255, 0, 0);
g.fillArc(halfWidth + sin30_r - 2, halfHeight - cos60_r, 4, 4, 0, 360);
g.fillArc(halfWidth + sin60_r - 2, halfHeight - cos30_r, 4, 4, 0, 360);
g.fillArc(halfWidth + sin90_r - 2, halfHeight - cos90_r, 4, 4, 0, 360);
g.fillArc(halfWidth + sin0_r - 2, halfHeight + cos0_r, 4, 4, 0, 360);
g.fillArc(halfWidth + sin30_r - 2, halfHeight + cos60_r, 4, 4, 0, 360);
g.fillArc(halfWidth + sin60_r - 2, halfHeight + cos30_r, 4, 4, 0, 360);
g.fillArc(halfWidth - sin0_r - 2, halfHeight - cos0_r, 4, 4, 0, 360);
g.fillArc(halfWidth - sin30_r - 2, halfHeight - cos60_r, 4, 4, 0, 360);
g.fillArc(halfWidth - sin60_r - 2, halfHeight - cos30_r, 4, 4, 0, 360);
g.fillArc(halfWidth - sin30_r - 2, halfHeight + cos60_r, 4, 4, 0, 360);
g.fillArc(halfWidth - sin60_r - 2, halfHeight + cos30_r, 4, 4, 0, 360);
g.fillArc(halfWidth - sin90_r - 2, halfHeight + cos90_r, 4, 4, 0, 360);
//数字时钟,时间显示
g.setColor(0, 0, 0);
Font font = Font.getFont(Font.FACE_SYSTEM, Font.STYLE_BOLD, Font.SIZE_LARGE);
g.setFont(font);
int fontWidth = font.stringWidth(getTime);
int fontHeight = font.getHeight();
g.drawString(getTime, halfWidth + fontWidth / 2, halfHeight + out_r + fontHeight / 2, Graphics.TOP | Graphics.RIGHT);
//模拟时钟,指针
g.setColor(0, 0, 0);
// 秒针
g.drawLine(halfWidth, halfHeight, halfWidth + secondX, halfHeight + secondY);
//分针
g.fillTriangle(halfWidth + minuteY / 16, halfHeight - minuteX / 16, halfWidth - minuteY / 16, halfHeight + minuteX / 16, halfWidth + minuteX, halfHeight + minuteY);
// 时针
g.fillTriangle(halfWidth + hourY / 10, halfHeight - hourX / 10, halfWidth - hourY / 10, halfHeight + hourX / 10, halfWidth + hourX, halfHeight + hourY);
//表心
g.fillArc(halfWidth - centure / 2, halfHeight - centure / 2, centure, centure, 0, 360);
}
private void Clear(Graphics g) {
g.setColor(255, 255, 255);
g.fillRect(0, 0, getWidth(), getHeight());
}
class GetTime implements Runnable {
int n = 0;
int h = 0;
int num = 0;
public void run() {
while (true) {
//从系统中获取时间
Calendar calendar = Calendar.getInstance(TimeZone.getDefault());
calendar.set(Calendar.HOUR_OF_DAY, (calendar.get(Calendar.HOUR_OF_DAY) + 8) % 24);
getTime = calendar.getTime().toString();
getSecond = calendar.get(Calendar.SECOND);
getMinute = calendar.get(Calendar.MINUTE);
getHour = calendar.get(Calendar.HOUR);
//秒针定位//可以将秒针,分针,时针的定位封装在一个方法中。
if (getSecond < 15) {
secondX = (int) (sin[(getSecond % 16)] * (in_r - 10));
secondY = -(int) (sin[15 - (getSecond % 16)] * (in_r - 10));
}
if (getSecond >= 15 && getSecond < 30) {
secondX = (int) (sin[15 - (getSecond % 15)] * (in_r - 10));
secondY = (int) (sin[(getSecond % 15)] * (in_r - 10));
}
if (getSecond >= 30 && getSecond < 45) {
secondX = -(int) (sin[(getSecond % 15)] * (in_r - 10));
secondY = (int) (sin[15 - (getSecond % 15)] * (in_r - 10));
}
if (getSecond >= 45 && getSecond < 60) {
secondX = -(int) (sin[15 - (getSecond % 15)] * (in_r - 10));
secondY = -(int) (sin[(getSecond % 15)] * (in_r - 10));
}
// 分针定位
if (getMinute < 15) {
minuteX = (int) (sin[(getMinute % 16)] * (in_r - 20));
minuteY = -(int) (sin[15 - (getMinute % 16)] * (in_r - 20));
}
if (getMinute >= 15 && getMinute < 30) {
minuteX = (int) (sin[15 - (getMinute % 15)] * (in_r - 20));
minuteY = (int) (sin[(getMinute % 15)] * (in_r - 20));
}
if (getMinute >= 30 && getMinute < 45) {
minuteX = -(int) (sin[(getMinute % 15)] * (in_r - 20));
minuteY = (int) (sin[15 - (getMinute % 15)] * (in_r - 20));
}
if (getMinute >= 45 && getMinute < 60) {
minuteX = -(int) (sin[15 - (getMinute % 15)] * (in_r - 20));
minuteY = -(int) (sin[(getMinute % 15)] * (in_r - 20));
}
//时针定位
h = ((getHour) % 12) * 5;
n=(int)(getMinute/12);
num = h + n;
if (num < 15) {
hourX = (int) (sin[(num % 16)] * (in_r - 30));
hourY = -(int) (sin[15 - (num % 16)] * (in_r - 30));
}
if (num >= 15 && num < 30) {
hourX = (int) (sin[15 - (num % 15)] * (in_r - 30));
hourY = (int) (sin[(num % 15)] * (in_r - 30));
}
if (num >= 30 && num < 45) {
hourX = -(int) (sin[(num % 15)] * (in_r - 30));
hourY = (int) (sin[15 - (num % 15)] * (in_r - 30));
}
if (num >= 45 && num < 60) {
hourX = -(int) (sin[15 - (num % 15)] * (in_r - 30));
hourY = -(int) (sin[(num % 15)] * (in_r - 30));
}
repaint();
try {
Thread.sleep(500);
} catch (Exception e) {
}
}
}
}
}
//指针的定位十分复杂,李老师有没有更好的方法为指针定位。
[此贴子已经被admin于2010-12-13 20:02:43编辑过]