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();
}
}