时钟万年历
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
Main.java
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JFrame;
public class Main extends JFrame
{
//创建面板
Calendar Calendar=new Calendar();
Clock clock=new Clock();
public Main()
{
//设置布局为null
setLayout(null);
//设置关闭方式
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//设置窗口位置和大小
setBounds(300, 200, 600, 360);
//将面板加入窗口
add(Calendar);
add(clock);
//设置面板位置和大小
Calendar.setBounds(0, 0, 400, 360);
clock.setBounds(420, 100, 200, 200);
//设置窗口关闭事件
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
//使窗口显示
setVisible(true);
}
public static void main(String args[])
{
new Main();
}
}
Clock.java
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.util.Calendar;
import java.awt.geom.*;
public class Clock extends JPanel implements ActionListener { protected static Ellipse2D face = new Ellipse2D.Float(3,3,94,94); protected static GeneralPath tick = new GeneralPath(); //包含在java.awt.geom.*;
static
{
tick.moveTo(100, 100);
tick.moveTo(49, 0);
tick.lineTo(51, 0);
tick.lineTo(51, 6);
tick.lineTo(49, 6);
tick.lineTo(49, 0);
}
protected static GeneralPath hourHand = new GeneralPath();
static
{
hourHand.moveTo(50, 15);
hourHand.lineTo(53, 50);
hourHand.lineTo(50, 53);
hourHand.lineTo(47, 50);
hourHand.lineTo(50, 15);
}
protected static GeneralPath minuteHand = new GeneralPath(); static
{
minuteHand.moveTo(50, 2); minuteHand.lineTo(53, 50); minuteHand.lineTo(50, 58);
minuteHand.lineTo(47, 50);
minuteHand.lineTo(50, 2);
}
protected static GeneralPath secondHand = new GeneralPath();
static
{
secondHand.moveTo(49, 5);
secondHand.lineTo(51, 5);
secondHand.lineTo(51, 62);
secondHand.lineTo(49, 62);
secondHand.lineTo(49, 5);
}
protected static Color faceColor = new Color(220,220,220); protected static Color hourColor = Color.red.darker();
protected static Color minuteColor = new Color(220,0,220); protected static Color secondColor = Color.blue.darker(); protected static Color pinColor = new Color(220,20,111);
protected static Ellipse2D pivot = new Ellipse2D.Float(47,47,6,6); protected static Ellipse2D centerPin = new
Ellipse2D.Float(49,49,2,2);
protected AffineTransform hourTransform =
AffineTransform.getRotateInstance(0,50,50);
protected AffineTransform minuteTransform =
AffineTransform.getRotateInstance(0,50,50);
protected AffineTransform secondTransform =
AffineTransform.getRotateInstance(0,50,50);
protected Timer timer = new Timer(1000,this);
protected Calendar calender = Calendar.getInstance();
public Clock()
{
setPreferredSize(new Dimension(100,100));
}
public void addNotify()
{
super.addNotify();
timer.start();
} public void removeNotify()
{
timer.stop();
super.removeNotify();
}
public void actionPerformed (ActionEvent event)
{
this.calender.setTime(new java.util.Date());
int hour = this.calender.get(Calendar.HOUR);
int minute = this.calender.get(Calendar.MINUTE);
int second = this.calender.get(Calendar.SECOND);
hourTransform.setToRotation((((double)(hour-1.0+minute/60.0))*(Ma th.PI/6.0)),50,50);
minuteTransform.setToRotation(((double)(minute+second/60.0))*(Mat h.PI/30.0),50,50);
secondTransform.setToRotation(((double)second)*(Math.PI/30.0),50, 50);
repaint();
}
public void paint(Graphics g)
{
super.paint(g);