Applet in Java
- Applets are small Java applications that can be accessed on an Internet server, transported over Internet, and can be automatically installed and run as apart of a web document. Any applet in Java is a class that extends the java.applet.Applet class.
- An Applet class does not have any main() method.
- It is viewed using JVM. The JVM can use either a plug-in of the Web browser or a separate runtime environment to run an applet application.
- JVM creates an instance of the applet class and invokes init() method to initialize an Applet.
A Simple Applet
import java.awt.*;
import java.applet.*;
public class Simple extends Applet
{
public void paint(Graphics g)
{
g.drawString("A simple Applet", 20, 20);
}
}
Every Applet application must declare a paint()
method. This method is defined by AWT class and must be overridden by the applet. paint()
method is called each time an applet neede to redisplay its output. Another important thing to notice about applet application is that, execution of an applet does not begin at main()
method. In fact an applet application does not have any main() method.
import java.applet.*;
public class Simple extends Applet
{
public void paint(Graphics g)
{
g.drawString("A simple Applet", 20, 20);
}
}
paint()
method. This method is defined by AWT class and must be overridden by the applet. paint()
method is called each time an applet neede to redisplay its output. Another important thing to notice about applet application is that, execution of an applet does not begin at main()
method. In fact an applet application does not have any main() method.Advantages of Applets
- Very less response time as it works on the client side.
- Can be run using any browser, which has JVM running in it.
Applet class
Applet class provides all necessary support for applet execution, such as initializing and destroying of applet. It also provide methods that load and display images and methods that load and play audio clips.
An Applet Skeleton
Most applets override these four methods. These four methods forms Applet lifecycle.- init() : init() is the first method to be called. This is where variable are initialized. This method is called only once during the runtime of applet.
- start() : start() method is called after init(). This method is called to restart an applet after it has been stopped.
- stop() : stop() method is called to suspend thread that does not need to run when applet is not visible.
- destroy() : destroy() method is called when your applet needs to be removed completely from memory.
Example of an Applet Skeleton
import java.awt.*;
import java.applet.*;
public class AppletTest extends Applet
{
public void init()
{
//initialization
}
public void start ()
{
//start or resume execution
}
public void stop()
{
//suspend execution
{
public void destroy()
{
//perform shutdown activity
}
public void paint (Graphics g)
{
//display the content of window
}
}
import java.applet.*;
public class AppletTest extends Applet
{
public void init()
{
//initialization
}
public void start ()
{
//start or resume execution
}
public void stop()
{
//suspend execution
{
public void destroy()
{
//perform shutdown activity
}
public void paint (Graphics g)
{
//display the content of window
}
}
Example of an Applet
import java.applet.*;
import java.awt.*;
public class MyApplet extends Applet
{
int height, width;
public void init()
{
height = getSize().height;
width = getSize().width;
setName("MyApplet");
}
public void paint(Graphics g)
{
g.drawRoundRect(10, 30, 120, 120, 2, 3);
}
}
import java.awt.*;
public class MyApplet extends Applet
{
int height, width;
public void init()
{
height = getSize().height;
width = getSize().width;
setName("MyApplet");
}
public void paint(Graphics g)
{
g.drawRoundRect(10, 30, 120, 120, 2, 3);
}
}