MyException - 我的异常网
当前位置:我的异常网» J2SE » applet程序不能一般显示动画

applet程序不能一般显示动画

www.MyException.Cn  网友分享于:2013-11-29  浏览:3726次
applet程序不能正常显示动画
我正在学java,这是教材里面的代码,一模一样输入
Java code

//<applet code = "Ani.class" height = "200" width = "200">
//</applet>
import java.awt.*;
import javax.swing.*;

class ImagePanel extends JPanel
{
        Image[] img;
        int currentImage = 0;
        public ImagePanel(Image[] img)
        {
                this.img = img;
        }
        public void paintComponent(Graphics g)
        {
                super.paintComponent(g);
                g.drawImage(img[currentImage], 0, 25, this);
                currentImage = (currentImage + 1) % 10;
        }
        public void run()
        {
                while (true)
                {
                        repaint();
                        try
                        {
                                Thread.sleep(200);
                        } catch (InterruptedException e) { }
                }
        }
}

public class Ani extends JApplet
{
        ImagePanel panel = null;
        Image[] image = new Image[10];

        Thread t = null;
        public void init()
        {
                for (int i = 0; i < 10; i++)
                {
                        image[i] = getImage(getDocumentBase(), "T"+(i+1)+".gif");
                }
                panel = new ImagePanel(image);
                panel.setBackground(Color.GREEN);
                getContentPane().add(panel);
        }
        public void start()
        {
                t = new Thread(panel);
                t.start();
        }
}




然后用appletviewer打开,动画什么都没有

------解决方案--------------------

你看的教科书可能过时了,从这个站点吸取点营养:

http://download.oracle.com/javase/tutorial/uiswing/components/applet.html
------解决方案--------------------
import java.awt.*;
import javax.swing.*;

class ImagePanel extends JPanel
{
Image[] img;
int currentImage = 0;
public ImagePanel(Image[] img)
{
this.img = img;
}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
g.drawImage(img[currentImage], 0, 25, this);
currentImage = (currentImage + 1) % 10;
}
public void run()
{
while (true)
{
repaint();
try
{
Thread.sleep(200);
} catch (InterruptedException e) { }
}
}
}

public class Ani extends JApplet
{
ImagePanel panel = null;
Image[] image = new Image[10];

Thread t = null;
public void init()
{
for (int i = 0; i < 10; i++)
{
image[i] = getImage(getDocumentBase(), "T"+(i+1)+".gif");
}
panel = new ImagePanel(image);
panel.setBackground(Color.GREEN);
getContentPane().add(panel);
}
public void start()
{
t = new Thread();
t.start();
}
}
这样就可以了,同时,你要把图片放在工程的bin目录下
------解决方案--------------------
不要看qpplet了,这已经过时了,已经没有用了。学这个白费劲啊。

文章评论

软件开发程序错误异常ExceptionCopyright © 2009-2015 MyException 版权所有