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

📄 activeimages.java

📁 JAVA源程序-动画设计的例子 可用记事本打开
💻 JAVA
字号:
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
public class ActiveImages extends Applet implements Runnable,ActionListener
{
    Image iImages[];                     //图像数组
    Thread aThread;
    int iFrame;                          //图像数组下标
    int sleeptime;                       //参数,线程sleep时间
    String graphfile;                    //图片文件名
    int graphcount;                      //图片张数
    AudioClip au;                        //定义一个声音对象
    Button b1,b2;
    public void init() 
    {
        int i,j;
        iFrame=0;
        aThread=null;
        sleeptime=Integer.parseInt(getParameter("sleeptime"));
        graphfile = getParameter("graphfile");
        graphcount = Integer.parseInt(getParameter("graphcount"));
        iImages = new Image[graphcount];
        String fname = graphfile;
        j = fname.indexOf(".");
        for (i=0;i<graphcount;i++)
        {
            fname = fname.substring(0,j-1)+i+fname.substring(j);
            iImages[i] = getImage(getDocumentBase(),"Images/"+fname);
        }
        au=getAudioClip(getDocumentBase(),"Wav/Sound.wav");
        au.play();                       //播放一次声音文件
        Panel p1 = new Panel();
        b1 = new Button("Start");
        b2 = new Button("Stop");
        p1.add(b1);
        p1.add(b2);
        b1.addActionListener(this);
        b2.addActionListener(this);
        setLayout(new BorderLayout());
        add(p1,"South");
    }
    public void start() 
    {
        if (aThread == null) 
        {
            aThread = new Thread(this);
            aThread.start();             //线程启动
            b1.setEnabled(false);
        }
    }
    public void stop() 
    {
        if (aThread != null) 
        {
            aThread.interrupt();         //线程中断
            aThread = null;
            au.stop();                   //停止播放声音文件
        }
    }
    public void run() 
    {
        while (true) 
        {
            iFrame++;
            iFrame %= (iImages.length);  //下一幅图像的下标
            repaint();
            try 
            {
                Thread.sleep(sleeptime);
            }
            catch (InterruptedException e) 
            {                            //中断时抛出
                break;                   //退出循环
            }
        }
    }
    public void update(Graphics g)
    {
        g.drawImage(iImages[iFrame],0,0,this);
    }
    public void actionPerformed(ActionEvent e)
    {
        if ((e.getSource()==b1) && (aThread == null) )
        {                                //单击Start按钮时触发
            aThread = new Thread(this);
            aThread.start();             //线程启动
            b1.setEnabled(false);
            b2.setEnabled(true);
            au.loop();                   //循环播放声音文件
        }
        if ((e.getSource()==b2) && (aThread != null) )
        {                                //单击Stop按钮时触发
            aThread.interrupt();         //线程中断
            aThread = null;
            b1.setEnabled(true);
            b2.setEnabled(false);
            au.stop();                   //停止播放声音文件
        }
    }
}

⌨️ 快捷键说明

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