绘制图像
Java支持两种主要的图像格式:Gif和Jpeg
1.在小应用程序中绘制图像
首先使用以下方法创建一个Image(该类在java.awt包中)对象:public Image getImage(getCodeBase(),"图像名称")
再在public void paint(Graphics g)方法中调用Graphics类的以下方法输出图像
public boolean drawImage(Image img,int x,int y,int width,int height,ImageObserver observer)
x,y:绘制指定图像的矩形左上角所处的位置
width,height:矩形的宽和高;如果不想在运行时图像有比例上的变化,可以通过Image类的以下方法获取被加载的图像的宽和高
img.getwidth(this)和img.getheight(this)
observer:加载图像时的图像观察器,一般为this
2.在应用程序中绘制图像
首先用Toolkit类的getToolkit()创建一个Toolkit对象
再用Toolkit对象调用public Image getImage("图像名称")方法返回一个Image对象
最后在public void paint(Graphics g)方法中调用Graphics类的以下方法输出图像
public boolean drawImage(Image img,int x,int y,int width,int height,ImageObserver observer)