大致改了一下//Main_Ero.java 文件import javax.swing.*;
import java.awt.*;
import java.awt.event.*;public class Main_Ero extends JFrame{   private Ero_Panel eropanel;
   public Main_Ero()
   {
      Container cn = getContentPane();
      cn.setLayout( null );
      cn.setBackground( Color.blue );      eropanel = new Ero_Panel();
      eropanel.setSize( 300,600 );
      eropanel.setLocation( 30,30 );
      cn.add( eropanel);
      setSize( 600,700 );
      setLocation( 199,10 );
   }
   public void drawSth(){
     eropanel.tt();
   }   public static void  main( String[] arge )
   {
      Main_Ero ERO = new Main_Ero();
      ERO.show();
      ERO.drawSth();
   }
}
//Ero_Panel.java 文件import javax.swing.*;
import java.awt.*;
import java.awt.event.*;public class Ero_Panel extends JPanel{   Graphics g;
   aa aaa;   public Ero_Panel()
   {
      this.setBackground( Color.pink );
      aaa = new aa( this );
 //     tt();
   }   public void tt()
  { 
      aaa.draw_image();
   }
}class aa
{
    JPanel pp;
 //   Graphics gg;
    public aa( JPanel a )
    {
       pp = a;
//       gg = g;
    }    public void draw_image()
    {
        Image mg = pp.createImage( 600,600 );
        Graphics gt = mg.getGraphics();
        gt.fillRect(0,0,600,600);
        pp.getGraphics().drawImage( mg,0,0,pp  );    }
}
主要问题是你在Frame显示出来之前
不能通过getGraphics来调用里面的任何一个组件的Graphics

解决方案 »

  1.   


    BTW
      问题要是搞定了记得揭帖哦
      

  2.   

    主要问题是你在Frame显示出来之前
    不能通过getGraphics来调用里面的任何一个组件的Graphics这是为什么  
    怎么教程里都没说过啊这个怎么可以呢
    呵呵import java.awt.*;
    import java.awt.event.*;
    import java.applet.*;
    import javax.swing.*;
    import java.awt.*;
    import java.applet.*;public class showli extends JFrame {    private carton carton_1 = null;
        public showli()
        {
            Container cn = getContentPane();
            cn.setBackground( new Color( 1,1,199 ) );    }
        public void start()
        {        if ( carton_1 == null )
            {
                carton_1 = new carton( getGraphics(),this );
                carton_1.run();
            }
        }   public void stop()
        {
           carton_1.stop();
           carton_1=null;
        }    public static void main( String[] arge )
        {
            showli aa = new showli();
            aa.setSize( 600,600 );
            aa.show();
            aa.start();
        }
    }
    class carton extends Thread
    {    JFrame  mm;
        Graphics g;    public carton(Graphics a,JFrame app)
        {
            g = a;
            mm = app;
        }    public void run()
        {
            draw_image();    }    public void draw_image()
        {
            Image mg = mm.createImage( 600,600 );
            Graphics gt = mg.getGraphics();
            gt.fillRect(0,0,90,90 );
            g.drawImage( mg,0,0,mm  );
        }}
      

  3.   

    getGraphicspublic Graphics getGraphics()
    Creates a graphics context for this component. This method will return null if this component is currently not displayable.
    看API里面说的
    在你show出来之前,这个getGraphics()是一个NULL
      

  4.   

    aa.show();
    aa.start();在你show了之后,你才调用了start()
    初始化了你的线程
    if ( carton_1 == null )
            {
                carton_1 = new carton( getGraphics(),this );
                carton_1.run();
            }
    这样的话,既然你已经把Frame画出来了,那么你对
    getGraphics()的引用就不是null了,而是真正的Frame
    的Graphics()了