程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> 關於JAVA >> java完成的小時鐘示例分享

java完成的小時鐘示例分享

編輯:關於JAVA

java完成的小時鐘示例分享。本站提示廣大學習愛好者:(java完成的小時鐘示例分享)文章只能為提供參考,不一定能成為您想要的結果。以下是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();
 }

}

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved