⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 clock.java

📁 java做的时钟
💻 JAVA
字号:
/*
 * Created on 2006-6-30
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */

/**
 * @author webb
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
import javax.swing.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.*;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
public class clock extends JFrame implements ActionListener,Runnable{
	Thread thread=null;
	int x0,y0,xh,yh,xm,ym,xs,ys,r,s,m,h;
	int i,j,k,y,mon,day;
	int delay=1000;
	int xx=-200,yy=310;
	int fromGMT;
    int currentZone;
    Image image=null;
    String week,ss,qq;
    Font font= null;
static final Object cityZone[][] = {
        {
            "Hawaii", "-10"
        }, {
            "Alaska", "-9"
        }, {
            "Arizona", "-7"
        }, {
            "Mexico", "-6"
        }, {
            "Brasilia", "-3"
        }, {
            "GMT", "0"
        }, {
            "Paris", "1"
        }, {
            "Cairo", "2"
        }, {
            "Moscow", "3"
        }, {
            "Jakarta", "7"
        }, {
            "Hong Kong", "8"
        }, {
            "Tokyo", "9"
        }, {
            "Sydney", "10"
        }
    };
   public void setCityZone(String s)
   {//由得到的地区参数设置currentZone的值
   // currentZone = fromGMT;
    for(int i = 0; i < cityZone.length; i++)
    {
        if(s.indexOf((String)cityZone[i][0]) != -1)
        {
            currentZone = Integer.parseInt((String)cityZone[i][1]);
        }
    }

}
	 public void init()
	    {
	     //初始化日期时间 
	        Date date = new Date();
	        fromGMT = -Math.round((float)date.getTimezoneOffset() / 60F);
	        //currentZone = fromGMT;
	    }
	public void paint(Graphics sg){
		int []xpointh=new int[5];
		int []ypointh=new int[5];
		int []xpointm=new int[5];
		int []ypointm=new int[5];
		final double bili=Math.PI/180;
	    Graphics2D g2D=(Graphics2D) sg;
	   // Insets insets=getInsets();  
	    Image t;
	    Graphics g;
	    ////////////双缓冲///////////
	    t=this.createImage(410,520);
	    g=t.getGraphics();
	    ////////////////////////
        ////画背景图///
	    g.drawImage(image,5,25,this);  
	    Date date = new Date();      
         i = (date.getHours() + currentZone) - fromGMT;//以24h制表示的时
        if(i >= 24)
        {
            i -= 24;
        }
        if(i < 0)
        {
            i += 24;
        }       
         j = date.getMinutes();//分
         k = date.getSeconds();//秒    
        Calendar now=new GregorianCalendar();
         y=now.get(Calendar.YEAR);
	     mon=now.get(Calendar.MONTH)+1;
	     day=now.get(Calendar.DATE);	     
        int w=(int)now.get(Calendar.DAY_OF_WEEK);	   
        switch(w)
		{
        case 1:week="星期天";break;
        case 2:week="星期一";break;
        case 3:week="星期二";break;
        case 4:week="星期三";break;
        case 5:week="星期四";break;
        case 6:week="星期五";break;
        case 7:week="星期六";break;
        default:break;
        }
 
	   ////// //时钟中点坐标/////////
	    x0=205;
	    y0=225;	 
	    ////////////////
        r=200;
        s=k*6-90;
        if(0<k&&k<=59)m=j*6+k/10-90;
        else m=j*6-90;
        if(0<j&&j<=59)h=i*30+j/2-90;
        else if(j==0)h=i*30-90;
      //计算时针五边形
	    xh=(int)(r*0.54*Math.cos(bili*h))+x0;
	    yh=(int)(r*0.54*Math.sin(bili*h))+y0;  
	     xpointh[0]=xh; 
	     ypointh[0]=yh;	
	     
	     xpointh[1]=x0+(int)(r*0.05*Math.cos(bili*(h+90))); 
		 ypointh[1]=y0+(int)(r*0.05*Math.sin(bili*(h+90)));		     		     
		
		 xpointh[4]=x0+(int)(r*0.05*Math.cos(bili*(h-90))); 
		 ypointh[4]=y0+(int)(r*0.05*Math.sin(bili*(h-90)));		     
		
		 xpointh[2]=x0+(int)(r*0.2*Math.cos(bili*(h+170))); 
		 ypointh[2]=y0+(int)(r*0.2*Math.sin(bili*(h+170)));		     		     
		
		 xpointh[3]=x0+(int)(r*0.2*Math.cos(bili*(h-170))); 
		 ypointh[3]=y0+(int)(r*0.2*Math.sin(bili*(h-170)));	     
        //绘制时针
	     g.setColor(Color.BLACK);
	     g.drawPolygon( xpointh, ypointh, 5);
	     g.fillPolygon(xpointh,ypointh,5);
    
        //计算分钟的五边形
	     xm=(int)(r*0.74*Math.cos(bili*m))+x0;
		 ym=(int)(r*0.74*Math.sin(bili*m))+y0;  
		     xpointm[0]=xm; 
		     ypointm[0]=ym;	
		     
		     xpointm[1]=x0+(int)(r*0.06*Math.cos(bili*(m+90))); 
			 ypointm[1]=y0+(int)(r*0.06*Math.sin(bili*(m+90)));		     		     
			
			 xpointm[4]=x0+(int)(r*0.06*Math.cos(bili*(m-90))); 
			 ypointm[4]=y0+(int)(r*0.06*Math.sin(bili*(m-90)));		     
			
			 xpointm[2]=x0+(int)(r*0.2*Math.cos(bili*(m+170))); 
			 ypointm[2]=y0+(int)(r*0.2*Math.sin(bili*(m+170)));		     		     
			
			 xpointm[3]=x0+(int)(r*0.2*Math.cos(bili*(m-170))); 
			 ypointm[3]=y0+(int)(r*0.2*Math.sin(bili*(m-170)));	     
	        //绘制分针
		     g.setColor(Color.BLACK);
		     g.drawPolygon( xpointm, ypointm, 5);
		     g.fillPolygon(xpointm,ypointm,5);     
        
	        //绘制秒针
			    xs=(int)(r*0.85*Math.cos(bili*s))+x0;
			    ys=(int)(r*0.85*Math.sin(bili*s))+y0;
			    g.setColor(Color.BLACK);
			    g.drawLine(x0,y0,xs,ys);			   
			   // g.fillOval(x0,y0,3,3);
			    //走马灯   				
			  zoumadengzuo(g);
			  zoumadengyou(g);
			  sg.drawImage(t,0,0,this);
	    }
	public void zoumadengyou(Graphics g)
	{
		start();
		g.setColor(Color.MAGENTA);
		font=new Font("Monospaced",Font.ITALIC,20);
		g.setFont(font);
		qq="计算机学院  软件(2)班  陈伟  3103003333"; 
		g.drawString(qq,yy,470);
		yy-=2;
		if(yy+qq.length()<=-360)
		{
		  yy=410;	
		}	
	}
	public  void zoumadengzuo(Graphics g){	
		start();
		g.setColor(Color.BLUE);
        g.setFont(font);
        font=new Font("TimesRoman",Font.ITALIC,20);
		 ss=y+"年"+"   "+mon+"月"+day+"日"+"   "+week+"   "+String.valueOf(i)+":"+String.valueOf(j)+":"+String.valueOf(k);  
		g.drawString(ss,xx,448);
		xx+=2;
		if(xx-ss.length()>=380)
		{
		   xx=-350;	
		}		
}

	public void start(){
		if(thread==null){
			thread=new Thread(this);
			thread.start();
		}
		if(thread!=null)
			try{
				Thread.sleep(10);
		}
		catch(InterruptedException e){}
	}	

	public void stop(){
	        if(thread!= null)
	        {
	            thread.stop();
	            thread = null;
	        }
	}

    public void run(){
		while(thread!=null){
			try{
				Thread.sleep(10);   
			}
			catch(InterruptedException e){}
			repaint();
		}
		thread=null;
	}
	 public clock(){
	 	super("走马灯时钟  软件(2)班  陈伟  3103003333");
	 	start();	
	   image=getToolkit().getImage("2.JPG");
	   setSize(410,485);
	   setBackground(Color.WHITE);
	   setLocation(300,150);
	   setResizable(false);
	   show();
	   ActionListener drawClock=new ActionListener(){
	     public void actionPerformed(ActionEvent evt){
	       repaint();	
	     }	
	   };
	   //时间计数器
	   new Timer(delay,drawClock).start();  
	 } 
	  public void actionPerformed(ActionEvent e){
	  	repaint();
	  }

	public static void main(String[] args) {
		 clock clock1=new clock();
		// clock1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}
	
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -