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

📄 movingimage.java

📁 java applet编程,实现对相关图片声音的调用
💻 JAVA
字号:
import java.awt.*;import java.applet.*;/*  * This applet moves one image in front of a background image. * It eliminates flashing by double buffering. */public class MovingImage extends Applet implements Runnable{    int frameNumber = -1;    int delay;    Thread animatorThread;    Dimension offDimension;    Image offImage;    Graphics offGraphics;    Image stars;    Image rocket;    public void init()	{        String str;        int fps = 10;        //How many milliseconds between frames?        str = getParameter("fps");        try		{            if (str != null)			{                fps = Integer.parseInt(str);            }        } catch (Exception e) {}        delay = (fps > 0) ? (1000 / fps) : 100;        //Get the images.        stars = getImage(getCodeBase(), "starfield.gif");        rocket = getImage(getCodeBase(), "rocketship.gif");    }    public void start()	{        //Start animating!        if (animatorThread == null)		{            animatorThread = new Thread(this);        }        animatorThread.start();    }    public void stop()	{        //Stop the animating thread.        animatorThread = null;        //Get rid of the objects necessary for double buffering.        offGraphics = null;        offImage = 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);         //Remember the starting time.        long startTime = System.currentTimeMillis();        //This is the animation loop.        while (Thread.currentThread() == animatorThread)		{            //Advance the animation frame.            frameNumber++;            //Display it.            repaint();            //Delay depending on how far we are behind.            try			{                startTime += delay;                Thread.sleep(Math.max(0,                                       startTime-System.currentTimeMillis()));            } catch (InterruptedException e) {                break;            }        }    }    public void update(Graphics g)	{		paint(g);	}    public void paint(Graphics g)	{        Dimension d = size();        //Create the offscreen graphics context, if no good one exists.        if ( (offGraphics == null)          || (d.width != offDimension.width)          || (d.height != offDimension.height) )		  {            offDimension = d;            offImage = createImage(d.width, d.height);            offGraphics = offImage.getGraphics();        }        //Erase the previous image.        offGraphics.setColor(getBackground());        offGraphics.fillRect(0, 0, d.width, d.height);        offGraphics.setColor(Color.black);        //Paint the frame into the image.        paintFrame(offGraphics);        //Paint the image onto the screen.        g.drawImage(offImage, 0, 0, this);    }    void paintFrame(Graphics g)	{        Dimension d = size();        int w;        int h;        //如果背景图象已经完全装入,就将它画出来        w = stars.getWidth(this);        h = stars.getHeight(this);        if ((w > 0) && (h > 0))		{            g.drawImage(stars, (d.width - w)/2,                        (d.height - h)/2, this);        }        //如果前景图象已经完全装入,就将它画出来        w = rocket.getWidth(this);        h = rocket.getHeight(this);        if ((w > 0) && (h > 0))		{            g.drawImage(rocket, ((frameNumber*5) % (w + d.width)) - w,                        (d.height - h)/2, this);   //图象的水平显示位置范围为(-w, d.width)        }    }}

⌨️ 快捷键说明

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