做一个时钟
来源:互联网 发布:软件系统结构图 编辑:程序博客网 时间:2024/05/21 17:01
import java.util.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.Timer;
public class Clock extends JFrame implements ActionListener
{
private static final long serialVersionUID = 6790815213225162093L;
Timer timer;
int x,y,old_X,old_Y, r,x0,y0,w,h,ang;
int sdo,mdo,hdo,old_M,old_H;
TimeZone tz =TimeZone.getTimeZone("JST");
final double RAD=Math.PI/180.0;
public Clock()
{
super("时钟");
setSize(300,300);
setBackground(new Color(0,0,192));
setResizable(false);
Dimension scr=Toolkit.getDefaultToolkit().getScreenSize();
Dimension fra=this.getSize();
if(fra.width>scr.width)
{
fra.width=scr.width;
}
if(fra.height>scr.height)
{
fra.height=scr.height;
}
this.setLocation((scr.width-fra.width)/2,(scr.height-fra.height)/2);
setVisible(true);
int delay = 1000;
ActionListener taskPerformer = new ActionListener()
{
public void actionPerformed(ActionEvent evt)
{
repaint();
}
};
new Timer(delay, taskPerformer).start();
}
public void actionPerformed(ActionEvent e)
{
timer.restart();
}
public void paint( Graphics g )
{
Insets insets = getInsets();
int L0 = (insets.left)/2, T0 = (insets.top)/2;
int hh,mm,ss;
String st;
h=getSize().height;
g.setColor(Color.white);
g.drawOval(L0+30,T0+30,h-60,h-60);
g.drawOval(L0+32,T0+32,h-64,h-64);
r=h/2-30;
x0=30+r-5+L0;
y0=30+r-5-T0;
ang=60;
for (int i=1; i<=12; i++)
{
x=(int)((r+10)*Math.cos(RAD*ang)+x0);
y=(int)((r+10)*Math.sin(RAD*ang)+y0);
g.drawString(""+i,x,h-y);
ang-=30;
}
x0=30+r+L0; y0=30+r+T0;
Calendar now=Calendar.getInstance();
hh=now.get(Calendar.HOUR_OF_DAY);
mm=now.get(Calendar.MINUTE);
ss=now.get(Calendar.SECOND);
g.setColor(Color.pink);
g.fillRect(L0,T0,60,28);
g.setColor(Color.blue);
if (hh < 10) st="0"+hh; else st=""+hh;
if (mm < 10) st=st+":0"+mm; else st=st+":"+mm;
if (ss < 10) st=st+":0"+ss; else st=st+":"+ss;
g.drawString(st,L0,T0+25);
sdo=90-ss*6;
mdo=90-mm*6;
hdo=90-hh*30-mm/2;
if (old_X > 0)
{
g.setColor(getBackground());
g.drawLine(x0,y0,old_X,(h-old_Y));
} else
{
old_M=mdo;
old_H=hdo;
}
g.setColor(Color.yellow);
x=(int)((r-8)*Math.cos(RAD*sdo)+x0);
y=(int)((r-8)*Math.sin(RAD*sdo)+y0)-2*T0;
g.drawLine(x0,y0,x,(h-y));
old_X=x;
old_Y=y;
if (mdo != old_M)
{
line(g,old_M,(int)(r*0.7),getBackground());
old_M=mdo;
}
if (hdo != old_H)
{
line(g,old_H,(int)(r*0.5),getBackground());
old_H=hdo;
}
line(g,mdo,(int)(r*0.7),Color.green);
line(g,hdo,(int)(r*0.5),Color.red);
}
public void line(Graphics g, int t, int n, Color c)
{
int [] xp = new int[4];
int [] yp = new int[4];
xp[0]=x0;
yp[0]=y0;
xp[1]= (int)((n-10)*Math.cos(RAD*(t-4))+x0);
yp[1]=h-(int)((n-10)*Math.sin(RAD*(t-4))+y0);
xp[2]= (int)( n *Math.cos(RAD* t )+x0);
yp[2]=h-(int)( n *Math.sin(RAD* t )+y0);
xp[3]= (int)((n-10)*Math.cos(RAD*(t+4))+x0);
yp[3]=h-(int)((n-10)*Math.sin(RAD*(t+4))+y0);
g.setColor(c);
g.fillPolygon(xp,yp,4);
}
public static void main(String args[])
{
new Clock();
}
}
- 做一个时钟
- 做一个时钟
- flash做一个时钟程序
- 用QT做一个时钟
- 用JAVA做一个时钟程序
- 用javascript做的一个背景时钟
- 用SVG+js做一个动态时钟
- java做的一个时钟的小程序
- 使用HTML5的canvas做一个会动的时钟
- 使用HTML5的canvas做一个会动的时钟
- 使用HTML5的canvas做一个会动的时钟
- 一个使用svg做的动态时钟--svg clock
- 一个时钟
- 自做电子时钟
- 单片机做时钟
- 如何用透明的PNG图,做一个好看的时钟
- j2me做的一个时钟(请大家提出改进意见)
- 手把手教你:用Qt做一个漂亮的电子时钟(纯代码)
- 第三章 操作符
- struts2学习笔记(一)
- struts2学习笔记(二)
- Hibernate学习笔记(一)
- 在CSDN上学习
- 做一个时钟
- 【转】如何有效地记忆与学习
- 网站建设初期需要经常关注的方面是什么
- 初识Ruby on Rails 另一个优秀的j2ee框架
- 建议CSDN开一个linux内核学习模块,和linux相关也可以。
- 常见题型及解法(一)
- 普利姆算法(实现代码)
- 切记!~
- [原创+连载]一步一步做拼图游戏,C++版(六)