How to initialize a Graphics object in Java?

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 to initialize an abstract class?

+4
source share
7 answers

There are two problems here:

  Graphics g1; a.paint(g1); 

And you get the error that G1 is not initialized. This is because the g1 variable is never set to anything, and this causes a compilation error. To get the code to compile, you need to at least do this:

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

However, this clearly will not help you too much. When you try to run the code, you will get a NullPointerException. To actually make your graphics draw, you need to:

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

However, this still will not work, because Anim1 will not appear on the screen. To get it 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 that we do not actually call the paint () function. This is handled by awt, which actually selects the graphics context and calls our drawing function for us. However, if you want, you can transfer any desired graphic object and ask him to do it. (therefore, if you want to draw your component on the image, you can do it)

(note, I changed the class name from anim1 to So1)

+13
source

An applet does not need the main method, as a regular Java application does. I recommend starting with the Sun Applets Tutorial . In particular, you can go to the "Applet Life Cycle" section to see how the Graphics object in the applet is handled.

+1
source

All you have to do is simply remove the main method as follows:

 import java.awt.*; import java.applet.*; public class anim1 extends Applet { public void paint (Graphics g) { g.drawString("Hello",100,100); } } 
+1
source

You must manipulate the component graphics in the paint method and call repaint () or update (), but not directly using the drawing method.

Start here for more information.

0
source

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

You have overridden the Paint method for your class, and basically you are trying to call the paint method. Instead of paint, you need to call the redraw or update method (if your class is in the java.awt.Container hierarchy), and the java event dispatch system calls your overridden paint method.

0
source

You are not initializing the Graphics object.

You get a graphic from a component using the Component#getGraphics() method.

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

0
source

You don’t, you use getGraphics, but if you really want to initialize it, enter a new Graphics () {}; And have fun filling in all the abstract methods. In most cases, when you just need to enter the code in the paint (g), you need to remember that your applet should be visible, and usually it should be the last line in your constructor or even outside it, I made a mistake when I made visible. and then initialized by a bunch of variables, for a while it showed a black screen.

0
source

All Articles