Sample Applet program in Java

Applets are the small applications programmed in Java and embedded in html code. Usually applets are delivered to user as byte-code.Every page in a website contains applet code and is executed within a JavaVirtualMachine(JVM).We can distinguish a general java program to applets easily,since applet do not have main method in them.

To create an applet we need to import java.appet.* package.This package contains definition of all the keywords of applets.We need to extend the class Applet to our class which contains the applet methods.It is just inheriting Applet class to out class.We need to make this class has public because some other class may need to use our class.

Applets contains five inbuilt methods:

  • init()
  • start()
  • paint()
  • stop()
  • destroy()

These are invoked in the order they are written above.when applet is started inti() method used for initialization of variable is invoked first.Then the methods start() is invoked.The paint method is used for displaying the text on the applet screen.
Today we shall see a sample Applet program.

import java.applet.*;
import java.awt.*;
<applet code="app_demo" height=200 width=200>
public class app_demo extends Applet 
	public void paint(Graphics g)
			g.drawString("This data is displayed on the applet screen",200,200);

Explanation:In the above program after importing the required the package,we have written some code.This code is the html code which is directly written in the our applet program.The applet tag is used to tell the compiler that this program is an applet and it won’t contain a main method.While defining a paint method we declared an object for Graphics class which is used to invoke the drawString() method to display the text.
The drawString() method contains parameters String,int,int.The string is for the message to be displayed and the int ,int are for defining the position of the message.

To compile the applet program from cmd we use the following command:



Download the source code


About Anuroop D

Very enthusiastic about technology and likes to share my knowledge through blogging. Has Bachelor's in Information Technology and currently pursuing my PhD in Computer Science.
This entry was posted in Java and tagged , , , , , , , , , , , , , , , , , , , , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s