例8-14.java

来自「医院无线局域网设计方案 医院无线局域网设计方案」· Java 代码 · 共 97 行

JAVA
97
字号
//Example 8-14
import java.awt.*;
import java.awt.event.*;
import java.applet.Applet;
public class tt extends Applet implements Runnable,MouseListener
{	
  int frameNumber,delay,allFrame=10;
  Thread animatorThread;
  Dimension offDimension;
  Image offImage,images[];
  Graphics offGraphics;
  MediaTracker tracker;
  String s;
  public void init()
  {
    String str;
    addMouseListener(this);
    str=getParameter("fps");
    int fps=(str!=null)?Integer.parseInt(str):10;
    delay=(fps>0)?(1000/fps):100;
    images=new Image[allFrame];
    tracker=new MediaTracker(this);
    for(int i=0;i<allFrame;i++)
    {
      images[i]=getImage(getCodeBase(),"T"+i+".gif");
      tracker.addImage(images[i],0);
    }
  }
  public void start()
  {
    if(animatorThread==null)
    {
      animatorThread=new Thread(this);
      animatorThread.start();
    } 
  }
  public void stop()
  {
    animatorThread=null;
    offImage=null;
    offGraphics=null;
  }
  public void mouseClicked(MouseEvent e) { }
  public void mouseEntered(MouseEvent e) { }
  public void mouseExited(MouseEvent e) { }
  public void mousePressed(MouseEvent e)
  {
    if(animatorThread==null)
      start();
    else
      animatorThread=null; 
  }
  public void mouseReleased(MouseEvent e) { }
  
  public void run()
  {
    long startTime=System.currentTimeMillis();
    while(Thread.currentThread()==animatorThread)
    {
      repaint();
      try
      {
        startTime+=delay;
        Thread.sleep(Math.max(0,startTime-System.currentTimeMillis()));
      }
      catch(InterruptedException e)
      {
        break;
      }
      frameNumber++;
    }
  }
  public void paint(Graphics g)
  {
    if(offImage!=null)
      g.drawImage(offImage,0,0,this);
  }
  public void update(Graphics g)
  {
    Dimension d=getSize();
    if((offGraphics==null)||(d.width!=offDimension.width)||(d.height!=offDimension.height))
    {
      offDimension=d;
      offImage=createImage(d.width,d.height);
      offGraphics=offImage.getGraphics();
    }
    offGraphics.setColor(getBackground());
    offGraphics.fillRect(0,0,d.width,d.height);
    offGraphics.setColor(Color.black);
    if(tracker.statusID(0,true)==MediaTracker.COMPLETE)
    {
      offGraphics.drawImage(images[frameNumber%allFrame],0,0,this);
    }
    g.drawImage(offImage,0,0,this);
  }
} 

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?