Open all | Close all


Simple Java Applet

Below you will find the Java code for creating a simple applet. Next step is to add and applet to your
webpage. To do this simple copy the .class file into your directory and insert the following code:
			<APPLET CODE=TestApplet.class WIDTH=200 HEIGHT=100>
			</APPLET> 

The applet will then be displayed as follows:
	
	

The Java code for the above applet:
import java.applet.Applet;
import java.awt.Graphics;

public class TestApplet extends Applet {

    StringBuffer buffer;

    public void init() {
	buffer = new StringBuffer();
	 System.out.println("HelloWorld (Console)");
	 buffer.append("HelloWorld (Applet)");
     repaint();
    }

    public void paint(Graphics g) {
	//Draw a Rectangle around the applet's display area.
        g.drawRect(0, 0, size().width - 1, size().height - 1);

	//Draw the current string inside the rectangle.
        g.drawString(buffer.toString(), 5, 15);
    }
}