java實現的小時鐘樣本分享_java

來源:互聯網
上載者:User

複製代碼 代碼如下:

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

}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.