现在的位置: 首页 > 综合 > 正文

【原创】java applet Clock + Timer时钟或者电子钟也行,计时功能

2013年10月18日 ⁄ 综合 ⁄ 共 8237字 ⁄ 字号 评论关闭
Code:
  1. /*  
  2.  * To change this template, choose Tools | Templates  
  3.  * and open the template in the editor.  
  4.  */  
  5.   
  6. package clock;   
  7.   
  8. import java.applet.Applet;   
  9. import java.awt.BasicStroke;   
  10. import java.awt.Button;   
  11. import java.awt.Color;   
  12. import java.awt.Font;   
  13. import java.awt.Graphics;   
  14. import java.awt.Graphics2D;   
  15. import java.awt.TextArea;   
  16. import java.awt.TextField;   
  17. import java.awt.event.ActionEvent;   
  18. import java.awt.event.ActionListener;   
  19. import java.util.Date;   
  20. import java.util.Timer;   
  21. import javax.swing.JOptionPane;   
  22.   
  23. /**  
  24.  *  
  25.  * @author BLH  
  26.  */  
  27. public class Clock extends Applet implements ActionListener   
  28. {   
  29.     //钟表变量   
  30.     private int iHour;   
  31.     private int iMinute;   
  32.     private int iSecond;   
  33.   
  34.     private Button changeTime = new Button("修改时间");   
  35.     private Button calculagraphStart = new Button("开始计时");   
  36.     private Button calculagraphRecord = new Button("记录计时");   
  37.     private Button calculagraphEnd = new Button("结束计时");   
  38.   
  39.     private TextField time = new TextField(15);   
  40.     private TextArea record = new TextArea(10,15);   
  41.   
  42.     private Timer timer = new Timer();//钟表计时器间隔为1秒   
  43.     private Timer calculagraph;//计时器间隔为10毫秒   
  44.   
  45.     //计时器变量   
  46.     private int iTimeHour;   
  47.     private int iTimeMinute;   
  48.     private int iTimeSecond;   
  49.     private int iTimeMilliSecond;   
  50.   
  51.     public Clock()   
  52.     {   
  53.         //将系统的时,分秒设定为当前时间   
  54.         Date timeCurrent=new Date();   
  55.         iHour=timeCurrent.getHours();   
  56.         iMinute=timeCurrent.getMinutes();   
  57.         iSecond=timeCurrent.getSeconds();   
  58.   
  59.         timer.schedule(new JobTime(), 01000);//在0秒后执行此任务,每次间隔1秒,如果传递一个Data参数,就可以在某个固定的时间执行这个任务.   
  60.   
  61.     }   
  62.     /**  
  63.      * Initialization method that will be called after the applet is loaded  
  64.      * into the browser.  
  65.      */  
  66.     @Override  
  67.     public void init() {   
  68.         // TODO start asynchronous download of heavy resources   
  69.         setLayout(null);   
  70.   
  71.         add(changeTime);   
  72.         changeTime.setBounds(160206525);   
  73.         changeTime.addActionListener(this);   
  74.   
  75.         add(calculagraphStart);   
  76.         calculagraphStart.setBounds(160506525);   
  77.         calculagraphStart.addActionListener(this);   
  78.   
  79.         add(calculagraphRecord);   
  80.         calculagraphRecord.setBounds(160806525);   
  81.         calculagraphRecord.addActionListener(this);   
  82.         calculagraphEnd.setEnabled(false);   
  83.   
  84.         add(calculagraphEnd);   
  85.         calculagraphEnd.setBounds(1601106525);   
  86.         calculagraphEnd.addActionListener(this);   
  87.         calculagraphRecord.setEnabled(false);   
  88.   
  89.         add(time);   
  90.         time.setBounds(2552010025);   
  91.         time.setEditable(false);   
  92.         add(record);   
  93.         record.setEditable(false);   
  94.         record.setBounds(255,50,100,100);   
  95.   
  96.         resize(400,200); // Set clock window size   
  97.     }   
  98.     // TODO overwrite start(), stop() and destroy() methods   
  99.   
  100.     @Override  
  101.     public void start()   
  102.     {   
  103.   
  104.     }   
  105.     @Override  
  106.     public void stop()   
  107.     {   
  108.   
  109.     }   
  110.     @Override  
  111.     public void destroy()   
  112.     {   
  113.         timer.cancel();   
  114.     }   
  115.     @Override  
  116.     public void paint(Graphics g )   
  117.     {   
  118.         Graphics2D g2=(Graphics2D)g;   
  119.            
  120.         //设置背景颜色   
  121.         setBackground(Color.white);    
  122.         //画表盘   
  123.         g2.setStroke(new BasicStroke(4.0f));   
  124.         g.setColor(Color.BLUE); //表盘颜色   
  125.         //g.fillOval(0, 0, 150, 150);   
  126.         g.drawOval(00150150);   
  127.         //getSize().height //获取窗口的高度   
  128.   
  129.         //绘制时钟上的12 个数字   
  130.         g.setColor(Color.lightGray); //字体颜色   
  131.         int angle=60,x,y,radius=65;   
  132.         for(int i=1;i<=12;i++)   
  133.         {   
  134.             x=(int)((radius)*Math.cos(Math.PI/180*angle));   
  135.             y=(int)((radius)*Math.sin(Math.PI/180*angle));   
  136.             g.drawString(""+i,x+75-4,75-y+6);   
  137.             angle-=30;   
  138.         }   
  139.         //画秒针   
  140.         g2.setStroke(new BasicStroke(2.2f));   
  141.         g.setColor(Color.green); //秒针颜色   
  142.         int radiusOfSecond = 50;   
  143.         g.drawLine(75,75,(int)(radiusOfSecond*Math.cos(Math.PI/180*(-6*(iSecond-15))))+75,75-(int)(radiusOfSecond*Math.sin(Math.PI/180*(-6*(iSecond-15)))));   
  144.   
  145.         //画分针   
  146.         g2.setStroke(new BasicStroke(2.2f));   
  147.         g.setColor(Color.blue); //秒针颜色   
  148.         int radiusOfMinute = 45;   
  149.         g.drawLine(75,75,(int)(radiusOfMinute*Math.cos(Math.PI/180*(-6*(iMinute-15))))+75,75-(int)(radiusOfMinute*Math.sin(Math.PI/180*(-6*(iMinute-15)))));   
  150.   
  151.         //画时针   
  152.         g2.setStroke(new BasicStroke(2.2f));   
  153.         g.setColor(Color.red); //秒针颜色   
  154.         int radiusOfHour = 40;   
  155.         g.drawLine(75,75,(int)(radiusOfHour*Math.cos(Math.PI/180*(-30*(iHour%12-3))))+75,75-(int)(radiusOfHour*Math.sin(Math.PI/180*(-30*(iHour%12-3)))));   
  156.         //显示时分秒的值   
  157.         g.setFont(new Font("TimesToman",Font.PLAIN,14));   
  158.         g.setColor(Color.orange);   
  159.   
  160.         g.drawString(String.valueOf(iHour+" :"), 40180);   
  161.         g.drawString(String.valueOf(iMinute+" :"), 65180);   
  162.         g.drawString(String.valueOf(iSecond), 90180);   
  163.     }   
  164.   
  165.     public void actionPerformed(ActionEvent e)   
  166.     {   
  167.         if(e.getSource()==changeTime)   
  168.         {   
  169.             setCurrentTime();   
  170.         }   
  171.         if(e.getSource()==calculagraphStart)   
  172.         {   
  173.             iTimeHour= 0;   
  174.             iTimeMinute =0;   
  175.             iTimeSecond = 0;   
  176.             iTimeMilliSecond = 0;   
  177.             calculagraph = new Timer();   
  178.             calculagraph.schedule(new JobTimer(),0,10);   
  179.   
  180.             calculagraphEnd.setEnabled(true);   
  181.             calculagraphRecord.setEnabled(true);   
  182.             calculagraphStart.setEnabled(false);   
  183.         }   
  184.   
  185.         if(e.getSource()==calculagraphRecord)   
  186.         {   
  187.             record.append(iTimeHour+":"+iTimeMinute+":"+iTimeSecond+":"+iTimeMilliSecond+"/n");   
  188.         }   
  189.   
  190.         if(e.getSource()==calculagraphEnd)   
  191.         {   
  192.             calculagraph.cancel();   
  193.             time.setText("");   
  194.             record.setText("");   
  195.             calculagraphEnd.setEnabled(false);   
  196.             calculagraphRecord.setEnabled(false);   
  197.             calculagraphStart.setEnabled(true);   
  198.         }   
  199.     }   
  200.   
  201.     //用于改变时分秒的指针值的计时类   
  202.     class JobTime extends java.util.TimerTask   
  203.     {   
  204.         @Override  
  205.         public void run()   
  206.         {   
  207.             // TODO Auto-generated method stub   
  208.             iSecond++;   
  209.             if(iSecond==60)   
  210.             {   
  211.                 iSecond=0;   
  212.                 iMinute++;   
  213.             }   
  214.             if(iMinute==60)   
  215.             {   
  216.                 iMinute=0;   
  217.                 iHour++;   
  218.             }   
  219.   
  220.             if(iHour==24)   
  221.             {   
  222.                 iHour=0;   
  223.             }   
  224.             repaint();   
  225.         }   
  226.     }   
  227.   
  228.     private void setCurrentTime()   
  229.     {   
  230.         String strTemp = JOptionPane.showInputDialog(null"请输入当前小时(24小时制):");   
  231.         if(strTemp==null)   
  232.         {   
  233.             return;   
  234.         }   
  235.         int iHour = Integer.parseInt(strTemp);   
  236.   
  237.         strTemp = JOptionPane.showInputDialog(null"请输入当前分:");   
  238.   
  239.         if(strTemp==null)   
  240.         {   
  241.             return;   
  242.         }   
  243.   
  244.         int iMinute = Integer.parseInt(strTemp);   
  245.   
  246.         strTemp = JOptionPane.showInputDialog(null"请输入当前秒:");   
  247.   
  248.         if(strTemp==null)   
  249.         {   
  250.             return;   
  251.         }   
  252.            
  253.         int iSecond = Integer.parseInt(strTemp);   
  254.   
  255.        //设定当前时间为对话框输入的时间   
  256.         if (iHour >= 0 && iHour < 24)   
  257.         {   
  258.             this.iHour = iHour;   
  259.         }   
  260.   
  261.         if (iMinute >= 0 && iMinute < 60)   
  262.         {   
  263.             this.iMinute = iMinute;   
  264.         }   
  265.   
  266.         if (iSecond >= 0 && iSecond < 60)   
  267.         {   
  268.             this.iSecond = iSecond;   
  269.         }   
  270.     }   
  271.   
  272.     //计时器任务类   
  273.     class JobTimer extends java.util.TimerTask   
  274.     {   
  275.         @Override  
  276.         public void run()   
  277.         {   
  278.             // TODO Auto-generated method stub   
  279.             iTimeMilliSecond++;   
  280.             if(iTimeMilliSecond==100)   
  281.             {   
  282.                 iTimeSecond++;   
  283.                 iTimeMilliSecond=0;   
  284.             }   
  285.             if(iTimeSecond==60)   
  286.             {   
  287.                 iTimeMinute++;   
  288.                 iTimeSecond=0;   
  289.             }   
  290.             if(iTimeMinute==60)   
  291.             {   
  292.                 iTimeHour++;   
  293.                 iTimeMinute=0;   
  294.             }   
  295.             time.setText(iTimeHour+":"+iTimeMinute+":"+iTimeSecond+":"+iTimeMilliSecond);   
  296.         }   
  297.     }   
  298. }   

 

抱歉!评论已关闭.