本帖最后由 杨宗彬 于 2012-8-3 16:39 编辑
package com.yzb.GUI;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class DrawImage extends Frame {
/**
* @param args
*/
public DrawImage(String n){
super(n);
addWindowListener(new WindowAdapter(){
@Override
public void windowClosing(WindowEvent e) {
// TODO Auto-generated method stub
super.windowClosing(e);
dispose();
System.exit(0);
}
});
}
public static void main(String[] args) {
// TODO Auto-generated method stub
DrawImage di = new DrawImage("DrawImage");
Image img = di.getToolkit().getImage("I:\\图片\\7_5006e8dbb35c9.jpg");
Graphics g = di.getGraphics();
di.setSize(500, 500);
di.setVisible(true);
while(!g.drawImage(img, 0, 0, di));
}
}
如果把上面那两行代码写成:di.getGraphics().drawImage(img,0,0,di);就能实现 为什么?
程序报空指针的异常。貌似是没拿到画笔,怎么回事? |