package drawing;
import java.awt.*;
import java.awt.event.*;
class Draw extends Canvas
{
Toolkit tool;
Image img;
Draw()
{
setSize(200,200);
tool=getToolkit();
img=tool.getImage("boy.gif");
}
public void paint(Graphics g)
{ g.drawImage(img,10,10,img.getWidth(this),img.getHeight(this),this);
}
public Image getImage()
{
return img;
}
}
public class ImageCanvas
{
public static void main(String[] args)
{ Draw canvas=new Draw();
Frame frame=new Frame();
frame.add(canvas,BorderLayout.CENTER);
frame.setSize(300,300);
frame.setVisible(true);
frame.setIconImage(canvas.getImage());
frame.validate();
frame.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
}
}
上面的程序是在应用程序中绘制图像,boy.gif放在project的bin文件夹下面。运行程序,始终不能显示绘制的图标,窗口左上角的还是画布上的,都没有显示图像,这个是怎么回事???
------解决方案--------------------
在Draw.java的同一级目录下新建一个文件夹,名称为images,将图片放入到images文件夹下,img=tool.getImage("boy.gif");
这句代码改为img = tool.getImage(Draw.class.getResource("images/boy.gif"));