📄 digitclock.java
字号:
package digitclock;import java.awt.*;import java.awt.event.*;import java.applet.*;import java.awt.Color;import java.awt.Graphics;import java.awt.Image;import java.util.Date;/** * <p>Title: 数字时钟</p> * <p>Description: 以图片方式显示时钟</p> * <p>Copyright: Copyright (c) 2004</p> * <p>Company: 北京师范大学计算机系</p> * @author 孙一林 * @version 1.0 */public class DigitClock extends MultiThreadApplet { boolean isStandalone = false; Image[] digit_image = new Image[10]; // 数码(0-9)图像数组 Image colon_image, // 冒号图像 frame_image; // 边框图像 int digit_height = 21; // 数码(及冒号)高度 int digit_width = 16; // 数码宽度 int colon_width = 9; // 冒号宽度 int offset = 4; // 边框厚度 int applet_width ,applet_height; // 定义小出现宽、高变量 int[] image_start_x = new int[8]; // 数码或冒号的水平起始位置数组 //Get a parameter value public String getParameter(String key, String def) { return isStandalone ? System.getProperty(key, def) : (getParameter(key) != null ? getParameter(key) : def); } //Construct the applet public DigitClock() { } //Initialize the applet public void init() { try { jbInit(); } catch(Exception e) { e.printStackTrace(); } } //Component initialization private void jbInit() throws Exception { for (int i = 0; i < 10; i++){ digit_image[i] = // 读取图像到数组缓冲区 getImage( getCodeBase(), "lcdimages/lcd" + i + ".gif" ); } colon_image = getImage( getCodeBase(), "lcdimages/colon.gif" ); frame_image = getImage( getCodeBase(), "lcdimages/frame.gif" ); applet_width = ( 2 * offset ) + ( 6 * digit_width ) + ( 2 * colon_width ); // 计算显示宽度 applet_height = ( 2 * offset ) + ( digit_height ); // 计算显示高度 image_start_x[0] = offset; // 填充起始位置数组 for (int i = 1; i < 8; i++){ if ( (i == 3) || (i == 6) ){ // 计算冒号位置 image_start_x[i] = image_start_x[i - 1] + colon_width; } else{ // 计算数码位置 image_start_x[i] = image_start_x[i - 1] + digit_width; } } } public void paint( Graphics g ) { Date now = new Date(); // 获取当前日期和时间的对象 int hour = now.getHours(); // 取小时数 int minute = now.getMinutes(); // 取分钟数 int second = now.getSeconds(); // 取秒钟数 int i = 0; // 水平起始位置数组的索引 g.drawImage(frame_image, 0, 0, this); g.drawImage( digit_image[hour / 10],image_start_x[i++],offset,this ); g.drawImage( digit_image[hour % 10],image_start_x[i++],offset,this ); g.drawImage( colon_image,image_start_x[i++],offset,this);// 显示冒号 g.drawImage(digit_image[minute/10],image_start_x[i++],offset,this); g.drawImage(digit_image[minute%10],image_start_x[i++],offset,this); g.drawImage(colon_image,image_start_x[i++],offset,this);// 显示冒号 g.drawImage(digit_image[second/10],image_start_x[i++],offset,this); g.drawImage(digit_image[second%10], image_start_x[i],offset,this ); } // 按时、分、秒位置显示图像 public void run() { while ( thisThread != null ) { try{ thisThread.sleep(1000); // 1秒延时,每秒显示一次 } catch (InterruptedException e){ } repaint(); } } //Get Applet information public String getAppletInfo() { return "Applet Information"; } //Get parameter info public String[][] getParameterInfo() { return null; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -