有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

swing在JAVA中从序列化中读回一个文件后,如何获得一个新的Graphics2D对象?

从磁盘读回保存的对象后,调用Graphics2D对象时,我得到一个Null pointer Exception。这是意料之中的,因为我必须让Graphics2D对象暂时序列化我的对象。加载后,我想得到一个新的有效Graphics2D对象,这样我就可以调用我的绘图过程,而不需要得到空指针。有什么好办法吗?如果不是的话,我有办法避开它,但它很恶心。我宁愿直接做

提前谢谢

班级:

public class GrafPanel extends JPanel implements Serializable 
{
    private transient Graphics2D grafCanvas;
    private GrafSession gFrame;

    /**
     * Constructor for objects of class GrafPanel
     */
    public GrafPanel(GrafSession frame)
    {
       gFrame = frame;

    }

    public void setOwner(GrafSession gs) {gFrame = gs;}

    public void paintComponent(Graphics g){
        grafCanvas = (Graphics2D)g;
        for (GrafObject graf: gFrame.getGrafObjectList()){
            graf.drawGraf();
        }

      }

    public Graphics2D getGrafCanvas(){return grafCanvas;}

}

抛出Null pointer Exception的调用:

public void drawGraf(){
        double xMin = gStuff.getXMin();
        double yMin = gStuff.getYMin();
        double xMax = gStuff.getXMax();
        double yMax = gStuff.getYMax();
        Graphics2D gc =  Graphics2D)getOwner().getGrafPanel().getGrafCanvas();  //null pointer for gc here
...

共 (0) 个答案