【发布时间】:2012-03-28 01:58:39
【问题描述】:
现在我正在开发一个程序,该程序会抛出一堆单独的(在运行时生成的)图像,每个图像都在它们自己的窗口中。为此,我尝试了这种方法:
public void display(){
JFrame window = new JFrame("NetPart");
JPanel canvas = new JPanel();
window.getContentPane().add(canvas);
Graphics g = canvas.getGraphics();
Dimension d = getSize();
System.out.println(d);
draw(g,new Point(d.minX*50,d.maxY*50), 50);
window.setSize(d.size(50));
window.setResizable(false);
window.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
window.setVisible(true);
}
public void draw(Graphics g, Point startLoc, int scale){
// generate and draw the image
}
public Dimension getSize(){
//returns my own dimensions class
}
但是,这会在 draw 中引发 NullPointerException,声称图形为 null。有没有办法从外部绘制到 JPanel(不是从 JPanel 继承并覆盖 PaintComponent)?任何帮助,将不胜感激。
【问题讨论】: