






Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!







An applet is a Java program that runs in a Web browser.
Applet is a special type of program that is embedded in the webpage to generate the dynamic content. It runs inside the browser and works at client side.
Any applet in Java is a class that extends the java.applet.Applet class.
Advantage of Applet
There are many advantages of applet. They are as follows:
There are 5 lifecycle methods of Applet, Those are
public void init(): is used to initialized the Applet. It is invoked only once.
public void start(): is invoked after the init() method or browser is maximized. It is used to start the Applet.
public void paint(Graphics g): is invoked immediately after the start() method, and this method helps to create Applet’s GUI such as a colored background, drawing and writing.
public void stop(): is used to stop the Applet. It is invoked when Applet is stop or browser is minimized.
public void destroy(): is used to destroy the Applet. It is invoked only once.
Remember:
java.applet.Applet class provides 4 methods ( init,start,stop & destroy ) and java.awt.Graphics class provides 1 method ( paint ) to create Applet.
Simple example of Applet
To execute an Applet, First Create an applet and compile it just like a simple java program.
First.java import java.applet.Applet; import java.awt.Graphics; public class First extends Applet { public void paint(Graphics g){ g.drawString(“Welcome to Applet",50,150); } } Compile: D:> javac First.java D:> After successful compilation, we get First.class file.
java.awt.Graphics class provides many methods for graphics programming.
The Commonly used methods of Graphics class:
Example: GraphicsDemo.java
import java.applet.Applet; import java.awt.*; public class GraphicsDemo extends Applet { public void paint(Graphics g) { g.setColor(Color.red); g.drawString("Welcome",50, 50); g.drawLine(20,30,20,300); g.drawRect(70,100,30,30); g.fillRect(170,100,30,30); g.drawOval(70,200,30,30); g.setColor(Color.pink); g.fillOval(170,200,30,30); } }
Components of Applets
The components of AWT are the components of Applet ,i.e we can use AWT components (Button,TextField,Checkbox, TextArea,Choice & etc.…) in applet.
As we perform event handling in AWT or Swing, we can perform it in applet also.
Let's see the simple example of components and event handling in applet that prints a message by click on the button.
JApplet Class