java实现的小时钟示例分享

所属分类: 软件编程 / java 阅读数: 85
收藏 0 赞 0 分享

复制代码 代码如下:

//package com.clock;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.Calendar;
import java.util.GregorianCalendar;

import javax.swing.*;

import javax.swing.JFrame;

public class Clock extends JFrame implements ActionListener{
 private final double  RAD=Math.PI/180;

 public Clock(){
  super("Clock");
  setSize(400, 400);
  setLocation(400, 200);
  setVisible(true);
  setResizable(true);
  setBackground(Color.white);
  addWindowListener(new WindowAdapter() {
   @Override
   public void windowClosing(WindowEvent e) {
    System.exit(0);
   }

  });
  ActionListener drawClock=new ActionListener() {
   @Override
   public void actionPerformed(ActionEvent e) {
    repaint();
   }
  };
  new Timer(1000,drawClock).start();

 }
 @Override
 public void actionPerformed(ActionEvent e) {}
 public void paint(Graphics g){
  Graphics2D g2=(Graphics2D)g;
  Insets insert=getInsets();
  int h=getSize().height;
  g2.setStroke(new BasicStroke(3.0f));
  g.drawOval(40,40, h-80,h-80);

  int r=200;
  int R1=140;
  int initRad=60;
  for(int i=1;i<=5;i++){
   int x0= (int) (r+R1*Math.cos(RAD*initRad));
   int y0= (int) (r-R1*Math.sin(RAD*initRad));
   g.setFont(new Font("楷体", Font.BOLD, 20));
   g.drawString(""+i, x0, y0);
   initRad-=30; 
  }
  int R2=150;
  int initRad2=-120;
  for(int i=7;i<=11;i++){
   int x0= (int) (r+R2*Math.cos(RAD*initRad2));
   int y0= (int) (r-R2*Math.sin(RAD*initRad2));
   g.setFont(new Font("楷体", Font.BOLD, 20));
   g.drawString(""+i, x0, y0);
   initRad2-=30; 
  }
  g.drawString("6", 190, 350);
  g.drawString("12", 190, 60);

  Calendar now=new GregorianCalendar();
  int hour=now.get(Calendar.HOUR_OF_DAY);
  int minute=now.get(Calendar.MINUTE);
  int second=now.get(Calendar.SECOND);
  String time;
  if(hour<10){time="0"+hour;}else{time=""+hour;}

  if(minute<10){time+=":0"+minute;}else{time+=":"+minute;}

  if(second<10){time+=":0"+second;}else{time+=":"+second;}

  g.setColor(Color.WHITE);
  g.fillRect(10, 30, 90, 28);
  g.setColor(Color.red);
  g.drawString(time, 10, 50);

  int S=90-second*6;
  int M=90-minute*6;
  int H=90-hour*30-minute/2;

  
  g2.setStroke(new BasicStroke(1.0f));

  int x=(int) (200+130*Math.cos(RAD*S));
  int y=(int) (200-130*Math.sin(RAD*S));

  int SS=90-(second-1)*6;
  int xx=(int) (200+130*Math.cos(RAD*SS));
  int yy=(int) (200-130*Math.sin(RAD*SS));
  g.setColor(getBackground());
  g.drawLine(200, 200, xx, yy);
  g.setColor(Color.black);
  g.drawLine(200, 200, x, y);

  g2.setStroke(new BasicStroke(2.2f));

  int x1=(int) (200+110*Math.cos(RAD*M));
  int y1=(int) (200-110*Math.sin(RAD*M));

  int MM=90-(minute-1)*6;
  int x11=(int) (200+110*Math.cos(RAD*MM));
  int y11=(int) (200-110*Math.sin(RAD*MM));
  g.setColor(getBackground());
  g.drawLine(200, 200, x11, y11);
  g.setColor(Color.blue);
  g.drawLine(200, 200, x1, y1);

  g2.setStroke(new BasicStroke(4.3f));
  int xx1=(int) (200+90*Math.cos(RAD*H));
  int yy1=(int) (200-90*Math.sin(RAD*H));

  int HH=90-(hour-1)*30-minute/2;;
  int xxx1=(int) (200+90*Math.cos(RAD*HH));
  int yyy1=(int) (200-90*Math.sin(RAD*HH));
  g.setColor(Color.white);
  g.drawLine(200, 200, xxx1, yyy1);
  g.setColor(Color.green);
  g.drawLine(200, 200, xx1, yy1);
 }
 public static void main(String[] args) {
  new Clock();
 }

}

更多精彩内容其他人还在看

JavaWeb项目部署到服务器详细步骤详解

这篇文章主要介绍了JavaWeb项目如何部署到服务器,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

IDEA基于支付宝小程序搭建springboot项目的详细步骤

这篇文章主要介绍了IDEA基于支付宝小程序搭建springboot项目的详细步骤,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

详解SpringBoot应用服务启动与安全终止

这篇文章主要介绍了SpringBoot应用服务启动与安全终止,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

Spring Boot启动及退出加载项的方法

这篇文章主要介绍了Spring Boot启动及退出加载项的方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

Spring Data Jpa 自动生成表结构的方法示例

这篇文章主要介绍了Spring Data Jpa 自动生成表结构的方法示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

IDEA中osgi的开发应用指南详解

这篇文章主要介绍了IDEA中osgi的开发应用指南详解,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

详解用maven将dubbo工程打成jar包运行

这篇文章主要介绍了详解用maven将dubbo工程打成jar包运行,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

详解Java合并数组的两种实现方式

这篇文章主要介绍了Java合并数组的两种实现方式,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

使用Jenkins Pipeline自动化构建发布Java项目的方法

这篇文章主要介绍了使用Jenkins Pipeline自动化构建发布Java项目的方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

使用Maven配置Spring的方法步骤

这篇文章主要介绍了使用Maven配置Spring的方法步骤,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享
查看更多