日期:2014-05-20  浏览次数:20832 次

Timer构造器的问题
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.util.*;
import java.io.*;
import javax.swing.Timer.*;
import java.awt.geom.*;
import java.net.*;
import java.applet.*;
import java.util.Timer;

public class Clock extends Canvas implements ActionListener //刻画时钟
{  
  Date date;
  Timer secondtime;
  int hour,minute,second;
  int a,b,c;
  Line2D hourline,minuteline,secondline;
  URL url;
  AudioClip clip;
  double piontSX[] = new double[60]; //表示秒钟端点坐标
  double piontSY[] = new double[60];
  double piontMX[] = new double[60];
  double piontMY[] = new double[60];
  double piontHX[] = new double[60];
  double piontHY[] = new double[60];
  Clock()
  {
  secondtime = new Timer(1000,this);
  piontSX[0] = 0; //12点秒针坐标
  piontSY[0] = -100;
  piontMX[0] = 0;
  piontMY[0] = -90;
  piontHX[0] = 0;
  piontHY[0] = -70;
  double angle = 6 * Math.PI / 180;
  for(int i = 0;i < 59;i ++) //计算出各数组中的坐标
  {
  piontSX[i + 1] = piontSX[i] * Math.cos(angle) - Math.sin(angle) * piontSY[i];
  piontSY[i + 1] = piontSY[i] * Math.cos(angle) + piontSX[i] * Math.sin(angle);
  piontMX[i + 1] = piontMX[i] * Math.cos(angle) - Math.sin(angle) * piontMY[i];
  piontMY[i + 1] = piontMY[i] * Math.cos(angle) + piontMX[i] * Math.sin(angle);
  piontHX[i + 1] = piontHX[i] * Math.cos(angle) - Math.sin(angle) * piontHY[i];
  piontHY[i + 1] = piontHY[i] * Math.cos(angle) + piontHX[i] * Math.sin(angle);
  }
  for(int i = 0;i < 60;i ++) //坐标平移
  {
  piontSX[i] = piontSX[i] + 120;
  piontSY[i] = piontSY[i] + 120;
  piontMX[i] = piontMX[i] + 120;
  piontMX[i] = piontMX[i] + 120;
  piontHX[i] = piontHX[i] + 120;
  piontHX[i] = piontHX[i] + 120;
  }
  secondline = new Line2D.Double(0,0,0,0);
  minuteline = new Line2D.Double(0,0,0,0);
  hourline = new Line2D.Double(0,0,0,0);
secondtime.start();
  }
   
  public void paint(Graphics g)
  {
  for(int i = 0;i < 60;i ++)
  {
  int m = (int)piontSX[i];
  int n = (int)piontSY[i];
  if(i % 5 == 0)
  {
  g.setColor(Color.red);
  g.fillOval(m - 4, n - 4, 8, 8);
  }
  else
  {
  g.setColor(Color.cyan);
  g.fillOval(m - 2 , n - 2, 4, 4);
  }
  }
  g.fillOval(115,115,10,10);
  Graphics2D g_2d = (Graphics2D)g; //刻画秒针
  g_2d.setColor(Color.red);
  g_2d.draw(secondline);
  //刻画分针
  BasicStroke bs = new BasicStroke(3f,BasicStroke.CAP_ROUND,BasicStroke.JOIN_MITER);
  g_2d.setStroke(bs);
  g_2d.setColor(Color.blue);
  g_2d.draw(minuteline);
  bs = new BasicStroke(6f,BasicStroke.CAP_ROUND,BasicStroke.JOIN_MITER);
  g_2d.setStroke(bs);
  g_2d.setColor(Color.green);
  g_2d.draw(hourline);
  }
  public void actionPerformed(ActionEvent e)
  {
  if(e.getSource() == secondtime)