Hello World Example

Create an applet that prints Hello World to the screen. To do this:
  1. you must create a Java applet with a class that extends the Applet class. (you must import the java.applet package).
  2. you must have at least the method paint() that is part of this class. In this method is where you will create the code that will print out Hello World in the Web Browser's body.
  3. Note that now you must use the Graphics class instead of the System class as you are now writting things to the body of the Web Browser and not the screen.

Solution

//Purpose: To write a greetings "Hello World" inside of a web-page.
//         

import java.awt.Graphics;  //import the Graphics class
import java.applet.Applet; //import the Applet class



public class HelloWorld extends Applet {

   //Note that g is the local Graphics object representing the 
   //portion of the web-page designated for the applet when it
   //is included in HTML.
   //called when the contents of the component should be painted in
   //response to the component first being shown or damage needing repair.
   public void paint(Graphics g) {
       g.drawString("Hello World!", 25, 25); //start at 25,25 pixel location for the baseline of the text
   }
}