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

📄 jdkclock.java

📁 这是一个用java语言编写的小型具有提醒功能的时间提示工具
💻 JAVA
字号:
//jdkclock.java程序(appletviewer 下运行)
import java.awt.*;
import java.util.*;
import java.applet.*;
import java.text.*;
import java.awt.event.*;

public class jdkclock extends Applet implements Runnable{
  Image clockframe,blank,image[];    //分别为闹钟框架、刷新画面以及定点动画
  Thread timerthread=null;	//创建线程
  AudioClip ipau,birdau;	//时钟音乐以及闹铃音乐

  ControlPanel control;         //控制面板
  int i=0,j,ii=0;
  int s,m,h;
  Integer ts,tm,th=null;
  int ss,mm,hh;
  int mode=0;
  boolean candraw=true;
  boolean canpaint=false;
  boolean flag=false;
  boolean strike=true;
  int counter=0;
  int lasts;

  Image offscreen_buf = null;	
  Graphics offg;
  int timeout= 200;
  String gif_name[]={"1","2","3","4","5"};        //定点动画的文件名
  int lastxs=0, lastys=0, lastxm=0, lastym=0, lastxh=0, lastyh=0;
  Date dummy = new Date();
  GregorianCalendar cal = new GregorianCalendar();
  SimpleDateFormat df = new SimpleDateFormat("yyyy MM dd HH:mm:ss ");
  String lastdate = df.format(dummy);
  Font F1=new Font("Dialog",Font.PLAIN,20);
  Font F2=new Font("TimesRoman", Font.PLAIN, 14);
  Font F3=new Font("Dialog",Font.PLAIN,14);
  Date dat = null;

  public void init(){		//初始化方法
    ipau=getAudioClip(getCodeBase(),"ip.au");		//获取声音
    birdau=getAudioClip(getCodeBase(),"bird.au");

    offscreen_buf = createImage(300,450);
    offg = offscreen_buf.getGraphics();

    image =new Image[5];

    int xcenter,ycenter,s,m,h;		 //初始化指针
    xcenter=149;
    ycenter=294;

    s = (int)cal.get(Calendar.SECOND);                      
    m = (int)cal.get(Calendar.MINUTE);
    h = (int)cal.get(Calendar.HOUR_OF_DAY);

    if(h<4) h=h+24-4;
    else h=h-4;

    lastxs = (int)(Math.cos(s * 3.14f/30 - 3.14f/2) * 30 + xcenter);
    lastys = (int)(Math.sin(s * 3.14f/30 - 3.14f/2) * 30 + ycenter); 
    lastxm = (int)(Math.cos(m * 3.14f/30 - 3.14f/2) * 25 + xcenter);
    lastym = (int)(Math.sin(m * 3.14f/30 - 3.14f/2) * 25 + ycenter);
    lastxh = (int)(Math.cos((h*30 + m/2) * 3.14f/180 - 3.14f/2) * 18 + xcenter);
    lastyh = (int)(Math.sin((h*30 + m/2) * 3.14f/180 - 3.14f/2) * 18 + ycenter);
    lasts=s;

    clockframe=getImage(getDocumentBase(),"clockframe.gif");	//加载图片
    for(j = 0; j<5; j++){
       image[j] = getImage(getCodeBase(),gif_name[j]+".gif");
    }
    blank = getImage(getDocumentBase(),"blank.gif");

    setLayout(new BorderLayout());          //设置控制面板
    setBackground(Color.pink);
    control=new ControlPanel(this);
    control.setSize(200,450);
    add("East",control); 
  }
 
   public void paint(Graphics g){		//paint()方法
     int xh, yh, xm, ym, xs, ys,strike_times;
     int xcenter,ycenter;
     String today;
     Integer gif_number;
     xcenter=149;
     ycenter=294;
     dat = new Date();		//获取当前时间
     cal.setTime(dat);
     s = (int)cal.get(Calendar.SECOND);
     m = (int)cal.get(Calendar.MINUTE);
     h = (int)cal.get(Calendar.HOUR_OF_DAY);
     today = df.format(dat);

     if(h<4) h=h+24-4;
     else h=h-4;

     //指针位置
     xs = (int)(Math.cos(s * 3.14f/30 - 3.14f/2) * 30 + xcenter);
     ys = (int)(Math.sin(s * 3.14f/30 - 3.14f/2) * 30 + ycenter);
     xm = (int)(Math.cos(m * 3.14f/30 - 3.14f/2) * 25 + xcenter);
     ym = (int)(Math.sin(m * 3.14f/30 - 3.14f/2) * 25 + ycenter);
     xh = (int)(Math.cos((h*30 + m/2) * 3.14f/180 - 3.14f/2) * 18 + xcenter);
     yh = (int)(Math.sin((h*30 + m/2) * 3.14f/180 - 3.14f/2) * 18 + ycenter);

     g.setFont(F2);		 //设置字体和颜色
     g.setColor(Color.pink);
     g.fillRect(1,1,299,449);

     g.drawImage(clockframe,0,60,this);    //画钟面
     g.setColor(Color.blue);
     g.drawString(today, 100, 405);       //以数字方式显示日期和时间

     //画指针
     g.setColor(Color.orange);		
     g.drawLine(xcenter, ycenter, xs, ys);    
     g.setColor(Color.red);
     g.drawLine(xcenter, ycenter-1, xm, ym);
     g.drawLine(xcenter-1, ycenter, xm, ym);
     g.drawLine(xcenter, ycenter-1, xh, yh);
     g.drawLine(xcenter-1, ycenter, xh, yh);

     lastxs=xs; lastys=ys;
     lastxm=xm; lastym=ym;
     lastxh=xh; lastyh=yh;
     lastdate = today;

     strike_times=8;

     //判断定点报时
     if(canpaint){
     ss=ts.intValue();
     mm=tm.intValue();
     hh=th.intValue();


     if(h>12) h=h-12;
     else h=h;

//     System.out.println("s="+s+' '+"ss="+ss+'\n');
//     System.out.println("m="+m+' '+"mm="+mm+'\n');
//     System.out.println("h="+h+' '+"hh="+hh+'\n');

     if((s==ss && m==mm && h==hh)||flag){                
       if (counter<strike_times){
         flag=true;
         g.drawImage(blank,96,242,this);        
         g.setColor(Color.red);
         g.setFont(F3);

	 //根据所选项自主设定定点时的动画和文本
         switch(mode){
           case 0:{
             g.drawString("快起床,迎接美好一天" ,100,345);             
             g.drawImage(image[0],96,242,this);
             break;
           }
           case 1:{
             g.drawString( "吃饭啦,快去享用美味佳肴",100,345);             
             g.drawImage(image[1],96,242,this);
             break;
           }
           case 2: {
             g.drawString( "该休息了,睡眠充足才会精神好",100,345);             
             g.drawImage(image[2],96,242,this);
             break;
           }
           case 3:{
             g.drawString( "该去赴约了,别让人久等!" ,100,345);             
             g.drawImage(image[3],96,242,this);
             break;
           }
           case 4:{
             g.drawString("快,时间到了!" ,100,345);             
             g.drawImage(image[4],96,242,this);
             break;
           }
         }
          
         if( lasts != s){
            if(strike){
               counter++;
               birdau.play();		//播放闹铃声
             }
            if (strike) strike=false;
            else strike=true;
         }
       }
       else  {
         counter=0;
         flag=false;
       }
     }    
     else
       if( lasts!=s)
         ipau.play();		//播放滴答声
     }
     else
       if( lasts!=s)
         ipau.play();

     lasts = s;
     dat=null;

     //设定闪烁文本
     if(candraw){
       ii+=1;
       if(ii==5) ii=0;
     }
     switch(ii)
     {
        case 0: g.setColor(Color.black);
                break;
        case 1: g.setColor(Color.white);
                break;
        case 2: g.setColor(Color.green);
                break;
        case 3: g.setColor(Color.blue);
                break;
        case 4: g.setColor(Color.red);
                break;
      }
      g.setFont(F1);     
      g.drawString(" 欢迎使用咪咪闹钟", 70,30);
    candraw=false;
  }

  public void start() {
    if(timerthread == null)
    {
      timerthread = new Thread(this);		//将timerthread实例化
      timerthread.start();
    }
  }

  public void stop() {
    timerthread = null;
  }

  public void run() {
    while (timerthread != null) {
      try {timerthread.sleep(timeout);}
      catch (InterruptedException e){}
      candraw=true;
      repaint();       //刷新画面
    }
    timerthread = null;
  }
  
  public void setmode(int mode){	// 设置mode参数的方法
    this.mode=mode;
  }

  public void setts(Integer ts){	// 设置ts参数的方法
    this.ts=ts;
  }

