?? clockcanvas.java
字號:
package ClockDemo;
import java.io.IOException;
import java.util.Calendar;
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
public class ClockCanvas extends Canvas implements Runnable {
private Image source; //緩沖區(qū)要繪畫的圖像
private Image copy;//緩沖區(qū)對象
private Calendar cal;
private int imageWidth, imageHeight;
private int screenWidth, screenHeight;
private int minuteLength, hourLength,secLength;
private int minute = 0, hour = 0,second = 0;
private int hx, hy;
public ClockCanvas() {
try {
source = Image.createImage("/Clock.png");
//指定緩沖區(qū)域為屏幕大小
copy = Image.createImage(this.getWidth(), this.getHeight());
Graphics g = copy.getGraphics();// 獲得一個新的Graphics對象
imageWidth = source.getWidth();
imageHeight = source.getHeight();
screenWidth = this.getWidth();
screenHeight = this.getHeight();
minuteLength = 50;
hourLength = 36;
secLength = 58;
//TimeZone tz = TimeZone.getTimeZone("GMT+8:00");
cal = Calendar.getInstance();
g.drawImage(source, (screenWidth - imageWidth) / 2,
(screenHeight - imageHeight) / 2, Graphics.TOP
| Graphics.LEFT);
} catch (IOException e) {
e.printStackTrace();
}
Thread t = new Thread(this);
t.start();
}
protected void paint(Graphics g) {
//顯示緩存區(qū)的可變Image對象
g.drawImage(copy, 0, 0, Graphics.TOP | Graphics.LEFT);
g.setColor(0, 0, 0);
g.drawString(cal.getTime().toString(), 0, 0, Graphics.TOP
| Graphics.LEFT);
second = (second + 1) % 60;
double secangle = Math.PI * second / 30;//旋轉角度
int sx = screenWidth / 2 + (int) (secLength * Math.sin(secangle));
int sy = screenHeight / 2 - (int) (secLength * Math.cos(secangle));
//畫秒針
g.setColor(0x0000ffff);
g.drawLine(sx, sy, screenWidth / 2, screenHeight / 2);
if(second%60 == 0){
minute = (minute + 1) % 60;
}
double minangle = Math.PI * minute / 30;//旋轉角度
int mx = screenWidth / 2 + (int) (minuteLength * Math.sin(minangle));
int my = screenHeight / 2 - (int) (minuteLength * Math.cos(minangle));
//畫分針
g.setColor(0x0000ff00);
g.drawLine(mx, my, screenWidth / 2, screenHeight / 2);
if (minute % 60 == 0) {
hour = (hour + 1) % 12;
}
double hourangle = Math.PI * hour/ 6 + Math.PI *minute/360;//旋轉角度
hx = screenWidth / 2 + (int) (hourLength * Math.sin(hourangle));
hy = screenHeight / 2 - (int) (hourLength * Math.cos(hourangle));
//畫時針
g.setColor(0x00ff0000);
g.drawLine(hx, hy, screenWidth / 2, screenHeight / 2);
}
public void run() {
minute = cal.get(Calendar.MINUTE);
hour = cal.get(Calendar.HOUR);
second =cal.get(Calendar.SECOND);
while (true) {
repaint();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -