利用Java Applet编程实现动画特技(二)

2014-11-23 21:59:47 · 作者: · 浏览: 1
atter.format(currentDate));
   } catch (NumberFormatException n) {
    m = 10;
   }
   formatter.applyPattern("h");
   try {
    h = Integer.parseInt(formatter.format(currentDate));
   } catch (NumberFormatException n) {
    h = 10;
   }
   formatter.applyPattern("EEE MMM dd HH:mm:ss yyyy");
   today = formatter.format(currentDate);
   //设置时钟的表盘的中心点为(80,55)
   xcenter=80;
   ycenter=55;

   // a= s* pi/2 - pi/2 (to switch 0,0 from 3:00 to 12:00)
   // x = r(cos a) + xcenter, y = r(sin a) + ycenter

   xs = (int)(Math.cos(s * 3.14f/30 - 3.14f/2) * 45 + xcenter);
   ys = (int)(Math.sin(s * 3.14f/30 - 3.14f/2) * 45 + ycenter);
   xm = (int)(Math.cos(m * 3.14f/30 - 3.14f/2) * 40 + xcenter);
   ym = (int)(Math.sin(m * 3.14f/30 - 3.14f/2) * 40 + ycenter);
   xh = (int)(Math.cos((h*30 + m/2) * 3.14f/180 - 3.14f/2) * 30 + xcenter);
   yh = (int)(Math.sin((h*30 + m/2) * 3.14f/180 - 3.14f/2) * 30 + ycenter);

   //画时钟最外面的圆盘其中心在(xcenter,ycenter)半径为50
   g.setFont(clockFaceFont);
   g.setColor(handColor);
   circle(xcenter,ycenter,50,g);
   //画时钟表盘里的数字
   g.setColor(numberColor);
   g.drawString("9",xcenter-45,ycenter+3);
   g.drawString("3",xcenter+40,ycenter+3);
   g.drawString("12",xcenter-5,ycenter-37);
   g.drawString("6",xcenter-3,ycenter+45);

   // 如果必要的话抹去然后重画
   g.setColor(getBackground());
   if (xs != lastxs || ys != lastys) {
    g.drawLine(xcenter, ycenter, lastxs, lastys);
    g.drawString(lastdate, 5, 125);
   }
   if (xm != lastxm || ym != lastym) {
    g.drawLine(xcenter, ycenter-1, lastxm, lastym);
    g.drawLine(xcenter-1, ycenter, lastxm, lastym); }
    if (xh != lastxh || yh != lastyh) {
     g.drawLine(xcenter, ycenter-1, lastxh, lastyh);
     g.drawLine(xcenter-1, ycenter, lastxh, lastyh); }
    g.setColor(numberColor);
    g.drawString("", 5, 125);
    g.drawString(today, 5, 125);
    g.drawLine(xcenter, ycenter, xs, ys);
    g.setColor(handColor);
    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;
    currentDate=null;
   }
   //applet的启动方法
   public void start()
   {
    timer = new Thread(this);
    timer.start();
   }
   // applet的停止方法
   public void stop()
   {
    timer = null;
   }
   //线程的run方法
   public void run()
   {
    Thread me =