  public void settm(Integer tm){	// 设置tm参数的方法
    this.tm=tm;
  }

  public void setth(Integer th){	// 设置th参数的方法
    this.th=th;
  }

  public void setcounter(int i){        // 设置counter参数的方法
    this.counter=i;
  }


  public void setcanpaint(boolean b){	// 设置canpaint参数的方法
    this.canpaint=b;
  }
  public void setflag(boolean b){   // 设置flag参数的方法
    this.flag=b;
  }


  //采用双缓冲技术的update()方法
  public void update(Graphics g){
    if (offscreen_buf == null){
      offscreen_buf = createImage(300,450);
    }
    offg.clipRect(1,1,299,449);
    paint(offg);
    g.drawImage (offscreen_buf,0,0,this);
  }
}

//自定义的Controlpanel类 
class ControlPanel extends Panel implements ItemListener,ActionListener {
  jdkclock jdkclock;
  Panel timer;
  TextField h;
  TextField m;
  TextField s;
  Label error;
  Choice c;
  Button OK;  
  Button CLEAR;

  //Controlpanel类的构造方法
  public ControlPanel(jdkclock jc){
    jdkclock=jc;
    setLayout(new BorderLayout());
    timer=new Panel();
    timer.setLayout(new GridLayout(12,1));
    timer.add(new Label());
    timer.add(new Label());
    timer.add(new Label());
    timer.add(new Label());
                   
    Panel p1=new Panel();
    p1.setLayout(new FlowLayout(FlowLayout.LEFT));
    p1.add(new Label("闹钟定时:"));
    p1.add(h=new TextField());
    p1.add(new Label("时"));
    p1.add(m=new TextField());
    p1.add(new Label("分"));
    p1.add(s=new TextField());
    p1.add(new Label("秒"));
    timer.add(p1);

    Panel p2=new Panel();
    p2.setLayout(new FlowLayout(FlowLayout.LEFT));
    p2.add(new Label("动画:       "));
    c=new Choice();
    c.addItemListener(this);

    c.addItem("flower1----起床");
    c.addItem("flower2---吃饭");
    c.addItem("flower3---睡觉");
    c.addItem("heart-----约会");
    c.addItem("clock-----其他");

    p2.add(c);
    timer.add(p2);

    timer.add(error=new Label(" 闹钟未定时."));
    timer.add(new Label());

    Panel p3=new Panel();
    p3.setLayout(new FlowLayout(FlowLayout.CENTER));
    OK=new Button("定时");
    OK.addActionListener(this); 
    CLEAR=new Button("清除");
    CLEAR.addActionListener(this);

    p3.add(OK);
    p3.add(CLEAR);
    timer.add(p3);  

    timer.add(new Label());   
    Panel p4=new Panel();
    p4.setLayout(new FlowLayout(FlowLayout.CENTER));
    p4.add(new Label("咪咪闹钟"));
    p4.add(new Label());
   
    timer.add(p4);
    timer.add(new Label());
    
    add("East",timer);
     
  }             

  //组件的控制方法 
 
  public void itemStateChanged(ItemEvent e) {
    if (e.getSource() instanceof Choice) {
      jdkclock.setmode(c.getSelectedIndex());      //传递mode参数
    }
  }
  public void actionPerformed(ActionEvent evt) {
    String arg = evt.getActionCommand();
      if ("定时".equals(arg)) {
        try{
          jdkclock.setts(new Integer(s.getText()));		//传递ts参数
          jdkclock.settm(new Integer(m.getText()));                //传递tm参数
          jdkclock.setth(new Integer(h.getText()));  		//传递th参数

          error.setText(" 咪咪闹钟已定时为:  "+h.getText()+":"+m.getText()+":"+s.getText());
          jdkclock.setcanpaint(true);        //传递canpaint参数
          jdkclock.setcounter(0);        //传递counter参数
        }
        catch(NumberFormatException e){
          error.setText(" 请输入正确时间.");
          s.setText(null);
          m.setText(null);
          h.setText(null);
          jdkclock.setcanpaint(false);        //传递canpaint参数
          jdkclock.setflag(false);        //传递flag参数
        }
      }
      else if ("清除".equals(arg)) {
        s.setText(null);
        m.setText(null);
        h.setText(null);
        error.setText(" 闹钟未定时.");
        jdkclock.setcanpaint(false);            //传递canpaint参数
        jdkclock.setflag(false);        //传递flag参数
     }
   }  
}

⌨️ 快捷键说明

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