JAVA秒表

import javax.swing.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.*;
import java.util.Calendar;
import java.util.GregorianCalendar;
public class TimerTest extends JApplet implements ActionListener


  Timer timer;
  int i=0;
  int x0=150,y0=200;
  int r=100;
  int xPos,yPos;                               //圆周上画*的点的坐标
  int old_xPos=150,old_yPos=200;
  int xPos1,yPos1;

   public TimerTest()
  {
     
      int delay = 1000;
      ActionListener drawClock = new ActionListener()
      {
          public void actionPerformed(ActionEvent evt)
         {
                   repaint();
     i++;
         }
       };
       timer=new Timer(delay,drawClock);
       timer.start();
       
   }
    public void actionPerformed(ActionEvent e)
    {
     
    }

    public void init()
          
        JButton bt1=new JButton("复位");
        ButtonHandler handle1=new ButtonHandler();
        bt1.addActionListener(handle1);
        Container contentPane=getContentPane();     //得到小程序的内容面板
        contentPane.add(bt1,BorderLayout.EAST);
    }

    public void paint(Graphics g)
     

        setBackground(Color.BLUE);
        double alfa,beta,beta1;                                 //画圆点对应的角度
        for(int i=0;i<360;i+=30)
        {
         alfa=Math.toRadians(i);                    //把角度用弧度来表示
         xPos=x0+(int)(r*Math.cos(alfa));           //求画点的x坐标
         yPos=y0-(int)(r*Math.sin(alfa));           //求画点的y坐标
         g.drawString("*",xPos,yPos);                //在该点画*
        }
        //Calendar now= new GregorianCalendar();
        //int nows= now.get(Calendar.SECOND);
  int nows=i-15;      
 if(i==0)
        {
   g.setColor(Color.BLUE);
   g.drawLine(x0,y0,old_xPos,old_yPos);   
 }
        beta=Math.toRadians(0-6*nows%360);
        beta1=Math.toRadians(0-6*(nows-1)%360);
        xPos1=x0+(int)(r*Math.cos(beta));
        yPos1=y0-(int)(r*Math.sin(beta));
        g.setColor(Color.BLACK);
        g.drawLine(x0,y0,xPos1,yPos1);
        g.setColor(Color.BLUE);
        old_xPos=x0+(int)(r*Math.cos(beta1));    
        old_yPos=y0-(int)(r*Math.sin(beta1));
        g.drawLine(x0,y0,old_xPos,old_yPos);
    }

    private class ButtonHandler implements ActionListener
   {
     public void actionPerformed(ActionEvent event)
     {
      JButton button = (JButton) event.getSource();
      if (button.getText().equals("复位"))
      {
        repaint();  
 i=0;
 old_xPos=xPos1;
 old_yPos=yPos1;
     
     }
   }

    public static void main(String[] args)
    {
          TimerTest c = new TimerTest();
    }

  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值