你在构造函数中调用panel.getGraphics()是取不到一个graphics实例的,当然返回null.

解决方案 »

  1.   

    嗯,就是楼上说的问题!
    但是为什么取不到呢?用panel.getGraphics()不是其中一种取得绘图对象的方法吗?
    请说详细点,谢谢~
      

  2.   

    大哥,构造函数只是用来初始化实力变量和类对象的,你怎么把方法都加了进去?还有,在main中,你没有定义类对象,new Line();应改为:Line xxx=new Line();
      

  3.   

    getGraphics
    public Graphics getGraphics()Creates a graphics context for this component. This method will return null if this component is currently not displayable. Returns:
    a graphics context for this component, or null if it has none
    Since: 
    JDK1.0 
    See Also:
    paint(java.awt.Graphics)只要将frame.getContentPane().add(panel);这句放在前面就可以通过编译了,但是很少人会像你那样写画图程序.....