简单的模拟时钟
package Thread;
import java.awt.BasicStroke;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Insets;
import java.awt.Rectangle;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.geom.Line2D;
import java.awt.geom.Point2D;
import java.util.Calendar;
import java.util.GregorianCalendar;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
import javax.swing.border.EmptyBorder;
public class SimulatedClock extends JFrame {
private JPanel contentPane;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
SimulatedClock frame = new SimulatedClock();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public SimulatedClock() {
addWindowListener(new WindowAdapter(){
public void windowActivated(WindowEvent e){
do_this_windowActivated(e);
}
});
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(new BorderLayout(0,0));
setContentPane(contentPane);
JLabel label=new JLabel(" ");
label.setHorizontalAlignment(SwingConstants.CENTER);
label.setFont(new Font("Lucida Handwriting", Font.PLAIN, 32));
contentPane.add(label,BorderLayout.NORTH);
JLabel label1=new JLabel("BE FINE");
label1.setHorizontalAlignment(SwingConstants.CENTER);
label1.setFont(new Font("微软雅黑", Font.PLAIN, 32));
contentPane.add(label1,BorderLayout.CENTER);
}
public void paint(Graphics g){
super.paint(g);
Rectangle rectangle=getBounds();
Insets insets=getInsets();
int radius=120;
int x=(rectangle.width-2*radius-insets.left-insets.right)/2+insets.left;
int y=(rectangle.height-2*radius-insets.top-insets.bottom)/2+insets.top;
Point2D.Double center=new Point2D.Double(x+radius,y+radius);
g.drawOval(x, y, 2*radius, 2*radius);
Point2D.Double[] scales=new Point2D.Double[60];
Point2D.Double[] scales1=new Point2D.Double[60];
Point2D.Double[] scales2=new Point2D.Double[60];
Point2D.Double[] scales3=new Point2D.Double[60];
int radius1=80;
int radius2=60;
double angle=Math.PI/30;
for(int i=0;i<scales.length;i++){
scales[i]=new Point2D.Double();
scales[i].setLocation(x+radius+radius*Math.sin(angle*i),y+radius-radius*Math.cos(angle*i));
//分针的顶端位置
scales1[i]=new Point2D.Double();
scales1[i].setLocation(x+radius+radius1*Math.sin(angle*i),y+radius-radius1*Math.cos(angle*i));
//时针的顶端位置
scales2[i]=new Point2D.Double();
scales2[i].setLocation(x+radius+radius2*Math.sin(angle*i),y+radius-radius2*Math.cos(angle*i));
//秒针装饰的位置
scales3[i]=new Point2D.Double();
double x1=x+radius+radius1*Math.sin(angle*i)-10;
double y1=y+radius-radius1*Math.cos(angle*i)-10;
scales3[i].setLocation(x1,y1);
}
for(int i=0;i<scales.length;i++){
if(i%5==0){
g.setColor(Color.RED);
g.fillOval((int)scales[i].x-4,(int)scales[i].y-4,8,8);
}else{
g.setColor(Color.BLACK);
g.fillOval((int)scales[i].x-2,(int)scales[i].y-2,4,4);
g.setColor(Color.WHITE);
g.fillOval((int)scales[i].x-1,(int)scales[i].y-1,2,2);
}
}
Calendar calendar=new GregorianCalendar();
int hour=calendar.get(Calendar.HOUR);
int minute=calendar.get(Calendar.MINUTE);
int second=calendar.get(Calendar.SECOND);
Graphics2D g2d=(Graphics2D)g;
g2d.setColor(Color.RED);
g2d.draw(new Line2D.Double(center,scales[second]));
BasicStroke bs=new BasicStroke(3f,BasicStroke.CAP_ROUND,BasicStroke.JOIN_MITER);
g2d.setStroke(bs);
//秒针装饰
g2d.setColor(Color.RED);
g.drawOval((int)scales3[second].x,(int)scales3[second].y,20,20);
g2d.setColor(Color.BLACK);
g2d.draw(new Line2D.Double(center,scales1[minute]));
bs=new BasicStroke(6f,BasicStroke.CAP_BUTT,BasicStroke.JOIN_MITER);
g2d.setStroke(bs);
g2d.setColor(Color.BLACK);
g2d.draw(new Line2D.Double(center,scales2[hour*5+minute/12]));
}
private class ClockRunnable implements Runnable{
public void run(){
while(true){
repaint();
try{
Thread.sleep(1000);
}catch(InterruptedException e){
e.printStackTrace();
}
}
}
}
protected void do_this_windowActivated(WindowEvent e){
new Thread(new ClockRunnable()).start();
}
import java.awt.BasicStroke;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Insets;
import java.awt.Rectangle;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.geom.Line2D;
import java.awt.geom.Point2D;
import java.util.Calendar;
import java.util.GregorianCalendar;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
import javax.swing.border.EmptyBorder;
public class SimulatedClock extends JFrame {
private JPanel contentPane;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
SimulatedClock frame = new SimulatedClock();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public SimulatedClock() {
addWindowListener(new WindowAdapter(){
public void windowActivated(WindowEvent e){
do_this_windowActivated(e);
}
});
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(new BorderLayout(0,0));
setContentPane(contentPane);
JLabel label=new JLabel(" ");
label.setHorizontalAlignment(SwingConstants.CENTER);
label.setFont(new Font("Lucida Handwriting", Font.PLAIN, 32));
contentPane.add(label,BorderLayout.NORTH);
JLabel label1=new JLabel("BE FINE");
label1.setHorizontalAlignment(SwingConstants.CENTER);
label1.setFont(new Font("微软雅黑", Font.PLAIN, 32));
contentPane.add(label1,BorderLayout.CENTER);
}
public void paint(Graphics g){
super.paint(g);
Rectangle rectangle=getBounds();
Insets insets=getInsets();
int radius=120;
int x=(rectangle.width-2*radius-insets.left-insets.right)/2+insets.left;
int y=(rectangle.height-2*radius-insets.top-insets.bottom)/2+insets.top;
Point2D.Double center=new Point2D.Double(x+radius,y+radius);
g.drawOval(x, y, 2*radius, 2*radius);
Point2D.Double[] scales=new Point2D.Double[60];
Point2D.Double[] scales1=new Point2D.Double[60];
Point2D.Double[] scales2=new Point2D.Double[60];
Point2D.Double[] scales3=new Point2D.Double[60];
int radius1=80;
int radius2=60;
double angle=Math.PI/30;
for(int i=0;i<scales.length;i++){
scales[i]=new Point2D.Double();
scales[i].setLocation(x+radius+radius*Math.sin(angle*i),y+radius-radius*Math.cos(angle*i));
//分针的顶端位置
scales1[i]=new Point2D.Double();
scales1[i].setLocation(x+radius+radius1*Math.sin(angle*i),y+radius-radius1*Math.cos(angle*i));
//时针的顶端位置
scales2[i]=new Point2D.Double();
scales2[i].setLocation(x+radius+radius2*Math.sin(angle*i),y+radius-radius2*Math.cos(angle*i));
//秒针装饰的位置
scales3[i]=new Point2D.Double();
double x1=x+radius+radius1*Math.sin(angle*i)-10;
double y1=y+radius-radius1*Math.cos(angle*i)-10;
scales3[i].setLocation(x1,y1);
}
for(int i=0;i<scales.length;i++){
if(i%5==0){
g.setColor(Color.RED);
g.fillOval((int)scales[i].x-4,(int)scales[i].y-4,8,8);
}else{
g.setColor(Color.BLACK);
g.fillOval((int)scales[i].x-2,(int)scales[i].y-2,4,4);
g.setColor(Color.WHITE);
g.fillOval((int)scales[i].x-1,(int)scales[i].y-1,2,2);
}
}
Calendar calendar=new GregorianCalendar();
int hour=calendar.get(Calendar.HOUR);
int minute=calendar.get(Calendar.MINUTE);
int second=calendar.get(Calendar.SECOND);
Graphics2D g2d=(Graphics2D)g;
g2d.setColor(Color.RED);
g2d.draw(new Line2D.Double(center,scales[second]));
BasicStroke bs=new BasicStroke(3f,BasicStroke.CAP_ROUND,BasicStroke.JOIN_MITER);
g2d.setStroke(bs);
//秒针装饰
g2d.setColor(Color.RED);
g.drawOval((int)scales3[second].x,(int)scales3[second].y,20,20);
g2d.setColor(Color.BLACK);
g2d.draw(new Line2D.Double(center,scales1[minute]));
bs=new BasicStroke(6f,BasicStroke.CAP_BUTT,BasicStroke.JOIN_MITER);
g2d.setStroke(bs);
g2d.setColor(Color.BLACK);
g2d.draw(new Line2D.Double(center,scales2[hour*5+minute/12]));
}
private class ClockRunnable implements Runnable{
public void run(){
while(true){
repaint();
try{
Thread.sleep(1000);
}catch(InterruptedException e){
e.printStackTrace();
}
}
}
}
protected void do_this_windowActivated(WindowEvent e){
new Thread(new ClockRunnable()).start();
}
}
结果: