📄 clockdemo.java
字号:
import java.awt.Graphics;
import java.awt.Color;
import java.util.Date;
public class ClockDemo extends java.applet.Applet implements Runnable
{
Date timeNow;
Clock myClock;
Thread clockThread=null;
public void start() {
if (clockThread==null)
{
clockThread=new Thread(this);
clockThread.start();
}
}
public void stop() {
clockThread.stop();
clockThread=null;
}
public void run() {
while (true)
{
repaint();
try
{
Thread.sleep(1000);
}
catch (InterruptedException e){}
}
}
/*public void update(Graphics g)
{
myClock.clear(g,100,100,100,getBackground());
g.setColor(getForeground());
paint(g);
}*/
public void paint(Graphics g){
timeNow=new Date();
myClock=new Clock(timeNow.getHours(),timeNow.getMinutes(),timeNow.getSeconds());
g.drawString(timeNow.toString(),25,240);
myClock.show(g,100,100,100);
};
}
class Clock
{
Clock(int hrs,int min,int sec)
{
hour=hrs%12;
minute=min;
second=sec;
}
void show(Graphics g,int center_x,int center_y,int radius) {
int hrs_nid_len=(int)(radius*0.5),
min_nid_len=(int)(radius*0.7),
sec_nid_len=(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.0*2.0*Math.PI;
drawNiddle(g,Color.blue,center_x,center_y,hrs_nid_len,theta);
theta=(double)(minute*60+second)/3600.0*2.0*Math.PI;
drawNiddle(g,Color.blue,center_x,center_y,min_nid_len,theta);
theta=(double) second/60.0*2.0*Math.PI;
drawNiddle(g,Color.red,center_x,center_y,sec_nid_len,theta);
}
/*void clear(Graphics g,int center_x,int center_y,int radius,Color clearColor)
{
int hrs_nid_len=(int)(radius*0.5),
min_nid_len=(int)(radius*0.7),
sec_nid_len=(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.0*2.0*Math.PI;
drawNiddle(g,clearColor,center_x,center_y,hrs_nid_len,theta);
theta=(double)(minute*60+second)/3600.0*2.0*Math.PI;
drawNiddle(g,clearColor,center_x,center_y,min_nid_len,theta);
theta=(double) second/60.0*2.0*Math.PI;
drawNiddle(g,clearColor,center_x,center_y,sec_nid_len,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)));
}
int hour,minute,second;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -