java小程序映像未加载
我正在尝试将一个图像加载到我的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);
}}
如您所见,我还将一个背景图像加载到我的小程序中,显示得很好。可能是鱼在背景图像后面装载
感谢您的帮助,谢谢
**不相关注意:当我实现Runnable界面时(因为我想在背景图像上有动画),当我运行小程序时,背景图像突然不出现。还有什么建议吗
共 (0) 个答案