黑马程序员技术交流社区

标题: java绘图绘制的时钟 [打印本页]

作者: 夕阳游子    时间: 2015-9-6 23:01
标题: java绘图绘制的时钟

import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JPanel;
public class Paint extends Frame implements Runnable{
private int   hour , minute, second;   
private int i=0;
private int j=0;
private int l=0;
private int x0=300;
private int y0=300;
Thread runner;
public void paint(Graphics g){
  long starttime=System.currentTimeMillis();
  starttime=(long)starttime/1000;
  second = (int) (starttime% 60);   
  starttime = (int)starttime / 60;   
     minute = (int)(starttime% 60)-15;   
     starttime= (int)(starttime)/60;   
     hour=(int)(starttime%12)+5;
     g.setColor(Color.blue);
     g.fillOval(200-10, 200-10, 220, 220);
     g.setColor(Color.black);
  g.fillOval(200, 200, 200, 200);
  g.setColor(Color.CYAN);
  g.fillOval(300-05,300-70,10,20);
  g.setColor(Color.white);
  for(int k=0;k<=60;k++){
   if(k%15==0){
   g.drawLine((int)(x0+(85*Math.cos((Math.PI)*k/30))),(int)(y0-(85*Math.sin(Math.PI*k/30))),(int)(x0+(100*Math.cos((Math.PI)*k/30))),(int)(y0-(100*Math.sin(Math.PI*k/30))));
   }else if(k%5==0){
    g.drawLine((int)(x0+(90*Math.cos((Math.PI)*k/30))),(int)(y0-(90*Math.sin(Math.PI*k/30))),(int)(x0+(100*Math.cos((Math.PI)*k/30))),(int)(y0-(100*Math.sin(Math.PI*k/30))));
   }
   else {g.drawLine((int)(x0+(97*Math.cos((Math.PI)*k/30))),(int)(y0-(97*Math.sin(Math.PI*k/30))),(int)(x0+(100*Math.cos((Math.PI)*k/30))),(int)(y0-(100*Math.sin(Math.PI*k/30))));
  }
   }
  g.drawLine(x0,y0,(int)(x0+(80*Math.cos((-Math.PI)*(second)/30))),(int)(y0-(80*Math.sin(-Math.PI*(second)/30))));
  g.drawLine(x0,y0,(int)(x0+(65*Math.cos(-Math.PI*minute/30))),(int)(y0-(65*Math.sin(-Math.PI*minute/30))));
  g.drawLine(x0,y0,(int)(x0+(45*Math.cos(-Math.PI*(hour*5+minute/12+1)/30))),(int)(y0-(45*Math.sin(-Math.PI*(5*hour+minute/12+1)/30))));
  }

public void run(){
  while(true){
   for(j=0;j<=60;j++){
    for(i=0;i<61;i++){
    repaint();
    try
    {
     Thread.sleep(1000);
    }catch(Exception   e){}
    }
   }
   l++;
  }
  
}
public Paint(String title){
  super(title);
  setSize(600,600);
  setBackground(Color.yellow);
  this.addWindowListener(new WindowAdapter()
  { public void windowClosing(WindowEvent e) { System.exit(0); } });   
  runner=new Thread(this);
  runner.start();
  }

public static void main(String[] args ){
  Paint frame=new Paint("时钟");
  frame.setVisible(true);
  
}
}
作者: as604049322    时间: 2015-9-6 23:08
光看代码好像很牛逼的样子




欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2