Unit 6.1
Unit 6.1
Applets don’t use the main() method, but when they are loaded,
automatically call certain methods (init, start, paint, stop, destroy).
They are embedded inside a web page and executed in browsers.
Takes input through Graphical User Input ( GUI ).
They cannot read from or write to the files on local computer.
They cannot run any programs from the local computer.
They are restricted from using libraries from other languages.
init()
Begin Born
stop()
start()
Running Idle
destroy()
paint() start()
Dead End
Life cycle of an Applet
It is important to understand the order in which these methods are called.
At this point, you should free up any resources the applet may be using. The stop( )
method is always called before destroy( ).
Types of applets
After you create this file, open your browser and then load this file,
which causes SimpleApplet to be executed.
width and height specify the dimensions of the display used by the
applet.
Executing by using appletviewer
There are two ways
1. Use earlier html page, which contains applet tag, then execute by
using following command.
C:\>appletviewer SimpleApplet.html
2. Include a comment at the beginning of your source code file that
contains the applet tag, then start applet viewer with your java source
code file. C:\>appletviewer SimpleApplet.java
import java.awt.*;
import java.applet.Applet;
/* <applet code=“SimpleApplet” width=200 height=60 ></applet> */
public class SimpleApplet extends Applet {
public void paint(Graphics g) {
g.drawString (“A Simple Applet",20, 20);
}
}
Four of these methods init(), start(), stop(), and destroy() are defined by
Applet.
Although the above program does not do anything, it can be compiled and run.
Creating Applets
/* A simple applet that sets the foreground and background colors and outputs a string. */
import java.awt.*;
import java.applet.*;
/* <applet code="Sample" width=300 height=50> </applet> */
public class Sample extends Applet{
String msg;
public void init() { // set the foreground and background colors.
setBackground(Color.cyan);
setForeground(Color.red);
msg = "Inside init( ) --";
}
public void start() { // Initialize the string to be displayed.
msg += " Inside start( ) --";
}
public void paint(Graphics g) { // Display msg in applet window.
msg += " Inside paint( ).";
g.drawString(msg, 60, 40);
}
}
Passing Parameters to Applet
The APPLET tag in HTML allows you to pass parameters to your applet.
It returns the value of the specified parameter in the form of a String object.
Applet Program Accepting Parameters
import java.applet.Applet;
import java.awt.*;
/* <APPLET CODE="HelloAppletMsg" width=500 height=400>
<PARAM NAME="Greetings" VALUE="Hello Friend, How are you?">
</APPLET> */
import java.awt.*;
import javax.swing.*;
public class myjframe extends JFrame{
myjframe(){
Container contentPane = getContentPane();
JLabel jl=new JLabel("swing more powerful than
AWT");
contentPane.add(jl);
setSize(250,250);
setVisible(true);
}
public static void main(String arg[]){
new myjframe();
}
}