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)