用JAVA做小时钟

Posted on 2006-09-03 07:08 久城 阅读(992) 评论(0)  编辑  收藏 所属分类: JavaTest

学JAVA 做的第一个GUI程序......
import java.awt.*;
import javax.swing.*;
import java.util.*;

class Clock extends JFrame
{
 public static double PI = 3.14159265;
 Calendar now;
 int hh;
 int mm;
 int ss;
 Clock()
 {
  super("我的时钟");
  setSize(400,400);
  setVisible(true);
 }
 public void paint(Graphics g)
 {
  g.setColor(Color.WHITE);
  g.fillRoundRect(100,100,200,200,200,200);
  g.setColor(Color.BLACK);
  g.drawString("1",(int)(200-2+100*(Math.sin(30*2*PI/360))),(int)(200+10-100*(Math.cos(30*2*PI/360))));
  g.drawString("2",(int)(200-6+100*(Math.sin(30*2*2*PI/360))),(int)(200+10-100*(Math.cos(30*2*2*PI/360))));
  g.drawString("3",(int)(200-7+100*(Math.sin(30*3*2*PI/360))),(int)(200-100*(Math.cos(30*3*2*PI/360))));
  g.drawString("4",(int)(200-6+100*(Math.sin(30*4*2*PI/360))),(int)(200-100*(Math.cos(30*4*2*PI/360))));
  g.drawString("5",(int)(200-2+100*(Math.sin(30*5*2*PI/360))),(int)(200-2-100*(Math.cos(30*5*2*PI/360))));
  g.drawString("6",(int)(200+100*(Math.sin(30*6*2*PI/360))),(int)(200-100*(Math.cos(30*6*2*PI/360))));
  g.drawString("7",(int)(200+100*(Math.sin(30*7*2*PI/360))),(int)(200-100*(Math.cos(30*7*2*PI/360))));
  g.drawString("8",(int)(200+100*(Math.sin(30*8*2*PI/360))),(int)(200-100*(Math.cos(30*8*2*PI/360))));
  g.drawString("9",(int)(200+100*(Math.sin(30*9*2*PI/360))),(int)(200-100*(Math.cos(30*9*2*PI/360))));
  g.drawString("10",(int)(200+2+100*(Math.sin(30*10*2*PI/360))),(int)(200+2-100*(Math.cos(30*10*2*PI/360))));
  g.drawString("11",(int)(200+100*(Math.sin(30*11*2*PI/360))),(int)(200+10-100*(Math.cos(30*11*2*PI/360))));
  g.drawString("12",(int)(200+100*(Math.sin(30*12*2*PI/360))),(int)(200+10-100*(Math.cos(30*12*2*PI/360))));
  g.setColor(Color.RED);

  Calendar now=Calendar.getInstance();
  int hh=now.get(Calendar.HOUR_OF_DAY);//小时
  int mm=now.get(Calendar.MINUTE);//分钟
  int ss=now.get(Calendar.SECOND);// 秒
  g.drawLine(200,200,(int)(200+90*(Math.sin(6*ss*2*PI/360))),(int)(200-90*(Math.cos(6*ss*2*PI/360)))); //画秒针
  g.setColor(Color.black);
  g.drawLine(200,200,(int)(200+70*(Math.sin(6*mm*2*PI/360))),(int)(200-70*(Math.cos(6*mm*2*PI/360))));//画分针
  g.setColor(Color.blue);
  g.drawLine(200,200,(int)(200+50*(Math.sin((30*hh+0.5*mm)*2*PI/360))),(int)(200-50*(Math.cos((30*hh+0.5*mm)*2*PI/360)))); //画时针
  try{Thread.sleep(500);}catch(Exception e){}
  repaint();  
 }

 public static void main(String[] args)
 {
  
  Clock c = new Clock();
  c.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  
 }
}



欢迎来访!^.^!
本BLOG仅用于个人学习交流!
目的在于记录个人成长.
所有文字均属于个人理解.
如有错误,望多多指教!不胜感激!


只有注册用户登录后才能发表评论。


网站导航:
 

Copyright © 久城