现在我的applet(myapplet)使用 MyPanel类的paintnode(String nodelbl,boolean center)方法完成applet作图区结点的绘制功能(节点处引入一个gif,显示label鼠标响应等)
mypanel类涉及节点处绘图的程序段如下:
 
class mypanel extends Panel{
    myapplet thisapplet;    mypanel(myapplet Myapplet){
        thisapplet = Myapplet;
    }    void drawnode(String nodelbl,boolean center){
        Dimension d = getSize();        
        Image nodeimage = createImage(d.width,d.height);
        Graphics g = this.getGraphics(); 
        nodeimage = 
                thisapplet.getImage(thisapplet.getDocumentBase(),"computer.gif");
        g.drawImage(nodeimage,0,0,this);   //跟踪发现这里的g = null
   }
 因为Graphics是抽象类,不能直接new Graphics()生成其实例,可是applet中引入图片的
 最简单方法就是先 Applet.getImage(URL...)然后 g.drawImage吧, 可是为什么appletviewer运行的时候说空指针异常呢?
 g = null,那就是说this.getGraphics()没有返回了,不会啊?应该怎么办呢??谢谢

解决方案 »

  1.   

    现在我的applet(myapplet)使用 MyPanel类的paintnode(String nodelbl,boolean center)方法完成applet作图区结点的绘制功能(节点处引入一个gif,显示label鼠标响应等)
    mypanel类涉及节点处绘图的程序段如下:
     
    class mypanel extends Panel{
        myapplet thisapplet;    mypanel(myapplet Myapplet){
            thisapplet = Myapplet;
        }    void drawnode(String nodelbl,boolean center){
            Dimension d = getSize();        
            Image nodeimage = createImage(d.width,d.height);
            Graphics g = this.getGraphics(); 
            nodeimage = 
                    thisapplet.getImage(thisapplet.getDocumentBase(),"computer.gif");
            g.drawImage(nodeimage,0,0,this);   //跟踪发现这里的g = null
       }
     因为Graphics是抽象类,不能直接new Graphics()生成其实例,可是applet中引入图片的
     最简单方法就是先 Applet.getImage(URL...)然后 g.drawImage吧, 可是为什么appletviewer运行的时候说空指针异常呢?
     g = null,那就是说this.getGraphics()没有返回了,不会啊?应该怎么办呢??谢谢