📄 nowtime.java
字号:
import java.awt.*;
import java.applet.*;
import java.util.*;
import java.text.*;
public class nowtime extends Applet implements Runnable{
Date nowdate;
public Thread runner;
Clock myClock;
public void init(){
setBackground(Color.white);
}
public void start() {
if (runner == null); {
runner = new Thread(this); //new Thread
runner.start();
}
}
public void stop() {
if (runner != null) {
runner = null;
}
}
public void run() {
while(Thread.currentThread()==runner){
repaint();
try { Thread.sleep(1000); }
catch (InterruptedException e) { }
}
}
public void paint(Graphics g){
nowdate=new Date();
g.setColor(Color.red);
SimpleDateFormat formatter=new SimpleDateFormat("s",Locale.getDefault());
int s=Integer.parseInt(formatter.format(nowdate));
formatter.applyPattern("m");
int m=Integer.parseInt(formatter.format(nowdate));
formatter.applyPattern("h");
int h=Integer.parseInt(formatter.format(nowdate));
myClock=new Clock(h,m,s);
formatter=new SimpleDateFormat("yyyy年MM月dd日 hh:mm:ss a");
String today=formatter.format(nowdate);
g.drawString(today,30 ,250);
myClock.show(g,100,100,100,100);
}
}
class Clock {
int hour,minute,second;
Clock(int h,int m,int s){
hour=h % 12;
minute=m;
second=s;}
void show(Graphics g,int center_x,int center_y,int len,int radius){
int hlen=(int)(radius*0.5),mlen=(int)(radius*0.7),slen=(int)(radius*0.85);
double theta;
g.drawOval(center_x-radius,center_y-radius,radius*2,radius*2);
theta=(double)(hour*60*60+minute*60+second)/43200*2.0*Math.PI;
drawNiddle(g,Color.blue,center_x,center_y,hlen,theta);
theta=(double)(minute*60+second)/3600*2.0*Math.PI;
drawNiddle(g,Color.green,center_x,center_y,hlen,theta);
theta=(double)(second)/60*2.0*Math.PI;
drawNiddle(g,Color.red,center_x,center_y,hlen,theta);
}
private void drawNiddle(Graphics g,Color c,int x,int y,int len,double theta)
{ g.setColor(c);
g.drawLine(x,y,(int)(x+len*Math.sin(theta)),(int)(y-len*Math.cos(theta)));
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -