2014年5月26日星期一

Urgent help ! ! ! Questions about the Graphics class returns a null pointer

 This post last edited by u014246256 on 2014-05-26 12:18:28
//这些代码就是想在frame 窗口中画一条直线。

一.  返回空指针的错误代码。

public class Demo
{
    public static void main(String[] args)
    {
        Frame   f   = new Frame();
        f.setBounds(300,200,600,400);
       
        f.getGraphics().drawLine(1,1,100,100);   //这句话在编译时通过,但运行时却返回空指针异常
  //好像是getGraphics()方法没有获得Graphics对象。
        f.setVisible(true);
}


//但是这些代码却能正确执行。

二.   正确代码。

public class Demo
{
    public static void main(String[] args)
    {
        Frame   f   = new Frame();
        f.setBounds(300,200,600,400);
 
 //先创建一个监听器,并注册到f中,
//再通在监听器中通过getSource()得/到 f,然后再调用getGraphics()方法就行了。
//这不相当于绕了一个弯路吗?       
       f.addMouseListener(new MouseAdapter(){           
             public void mousePressed(MouseEvent   e)
             {
                     e.getSource().getGraphics().drawLine(1,1,100,100);
              }
         });
                                                                                         
        f.setVisible(true);
}


//怎样直接就能在f中画图呢???


there is another problem :

I customize a class called t let him inherit the Frame class , so I cover the paint () method, this method is not dead in the object the establishment of automatic call it? T go in this drawing class , simply do not consider the problems of the Graphics object . ( Because the paint () automatically, so there is no need for me to call paint (), of course, will not I create a Graphics object to pass actual parameters to the paint.) See next code :
class t extends Frame
{
    public void paint(Graphics g)
    {
        g.drawLine(1,1,70,80); 
    }
}
public class Demo
{
    public static void main(String[] args)
    {
        t   f   =   new t ();
        f.setBounds(300,200,600,400);
       f.setVisible(true);       
     }
}


However, this is not a passive way of drawing it? Because it is the paint () method is called automatically when you create an object .

To sum up : the Graphics class is about the problems of the object.
------ Solution ---------------------------------------- ----
using a method is necessary to understand the characteristics of a method , you encounter a mistake not to mention a closer look at that method
Here is the Javadoc for this method of presentation
Creates a graphics context for this component. This method will return null if this component is currently not displayable.
written very clearly, when the component is not currently displayed , it will return null So you have to setVisible

there is a problem , if you just drawLine painting once , then you can not see the effect , because the form is constantly redrawn , which is constantly emptied the contents , if you want to see results , you to continue drawLine, like this , but this approach is not good , the correct approach should be rewritten paint .
		Frame f = new Frame();
f.setSize(400, 300);
f.setVisible(true);

Graphics g = f.getGraphics();

while (true) {
g.drawLine(1, 1, 100, 100);
}

------ For reference only ----------------------------------- ----
Aoao , I tried, and you said the same.
But why rewrite the paint () method can solve the problem of the form redraw it . Is not to rewrite the paint method to redraw it?
redraw is how the matter .
I am a novice , please bear with me.
------ For reference I had read only ---------------------------------------
API documentation but did not understand , thank you master pointing .

public Graphics getGraphics ()
create a graphics context for the component . If the component is currently not displayed , this method returns null.

Returns:
components graphics context , if it does not , it returns null
start from the following versions :
JDK1.0
See also :
paint (java.awt.Graphics)

------ For reference only ---------------------------------- ----- mechanism
Swing is a Component of the appearance of the paint method is drawn , then he will be repeated ( for example, 60 times a second ) call the paint to ensure timely updated UI , which also provides animation support , animation has always been more than an illusion screen showing a short form
redraw refers repaint, whether you re not overridden paint method will redraw .
------ For reference only -------------------------------------- -
figuratively speaking, is the Graphics is a drawing board , paint is a painter , and repaint the leader , leader told the artist , every 1 /60 second , first wipe the slate original and re- painting again, this is heavy painted
rewrite paint method is equivalent to , you tell a painter, not to draw you want to draw something original , I'll tell you what to draw , how to paint .
And you did getGraphics first method is equivalent to , you first take it over the drawing board , above the artist 's paintings , and then modify your own , so make sure you modify the user can see , you can only painter same speed or faster than him , or you painted , 1 / 60 seconds after being erased painter , but still a bad practice
------ For reference only - --------------------------------------
you handsome ! I understand
, thank you !

没有评论:

发表评论