欢迎来到代码驿站!

JAVA代码

当前位置:首页 > 软件编程 > JAVA代码

java实现的小时钟示例分享

时间:2020-11-16 11:49:33|栏目:JAVA代码|点击:

复制代码 代码如下:

//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();
 }

}

上一篇:MyEclipse设置Console输出到文件的实现方法

栏    目:JAVA代码

下一篇:Java程序员容易犯的10大低级错误

本文标题:java实现的小时钟示例分享

本文地址:http://www.codeinn.net/misctech/23107.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有