📄 watch.java
字号:
package jiangy.timekeeper;
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Font;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
public class Watch extends Canvas implements Runnable {
private TimeShow ts;
private Font f;
private Image img_S, img_M, img_H, imgShow_S, imgShow_M, imgShow_H,
img_dial;
private int[] pixel;
private int imgWidth_S, imgHeight_S, imgWidth_M, imgHeight_M, imgWidth_H,
imgHeight_H;
private Display parent;
private int width, height, angle_S, angle_M, angle_H;
private long last;
public Watch(Display d) {
parent = d;
width = this.getWidth();
height = this.getHeight();
f = Font.getFont(Font.FACE_MONOSPACE, Font.STYLE_BOLD, Font.SIZE_LARGE);
try {
img_S = Image.createImage("/secondFinger.png");
img_M = Image.createImage("/minuteFinger.png");
img_H = Image.createImage("/hourFinger.png");
img_dial = Image.createImage("/dial.png");
} catch (Exception e) {
e.printStackTrace();
}
imgWidth_S = img_S.getWidth();
imgHeight_S = img_S.getHeight();
imgWidth_M = img_M.getWidth();
imgHeight_M = img_M.getHeight();
imgWidth_H = img_H.getWidth();
imgHeight_H = img_H.getHeight();
}
public void init() {
ts = new TimeShow();
angle_S = 360 - 6 * ts.second;
angle_M = 360 - 6 * ts.minute;
angle_H = 360 - 6 * ((ts.hour % 12) * 5 + ts.minute / 12);
try {
imgShow_S = Image.createImage(imgWidth_S, imgHeight_S);
imgShow_M = Image.createImage(imgWidth_M, imgHeight_M);
imgShow_H = Image.createImage(imgWidth_H, imgHeight_H);
} catch (Exception e) {
e.printStackTrace();
}
getImagePixel(img_S, imgShow_S);
getImagePixel(img_M, imgShow_M);
getImagePixel(img_H, imgShow_H);
imgShow_S = rotate(imgShow_S, imgWidth_S / 2, imgHeight_S, angle_S);
imgShow_M = rotate(imgShow_M, imgWidth_M / 2, imgHeight_M, angle_M);
imgShow_H = rotate(imgShow_H, imgWidth_H / 2, imgHeight_H, angle_H);
}
public void paint(Graphics g) {
init();
g.setColor(0xDEB887);
g.fillRect(0, 0, width, height);
g.setFont(f);
g.setColor(0x4682B4);
g.drawString(ts.date, 5, 5, 0);
g.drawString(ts.time, width - 5, 5, Graphics.RIGHT | Graphics.TOP);
// paint dial
g.drawImage(img_dial, width / 2, height / 2, Graphics.HCENTER
| Graphics.VCENTER);
// paint hour finger
if (angle_H % 360 == 0)
g.drawImage(imgShow_H, width / 2, height / 2, Graphics.HCENTER
| Graphics.BOTTOM);
else
g.drawImage(imgShow_H, width / 2, height / 2, Graphics.HCENTER
| Graphics.VCENTER);
// paint minute finger
if (angle_M % 360 == 0)
g.drawImage(imgShow_M, width / 2, height / 2, Graphics.HCENTER
| Graphics.BOTTOM);
else
g.drawImage(imgShow_M, width / 2, height / 2, Graphics.HCENTER
| Graphics.VCENTER);
// paint second finger
if (angle_S % 360 == 0)
g.drawImage(imgShow_S, width / 2, height / 2, Graphics.HCENTER
| Graphics.BOTTOM);
else
g.drawImage(imgShow_S, width / 2, height / 2, Graphics.HCENTER
| Graphics.VCENTER);
g.setColor(0x7CFC00);
g.fillArc(width / 2 - 10, height / 2 - 10, 20, 20, 0, 360);
}
public void run() {
long now = System.currentTimeMillis();
if (now - last >= 1000) {
if (angle_S <= 0)
angle_S = 360;
angle_S -= 6;
if (angle_M <= 0)
angle_M = 360;
angle_M -= 6;
if (angle_H <= 0)
angle_H = 360;
angle_H -= 6;
repaint();
last = now;
}
repaint();
parent.callSerially(this);
}
protected void showNotify() {
repaint();
parent.callSerially(this);
}
public void getImagePixel(Image img, Image imgShow) {
Graphics g = imgShow.getGraphics();
pixel = new int[img.getWidth()];
for (int i = img.getHeight(); i > 0; i--) {
img.getRGB(pixel, 0, img.getWidth(), 0, img.getHeight() - i, img
.getWidth(), 1);
g.drawRGB(pixel, 0, img.getWidth(), 0, img.getHeight() - i, img
.getWidth(), 1, true);
}
}
/**
* @param imgSource
* source image
* @param cx
* set the unchanged point y
* @param cy
* set the unchanged point y
* @param theta
* anticlockwise revolution theta angle
* @return return the image after rotate
*/
public Image rotate(Image imgSource, int cx, int cy, double theta) {
if (Math.abs(theta % 360) < 0.1)
return imgSource; // return when the angle less than 0.1
int w1 = imgSource.getWidth();
int h1 = imgSource.getHeight();
int[] srcMap = new int[w1 * h1];
imgSource.getRGB(srcMap, 0, w1, 0, 0, w1, h1);
double dr = Math.sqrt(cx * cx + cy * cy);
int wh2 = (int) (2 * dr + 1);
int[] destMap = new int[wh2 * wh2]; // storage new image array
double destX, destY;
double radian = theta * Math.PI / 180; // angle to radian
for (int i = 0; i < w1; i++) {
for (int j = 0; j < h1; j++) {
if (srcMap[j * w1 + i] >> 24 != 0) { // deal to unalpha
// get the coordinate of new image
destX = dr + (i - cx) * Math.cos(radian) + (j - cy)
* Math.sin(radian);
destY = dr + (j - cy) * Math.cos(radian) - (i - cx)
* Math.sin(radian);
// fill pixel from source image to target image
destMap[(int) destY * wh2 + (int) destX] = srcMap[j * w1
+ i];
}
}
}
return Image.createRGBImage(destMap, wh2, wh2, true);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -