用户注册



邮箱:

密码:

用户登录


邮箱:

密码:
记住登录一个月忘记密码?

发表随想


还能输入:200字
云代码 - java代码库

java时钟

2017-12-25 作者: 柯侧耳倾听者举报

[java]代码库

package day01;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.swing.JFrame;
import javax.swing.JPanel;

public class Clock {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		JFrame frame = new ClockFrame();
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setVisible(true);
	}
}
      


  class ClockFrame extends JFrame{
	  public ClockFrame(){
	  setTitle("java clock");
	  setBounds(100, 200, 600, 800);
		setVisible(true);	
	    ClockPanel cp = new ClockPanel();
		add(cp); 
	  }
  }
  
  
  class ClockPanel extends JPanel{
	  int h;
	  int m;
	  int s;
	  int sx;
	  int sy;	  	
	  int mx;
	  int my;
	  int hx;
	  int hy;
	     public ClockPanel() {
          	move();
	  	for(int i=0;i<60;i++){		
	  	int r = 100;
	  		if(i%5==0){
	  		
	  		if(i%15==0){

	  		r=120;
	  			
	  	       }else{
	  	
	  		r=140;
	  		
	  		}
	  	
	  	}
	  		
	  	int x1 = (int)(300+Math.sin(Math.toRadians(6*i))*r);

	  	int y1 = (int)(300-Math.cos(Math.toRadians(i*6))*r);
	  	
	  	int x2 = (int)(300+Math.sin(Math.toRadians(6*i))*180);
	  		
	  	int y2 = (int)(300-Math.cos(Math.toRadians(i*6))*180);
	  		

	  	}
	  	
	  }

	  	
	  public void paint(Graphics g ) {
	  	
	  	super.paint(g);
	  	
	  	setBackground(Color.LIGHT_GRAY);

	  	Graphics2D g2 = (Graphics2D) g;
	  	
	  	g2.drawString(h + ":" + m + ":" + s, 20, 50);
	  	
	  	g2.setStroke(new BasicStroke(5));
	  	
	  	g.setColor(Color.yellow);
	  	
	  	g.drawLine(300,300,sx,sy);
	  	
	  	g.setColor(Color.red);
	  	
	  	g.drawLine(300,300,mx,my);

	  	g.setColor(Color.gray);
	  	
	  	g.drawLine(300,300,hx,hy);
	  	
	  	g.setColor(Color.orange);
	  	
	  	g.drawOval(120,120,360,360);
	  	
	  	g.drawOval(205,205,190,190);
	  	
	  	g.setColor(Color.black);
	  	
	  	g.fillOval(292, 292, 15, 15);
	  	
	  	
	  for(int i=0;i<60;i++){
	  		
	  	int r = 100;
	           if(i%5==0){
	  	
	  	 if(i%15==0){
	  	
	  	  r=120;
	  		
	            }else{

	  	   r=140;
	  	
	  	}

	  }
	  			
	      int x1 = (int)(300+Math.sin(Math.toRadians(6*i))*r);
	  	
	  	int y1 = (int)(300-Math.cos(Math.toRadians(i*6))*r);
	  	
	  	int x2 = (int)(300+Math.sin(Math.toRadians(6*i))*180);
	  	
	  	int y2 = (int)(300-Math.cos(Math.toRadians(i*6))*180);
	  		
	  	g.setColor(Color.green);
	  	
	  	g.drawLine(x1,y1,x2,y2);
	  	
	  }
	  	
	      }


	  	public void move() {
	  	
	  	new Thread() {
	  	
	  		public void run() {
	  	
	  			while (true) {
	  	
	  				Date d = new Date();
	  	
	  				System.out.println(d);
	  	
	  				SimpleDateFormat ss = new SimpleDateFormat("ss");
	  	
	  				SimpleDateFormat mm = new SimpleDateFormat("mm");
	  	
	  				SimpleDateFormat hh = new SimpleDateFormat("HH");

	  	
	  				s = Integer.parseInt(ss.format(d));
	  	
	  				m = Integer.parseInt(mm.format(d));

	  				h = Integer.parseInt(hh.format(d));

	  				sx = (int) (300 + Math.sin(Math.toRadians(6 * s)) * 150);
	  		
	  			        sy = (int) (300 - Math.cos(Math.toRadians(6 * s)) * 150);
	  	
	  				mx = (int) (300 + Math.sin(Math.toRadians(6 * m)) * 120);

	  				my = (int) (300 - Math.cos(Math.toRadians(6 * m)) * 120);
	  			
	   		                hx = (int) (300 + Math.sin(Math.toRadians(h * 30 + m * 0.5)) * 90);
	  	
	  				hy = (int) (300 - Math.cos(Math.toRadians(h * 30 + m * 0.5)) * 90);

	  		
	    			       repaint();
	  		
	  			       try {
	  		
	  				Thread.sleep(1000);
	  	
	  				} catch (InterruptedException e) {
	  		
	  				e.printStackTrace();
	  		
	  			}

	            }				
	          }
	  			
	         }.start();

	       }

	  }


网友评论    (发表评论)

共1 条评论 1/1页

发表评论:

评论须知:

  • 1、评论每次加2分,每天上限为30;
  • 2、请文明用语,共同创建干净的技术交流环境;
  • 3、若被发现提交非法信息,评论将会被删除,并且给予扣分处理,严重者给予封号处理;
  • 4、请勿发布广告信息或其他无关评论,否则将会删除评论并扣分,严重者给予封号处理。


扫码下载

加载中,请稍后...

输入口令后可复制整站源码

加载中,请稍后...