Writing and executing your first applet

 

Write your first applet type of application in Notepad and save it in the file that has the same name as you named your java class. File must have .java extension.

 

import java.awt.*;

import java.applet.Applet;

 

public class MyApplet extends Applet

{

   public void paint(Graphics g)

   {   

       g.drawString("Hello class",150,150);

    }

}

My class is called MyApplet and it should be saved as MyApplet.java

 

 

Write HTML file also in notepad and save it with HTML extension.

<HTML>

<APPLET  CODE=MyApplet.class  WIDTH=400 HEIGHT=300>

</APPLET>

</HTML>

 

You may save it as MyApplet.html. This time it does not have to have the same name as before, but it must have .html extension.

 

Compile file MyApplet.java using command:

>javac MyApplet.java

 

Use either appletviewer or browser to run applet. Appletviewer provides nicer window, but both ways you can do the same job.

  >Appletviewer MyApplet.html

      This should bring applet window with words "Hello class" drawn in the middle.