specialimage.java

来自「java applet编程,实现对相关图片声音的调用」· Java 代码 · 共 124 行

JAVA
124
字号
import java.awt.*;
import java.applet.*;

public class SpecialImage extends Applet implements Runnable {
    int currentImage, nextImage;
	int imageW, imageH;
    int delay = 3000;
    int allFrame = 4;
    Thread animatorThread;

	int  centerX, centerY;

	Image offImage = null;
	Graphics offGraphics = null;

	Image images[];
	MediaTracker tracker;

    public void init()
	{
		//setBackground(Color.black);

        String str;
  
        //How many milliseconds between frames?
        str = getParameter("fps");
        try {
            if (str != null)
			{
                delay = Integer.parseInt(str);
            }
        } catch (Exception e) {}

        //Load Images
        images = new Image[allFrame];
		tracker = new MediaTracker(this);

		for( int i = 1; i <= allFrame; i++ )
		{
			images[i-1] = getImage(getCodeBase(), "QQ" + i + ".gif");
			tracker.addImage(images[i-1], i-1);
		}
		try{
			tracker.waitForAll();
		}catch(Exception e){}

		imageW = images[0].getWidth(this);
		imageH = images[0].getHeight(this);

		currentImage = 0;
		offImage = images[0];
    }

    public void start()
	{
        //Start animating!
        if (animatorThread == null) {
            animatorThread = new Thread(this);
        }
        animatorThread.start();
    }

    public void stop()
	{
        //Stop the animating thread.
        animatorThread = null;
		offImage = null;
		offGraphics = null;
    }

    public void run()
	{
        //Just to be nice, lower this thread's priority
        //so it can't interfere with other processing going on.
        Thread.currentThread().setPriority(Thread.MIN_PRIORITY);
 
        offImage = createImage(imageW, imageH);
		offGraphics = offImage.getGraphics();

		//This is the animation loop.
        while (Thread.currentThread() == animatorThread)
		{
            //Delay depending on how far we are behind.
            try {
                Thread.sleep(delay);

				//Display it.
				nextImage = (currentImage + 1) % allFrame;
				while( true )
				{
					int i, x, y;
					for( x = imageW/2, y = imageH/2, i = 0; x > (-imageW/10); x -= (imageW/10), y -= (imageH/10), i++)
					{
						offGraphics.drawImage(images[currentImage], 0, 0, this);  //背景图象

						offGraphics.drawImage(images[nextImage], x, y, 2*i*(imageW/10), 2*i*(imageH/10), this);
						repaint();
						Thread.sleep(100);
					}

					currentImage = nextImage;
					repaint();
					break;
				}
            } catch (InterruptedException e) {
                break;
            }
        }
    }

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

    public void update(Graphics g)
	{
		paint(g);
	}
}

⌨️ 快捷键说明

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