⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 movingcartoon.java

📁 针对越来越多的用户喜欢JAVA
💻 JAVA
字号:
import java.awt.*;
import java.applet.*;

public class MovingCartoon extends Applet implements Runnable
{
    int frameNumber = 0;
    int delay;
    int allFrame = 10;
    Thread animatorThread;

    Image offImage = null;
    Graphics offGraphics = null;
    Image images[];
    MediaTracker tracker;
    AudioClip  sound;

    public void init()
    {
        String str;
        int fps = 10;
        str = getParameter("fps");
        try
        {
            if (str != null)
	    {
                fps = Integer.parseInt(str);
            }
        } catch (Exception e) {}
        delay = (fps > 0) ? (1000 / fps) : 100;
        images = new Image[allFrame];
	tracker = new MediaTracker(this);
	for( int i = 1; i <= allFrame; i++ )
	{
			images[i-1] = getImage(getCodeBase(), "b" + i + ".gif");
			tracker.addImage(images[i-1], i-1);
	}
	try
        {
			tracker.waitForAll();
	}catch(Exception e){}
	try
        {
		    sound = getAudioClip(getDocumentBase(),"bark.au");
	}catch(Exception e){}
    }

    public void start()
    {
       if (animatorThread == null)
       {
            animatorThread = new Thread(this);
       }
       animatorThread.start();
       sound.loop();
    }

    public void stop()
    {
                animatorThread = null;
	        offImage = null;
		offGraphics = null;
		sound.stop();
    }

    public void run()
    {
       Thread.currentThread().setPriority(Thread.MIN_PRIORITY);
       long startTime = System.currentTimeMillis();
       while (Thread.currentThread() == animatorThread)
       {
            frameNumber++;
            repaint();
            try
            {
                startTime += delay;
                Thread.sleep(Math.max(0, 
                startTime-System.currentTimeMillis()));
            } catch (InterruptedException e) {
                break;
            }
        }
    }

    public void paint(Graphics g)
    {
		if( offImage != null )
		{
			g.drawImage(offImage, 0, 0, this);
		}
    }

    public void update(Graphics g)
    {
		Dimension d = size();
		if( offImage == null || offGraphics == null )
		{
			offImage = createImage(d.width, d.height);
			offGraphics = offImage.getGraphics();
		}

		offGraphics.setColor( getBackground() );
		offGraphics.fillRect(0, 0, d.width, d.height);
		offGraphics.setColor( Color.black );
		int no;
		no = frameNumber % allFrame;
		offGraphics.drawImage(images[no], 0, 0, this);
		g.drawImage(offImage, 0, 0, this);
    }
}

⌨️ 快捷键说明

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