Image nodeimage = createImage(d.width,d.height);
只能放在paint(Graphics g)或update(Graphics g)中才有效
你不如使用BufferedImage,就没有这个限制了        

解决方案 »

  1.   

    问题出在thisapplet.getDocumentBase(),
    是不是不以myapplet作文件名(就是说myapplet应该是个public class),就无法得到它的url呢?
      

  2.   

    你需要确定的是drawnode()是在什么阶段执行的。
    如果在 Panel被显示之前执行的话,
    由于没有显示,所以当前对象的graphics是没有的
    必须在setVisable或者show之后,
    再用getGraphics()
    那个时候就不是null啦!
    给出一段测试code,你运行一下就知道为什么啦:
    import java.awt.*;
    import java.awt.event.*;public class test1 extends Frame{    public test1() {
            Button h = new Button("ddd");
            h.addActionListener(new a());
            add(h);
        }    public static void main(String[] a){
            test1 f = new test1();
            System.out.println(f.getGraphics() + "------1");   // output: null----1
            f.show();
            System.out.println(f.getGraphics() + "-------2"); // output : sun.awt.windows.WGraphics-----------2    }    class a implements ActionListener{
            public void actionPerformed(ActionEvent e){
                System.exit(0);
            }
        }
    }
    我是刚刚进入csdn的
    记得给我加分哟
      

  3.   

    to masterz:你说的BufferedImage是那个类的方法?怎么用?能说得清楚一些吗?谢谢to短笛:getDocumentBase()是Applet得方法,我在继承的Panel类中使用它,当然需要指定对象是myapplet阿to whduyang:我的mypanle extend Panel是在myapplet中实例化的,然后drawnode是mypanel的方法,现在我在drawnode中加入你建议的this.show()或者this.setVisible(true);但是getGraphics后g仍然是null阿!!
      

  4.   

    getDocumentBase()要求在applet的init()之后执行
      

  5.   

    我说的意思和你理解的不一样了。
    看见我的例子啦,那么就应该知道了。
    必须是存在graphics才能get它。
    show和setVisable都是方法之一,paint  update也一样。
    我不知道你是怎样实现的,
    但是有一点必须强调:getGraphics必须在graphics存在的情况下使用
      

  6.   

    to whduyang: 在扩展的panel类的方法中怎样生成Graphics的实例阿??我的实现就是这样的啊:
    class mypanel extends Panel{
        myapplet thisapplet;    mypanel(myapplet Myapplet){
         this.thisapplet = Myapplet;
        }
    //由myapplet实例化mypanel后调用drawnode方法:      
        void drawnode(String nodelbl,boolean center){
            Dimension d = getSize();
            Graphics g;
            Image nodeimage;
    nodeimage = createImage(d.width,d.height);
    this.setVisible(true);  
    this.show();
            g = this.getGraphics();

            nodeimage = thisapplet.getImage(thisapplet.getDocumentBase(),"computer.gif");  
      
    ////        Node n = new Node();
    ////        double angle = 0.2;
      System.out.println("now test g is null or not"); 
      if (g==null){       
             System.out.println("now g is null!!!hahahahahahah"); 
    //         g.drawImage(nodeimage,0,0,this);
    }
      

  7.   

    to whduyang: 在扩展的panel类的方法中怎样生成Graphics的实例阿??我的实现就是这样的啊:
    class mypanel extends Panel{
        myapplet thisapplet;    mypanel(myapplet Myapplet){
         this.thisapplet = Myapplet;
        }
    //由myapplet实例化mypanel后调用drawnode方法:      
        void drawnode(String nodelbl,boolean center){
            Dimension d = getSize();
            Graphics g;
            Image nodeimage;        nodeimage = createImage(d.width,d.height);
            this.setVisible(true);  
    //        this.show();
            g = this.getGraphics();

            nodeimage = thisapplet.getImage(thisapplet.getDocumentBase(),"computer.gif");  
      
    System.out.println("now test if g is null"); 
      if (g==null){       
             System.out.println("now g is null!!!"); 
    }
    //        g.drawImage(nodeimage,0,0,this);
    另外你的test1例子我用javac,appletviewer的结果有错阿:
    java.lang.ClassCastException: test1
            at sun.applet.AppletPanel.createApplet(AppletPanel.java:542)
            at sun.applet.AppletPanel.runLoader(AppletPanel.java:478)
            at sun.applet.AppletPanel.run(AppletPanel.java:293)
            at java.lang.Thread.run(Thread.java:579)
    55555555555555,我现在知道getGraphics是需要g存在的前提下使用,但是它死活就是null~~~~
    ;(((
      

  8.   

    我给你的是application
    你用javac    ,java运行
    你在drawnode中用show是什么意思?我不懂。
    我的意思是:实例化一个句柄和存在一个graphics对象没有必然的联系。
    你要取得this的graphics,前提是this已经拥有了一个graphics.
    对你的applet来说,就是对drawnode的调用必须在paint()以后。
      

  9.   

    http://www.csdn.net/expert/topic/104/104092.shtm
      

  10.   

    to whduyang:
    555,虽然上个月做了一些jsp,servlet,以及现在开始写applet,发现自己的java还是没有入门的说;(  我还没有写过application,所以不是很明白你的例子,但总不是编译再运行吗?
    你给我一个applet的简单例子吧!!!
    drawnode中的this.show()是让当前mypanel.show()阿,
    然后g = mypanel.getGraphics,这样g是否就应该不再是null了呢?另外我不太明白对于Applet扩展类来说,paint()方法是必须写的吗?我的myapplet中没有paint(),因为我的画图试图放到mypanel中实现的,现在我是在myapplet的init()中调用(或者放到start()也行吧)drawnode()。
    可以的话,能否qq?516493333 ;)
    我加分!!
      

  11.   

    现在是mypanel是visible的,但是接着mypanel.getGraphics()得到的graphics对象却是null,为什么那?;((((
      

  12.   

    没有人能指点一下吗?我真的有点没办法了呢!!//bow