How do I initialize a Graphics object in Java?

喜欢而已 提交于 2019-12-21 05:26:09

问题


this is the code:

import java.awt.*;
import java.applet.*;

public class anim1 extends Applet{

    public void paint (Graphics g)
    {
        g.drawString("",400,300);
    }

    public static void main(String ad[])
    {
        anim1 a=new anim1();
        Graphics g1;
        a.paint(g1);
    }
}

It says that g1 is not initialized. But how do I initialize an abstract class?


回答1:


Well there are two issues here 1:

    Graphics g1;
    a.paint(g1);

And you are getting the error that G1 is not initialized. That's because the variable g1 is never set to anything, and that causes a compile error. To get the code to compile, you would need to, at the very least do this:

    Graphics g1 = null;
    a.paint(g1);

However, that obviously won't help you out too much. You'll get a NullPointerException when you try to run your code. In order to actually cause your graphics to draw you need to this:

    anim1 a=new anim1();
    Graphics g1 = anim1.getGraphics();
    a.paint(g1);

However, that still won't work because Anim1 will not appear on the screen. To get it to appear on the screen you need something like:

import java.awt.*;
import javax.swing.*;
import java.applet.*;

public class So1 extends Applet{

    public void paint (Graphics g)
    {
        g.drawString("hello",40,30);
    }

    public static void main(String ad[])
    {

        JFrame jp1 = new JFrame();
        So1 a=new So1 ();
        jp1.getContentPane().add(a, BorderLayout.CENTER);
        jp1.setSize(new Dimension(500,500));
        jp1.setVisible(true);

    }
}

Now notice, we don't actually call the paint() function ourselves. That's handled by the awt, which actually picks the graphics context, and calls our paint function for us. If you want, though, you can pass in any graphics object you want and ask it to draw on to that. (so if you want to draw your component onto an image, you can do it)

(note, I changed the classname from anim1 to So1)




回答2:


An applet doesn't need a main method like a regular Java application does. I'd recommend starting with Sun's Applets Tutorial. In particular you may want to skip ahead to the section Life Cycle of an Applet to see how the Graphics object is handled within the applet.




回答3:


All you need to do is simply remove the main method like so:

import java.awt.*;
import java.applet.*;

public class anim1 extends Applet {

    public void paint (Graphics g) {
        g.drawString("Hello",100,100);
    }
}



回答4:


instead of a call to paint(Graphics g) you need to invoke the repaint or update method. But for this your class must belong to the hierarchy in the java.awt.Container.

For your class you have overriden the Paint method and in the main you are trying to invoke the paint method. Instead of paint you need to invoke the repaint or update method (if your class is in the hierarchy of java.awt.Container) and the event dispatching system of java invokes your overridden paint method itself.




回答5:


You should manipulate the graphics of a component within the paint method and invoke repaint() or update(), but not the paint method directly.

Start here for some more information.




回答6:


You dont initialize a Graphics object.

You get the graphics object from a component via Component#getGraphics() method.

In your particular case I think repaint() is all you need!!




回答7:


You don't, you use getGraphics, but, if you really want to initialize it, then type new Graphics(){}; And have fun filling all the abstract methods. Most of the times just putting code in paint(g) should do, you need to remember to set your applet visible, and usually, it should be the last line in your constructor or even outside it, I have made a mistake once where I made visible and then initialized a bunch of variables, it showed a black screen for a while.



来源:https://stackoverflow.com/questions/1965347/how-do-i-initialize-a-graphics-object-in-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!