该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
费了我一天才做完的,而且付出了相应的代价(落下两顿饭),有兴趣的看看吧!!呵呵
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.*;
import java.util.Calendar;
import java.util.GregorianCalendar;
import javax.swing.*;
public class TIME extends Frame implements ActionListener
{
int ss,mm,hh,osx,osy,omx,omy,oldm,oldh,ohx,ohy;
Button close=new Button("关闭窗口");
final double RAD=Math.PI/180;
public static void main(String args[])
{
new TIME();
}
public TIME()
{
setTitle("钟表");
setSize(400,400);
setVisible(true);
setBackground(Color.red);
setResizable(false);
setLayout(null);
int delay=1000;
close.setSize(80,20);
close.setLocation(250,40);
add(close);
close.addActionListener(this);
ActionListener drawClock=new ActionListener()
{
public void actionPerformed(ActionEvent ec)
{ repaint();}
};
new Timer(delay,drawClock).start();
}
public void paint(Graphics g)
{
Calendar now=new GregorianCalendar();
int nowh=now.get(Calendar.HOUR_OF_DAY);
int nowm=now.get(Calendar.MINUTE);
int nows=now.get(Calendar.SECOND);
String st;
if(nowh<10) st="0"+nowh;else st=""+nowh;
if(nowm<10) st+=":0"+nowm;else st+=":"+nowm;
if(nows<10) st+=":0"+nows;else st+=":"+nows;
g.setColor(Color.pink);
g.fillRect(10,30,80,28);
g.setColor(Color.blue);
g.setFont(new Font("宋体",Font.BOLD,14));
g.drawString(st,20,46);
Graphics2D g2D=(Graphics2D)g;
g2D.setStroke(new BasicStroke(4.0f));
g.setColor(Color.green);
g.drawOval(60,100,250,250);
g.fillOval(180,220,12,12);
g.setColor(Color.black);
g.drawString("12",180,110);
g.drawString("9",60,230);
g.drawString("6",180,350);
g.drawString("3",304,230);
ss=270+nows*6;//最大是360!所以乘以6;
mm=270+nowm*6;
hh=270+nowh*30;
int x,y;
if (osx>0)
{
g.setColor(getBackground());
g.drawLine(185,225,osx,osy);
}
x=(int)(105*0.9*Math.cos(RAD*ss))+185;
y=(int)(105*0.9*Math.sin(RAD*ss))+225;
g.setColor(Color.blue);
g.drawLine(186,226,x,y);
osx=x;
osy=y;
if(oldm!=mm)
{
g.setColor(getBackground());
g.drawLine(185,225,omx,omy);
}
x=(int)(105*0.7*Math.cos(RAD*mm))+185;
y=(int)(105*0.7*Math.sin(RAD*mm))+225;
g.setColor(Color.black);
g.drawLine(185,225,x,y);
omx=x;
omy=y;
oldm=mm;
if(oldh!=hh)
{
g.setColor(getBackground());
g.drawLine(185,225,ohx,ohy);
}
x=(int)(105*0.5*Math.cos(RAD*hh))+185;
y=(int)(105*0.5*Math.sin(RAD*hh))+225;
g.setColor(Color.yellow);
g.drawLine(185,225,x,y);
ohx=x;
ohy=y;
oldh=hh;
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==close)
{
System.exit(0);
}
}
}