⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 clockcanvas.java

📁 《精通JAVA手机游戏与应用程序设计》随书光盘
💻 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; //缓冲区要绘画的图像

	private Image copy;//缓冲区对象

	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");
			//指定缓冲区域为屏幕大小
			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) {		
		//显示缓存区的可变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 + -