我正在尝试将图像加载到我的Java小程序中,我尝试了几种方法,但图像仍然没有出现在小程序中。
这是我的代码:
public class BackgroundApplet extends Applet {
Image backGround;
Image fish;
public void init() {
// set the size of the applet to the size of the background image.
// Resizing the applet may cause distortion of the image.
setSize(300,300);
// Set the image name to the background you want. Assumes the image is in
// the same directory as the class file is
backGround = getImage(getCodeBase(),"underwater.jpg");
BackGroundPanel bgp = new BackGroundPanel();
bgp.setLayout(new FlowLayout());
bgp.setBackGroundImage(backGround);
// set the layout of the applet to Border Layout
setLayout(new BorderLayout());
// now adding the panel, adds to the center(by default in Border Layout) of the applet
add(bgp);
fish = getImage(getCodeBase(), "fish.jpg");
}
public void paint(Graphics g)
{
// Attempt 1
g.drawImage(fish,20,20,this);
// Attempt 2
ImageIcon img = new ImageIcon("fish.jpg");
img.paintIcon(this, g, 20,20);
// Attempt 3
Image image = img.getImage();
g.drawImage(image, 50, 50, this);
}}
正如您所看到的,我还将背景图像加载到我的applet中,并且显示正常。可能是鱼正在背景图像后面加载吗?
感谢任何帮助,谢谢!
**无关注意:当我实现Runnable接口时(因为我希望在背景图像上有动画),当我运行applet时突然不会出现背景图像。对此有任何提示吗?