0

I want to draw a string using a init method but if use inside start method then its work fine. Please explain me

    public class Canv extends Applet //applet class 
    {   
        public void start()
        {

        }
         public void init()
        {                                     
            System.out.println("hi");
            Canvas c=new Canvas();              // want to print String in canvas

            c.setSize(500,500);
            c.setBackground(Color.red);
            add(c);
            Graphics g=c.getGraphics();  
            g.drawString("hello buddy",60,60); 
        }
        public void paint(Graphics g)
        {

        }
        public void stop()
        {
            System.out.println("stop");
        }
    }
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Sajid Dayer
  • 11
  • 1
  • 4
  • Can you paste the code that works? – darijan Aug 03 '15 at 14:18
  • just copy paste init code into the start method then its work – Sajid Dayer Aug 03 '15 at 15:36
  • 1) Why code an applet? If it is due to the teacher specifying it, please refer them to [Why CS teachers should **stop** teaching Java applets](http://programmers.blogoverflow.com/2013/05/why-cs-teachers-should-stop-teaching-java-applets/). 2) Why use AWT? See [this answer](http://stackoverflow.com/questions/6255106/java-gui-listeners-without-awt/6255978#6255978) for many good reasons to abandon AWT using components in favor of Swing. – Andrew Thompson Aug 05 '15 at 11:17

1 Answers1

1

If you carefully read the API provided by Java, then you would see for init:

 * Called by the browser or applet viewer to inform
 * this applet that it has been loaded into the system. It is always
 * called before the first time that the <code>start</code> method is
 * called.
 * <p>
 * A subclass of <code>Applet</code> should override this method if
 * it has initialization to perform. For example, an applet with
 * threads would use the <code>init</code> method to create the
 * threads and the <code>destroy</code> method to kill them.

And for start:

 * Called by the browser or applet viewer to inform
 * this applet that it should start its execution. It is called after
 * the <code>init</code> method and each time the applet is revisited
 * in a Web page.

Obviously, the code that uses this interface to manipulate Java applets does not show the canvas written in the init() method because the UI at that point is not ready. It's just a marker for you that the container started initializing your app.

darijan
  • 9,725
  • 25
  • 38