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

📄 nowtime.java

📁 Java课程中所有的可运行程序 全部都是java课程中所涉及到的源码
💻 JAVA
字号:
import java.awt.*;
import java.applet.*;
import java.util.*;
import java.text.*;

public class nowtime extends Applet implements Runnable{
    Date nowdate;
   public Thread runner;
Clock myClock;


    public void init(){

     setBackground(Color.white);

    }



     public void start() {
    if (runner == null); {
     runner = new Thread(this);			  //new Thread
    runner.start();
    }
  }

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

public void run() {
  while(Thread.currentThread()==runner){
	  repaint();
	   try { Thread.sleep(1000); }
	   catch (InterruptedException e) { }


}

 }

public void update(Graphics g){
     myClock.clear(g,100,100,100,100,getBackground());
     g.setColor(getBackground());
     g.clearRect(20,200,300,100);
     g.setColor(getForeground());
     paint(g);
}

public void paint(Graphics g){


    nowdate=new Date();

   g.setColor(Color.red);
   SimpleDateFormat formatter=new SimpleDateFormat("s",Locale.getDefault());
   int s=Integer.parseInt(formatter.format(nowdate));
   formatter.applyPattern("m");
   int m=Integer.parseInt(formatter.format(nowdate));
   formatter.applyPattern("h");
   int h=Integer.parseInt(formatter.format(nowdate));

myClock=new Clock(h,m,s);

   formatter=new SimpleDateFormat("yyyy年MM月dd日 hh:mm:ss a");
   String today=formatter.format(nowdate);
 g.drawString(today,30 ,250);
 myClock.show(g,100,100,100,100);

        }

}

class Clock {
    int hour,minute,second;
    Clock(int h,int m,int s){
    hour=h % 12;
    minute=m;
    second=s;}

 void show(Graphics g,int center_x,int center_y,int len,int radius){
    int hlen=(int)(radius*0.5),mlen=(int)(radius*0.7),slen=(int)(radius*0.85);
    double theta;

    g.drawOval(center_x-radius,center_y-radius,radius*2,radius*2);
theta=(double)(hour*60*60+minute*60+second)/43200*2.0*Math.PI;
drawNiddle(g,Color.blue,center_x,center_y,hlen,theta);


theta=(double)(minute*60+second)/3600*2.0*Math.PI;
drawNiddle(g,Color.green,center_x,center_y,hlen,theta);

theta=(double)(second)/60*2.0*Math.PI;
drawNiddle(g,Color.red,center_x,center_y,hlen,theta);

    }

void clear(Graphics g,int center_x,int center_y,int len,int radius,Color clearColor){
    int hlen=(int)(radius*0.5),mlen=(int)(radius*0.7),slen=(int)(radius*0.85);
    double theta;


theta=(double)(hour*60*60+minute*60+second)/43200*2.0*Math.PI;
drawNiddle(g,clearColor,center_x,center_y,hlen,theta);


theta=(double)(minute*60+second)/3600*2.0*Math.PI;
drawNiddle(g,clearColor,center_x,center_y,hlen,theta);

theta=(double)(second)/60*2.0*Math.PI;
drawNiddle(g,clearColor,center_x,center_y,hlen,theta);

    }

 private void drawNiddle(Graphics g,Color c,int x,int y,int len,double theta)
 { g.setColor(c);
    g.drawLine(x,y,(int)(x+len*Math.sin(theta)),(int)(y-len*Math.cos(theta)));

}


}

⌨️ 快捷键说明

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