Studywow: read Applet and Applet Life Cycle topics.
*/
import java.applet. Applet;
import java.awt.Graphics;
/*
<applet code = "BasicAppletExample" width = 200 height = 200>
</applet>
public class BasicAppletExample extends Applet {
public void paint (Graphics g) {
// write text using drawString method of Graphics class
g. drawString ("This is my First Applet", 30, 100);
} }
init() method
|
start() method
|
paint() method
|
stop() method
|
destroy() method
Fig. Life cycle of applet.
import java.applet.Applet;
import java.awt.Graphics;
import java.awt.*;
/*<applet code="AppletLifecycle.class" width="350" height="150"> </applet>*/
public class AppletLifeCycle extends Applet {
public void init()
{
setBackground (Colour.CYAN);
System.out.println("init() called");
}
public void start() {
System.out.println("Start() called"); }
public void paint(Graphics g) {
System.out.println("Paint() called"); }
public void stop() {
System.out.println("Stop () Called"); }
public void destroy() {
System.out.println("Destroy) () Called"); }
}
When the user opens applet life, an HTML page containing the applet life cycle.java in an applet viewer, an instance for each of the applet classes is created using the no-argument constructor, and its init() method is called. As a result, the message" init() called" is displayed in the command window. After execution of the code in the init() method, the program control returns to the applet viewer which then calls the applet's start () method. As a result, the message "start() called" is displayed. Next, the appletviewer calls the applet's paint() method as a result of which the message “paint() called" displayed.
Now when the user minimises the applet window, the stop() method is called and on restoring the window the start() method is called again. When the user exits the applet viewer, the destroy() method called that displays the message "destroy() called" in the command window.
import java.applet.*;
import java.awt.event.*;
import java.awt.*;
public class EventListeners extends Applet
implements ActionListener
{
TextArea txtArea;
String Add, Subtract;
int i=10, j=20, sum=0, Sub=0;
public void int() {
txtArea = new TextArea (10,20);
txtArea.setEditable(false);
add (txtArea,"center");
Button b = new Button ("Add");
Button e = new Button("Subtract");
b.addActionListener(this);
c.addActionListener(this);
add (b);
add (c);
}
public void actionPerformed (ActionEvent e) {
sum = i+ j;
txtArea.setText("");
txtArea.append("i = "+ i +"\t" + "j = "+ j + "\n");
Button source = (Button) e.getSource();
if (source.getLabel() =="Add") {
txtArea.append("Sum : " + sum + "\n");
}
if(i > j)
{
Sub = i - j;
}
else {
Sub = j - i;
}
if(source.getLabel() == "Subtract") {
txtArea.append("Sub : "+ Sub +"\n");
}
} }
abcd
Join us on Facebook and Twitter to get the latest study material. You can also ask us any questions.
Facebook = @ studywow
(click on it or search "studywow" on facebook)
Twitter = @ studywow
(click on it or search "studywow" on Twitter)
Email= studywow.com@gmail.com
Send us your query anytime!
Q1. What do you mean by Java applets?
Ans. An applet is a Java program that runs in a web browser. An applet can be a fully functional Java application because it has the entire Java API at its disposal. The Java applet executes within a Java Virtual Machine (JVM) in a process separate from the web-browser itself, yet it can appear in a frame of the web page, in a new application window, or in Sun's AppletViewer. Java applets were introduced in the first version of the Java language in 1995.Q2. Write a program of basic Java applet?
Ans. Following example demonstrate how to create a basic applet using Java applet class :*/
import java.applet. Applet;
import java.awt.Graphics;
/*
<applet code = "BasicAppletExample" width = 200 height = 200>
</applet>
public class BasicAppletExample extends Applet {
public void paint (Graphics g) {
// write text using drawString method of Graphics class
g. drawString ("This is my First Applet", 30, 100);
} }
Q3. Explain all the methods of applet life cycle in brief?
Ans. Applet Life Cycle : An applet undergoes between its object creation and object removal is known as its life cycle. In this, each state is represented by a method. These methods are also known as "callback methods”. Following are the methods of the applet life cycle :init() method
|
start() method
|
paint() method
|
stop() method
|
destroy() method
Fig. Life cycle of applet.
1. init():
In this method, the applet object is created. Because this method is called before all the other methods, programmer can utilise this method to instantiate objects, initialise variables, setting background and foreground colours etc. It is equivalent to born state of a thread. This method is called at the time of starting the execution.2. start():
In init() method, even through applet object is created, it is in inactive state. To make the applet active, the init() method calls start() method. In start () method, applet becomes active and thereby eligible for processor time. This method is called a number of times.3. paint():
This method takes a java.awt.Graphics object as parameter. This class includes many methods of drawing necessary to draw on the applet window. This is the place where the programmer can write his code of what he expects from applet (like animation etc). This is equivalent to runnable state of thread. This method is called by the start() method.4. stop() :
In this method, the applet becomes temporarily inactive. An applet can come any number of times into this method in its life cycle and can go back to the active state (paint() method) whenever would like. It is the best place to have cleanup code. It is equivalent to the blocked state of the thread.5. destroy():
This method is called just before an applet object, is garbage collected. This is the end of the life cycle of applet. It is the best place to have clean-up code. It is equivalent to the dead state of the thread.Q4. Write the all methods used to create applet with example?
Ans. Example to Show Applet Life Cycle in Javaimport java.applet.Applet;
import java.awt.Graphics;
import java.awt.*;
/*<applet code="AppletLifecycle.class" width="350" height="150"> </applet>*/
public class AppletLifeCycle extends Applet {
public void init()
{
setBackground (Colour.CYAN);
System.out.println("init() called");
}
public void start() {
System.out.println("Start() called"); }
public void paint(Graphics g) {
System.out.println("Paint() called"); }
public void stop() {
System.out.println("Stop () Called"); }
public void destroy() {
System.out.println("Destroy) () Called"); }
}
When the user opens applet life, an HTML page containing the applet life cycle.java in an applet viewer, an instance for each of the applet classes is created using the no-argument constructor, and its init() method is called. As a result, the message" init() called" is displayed in the command window. After execution of the code in the init() method, the program control returns to the applet viewer which then calls the applet's start () method. As a result, the message "start() called" is displayed. Next, the appletviewer calls the applet's paint() method as a result of which the message “paint() called" displayed.
Now when the user minimises the applet window, the stop() method is called and on restoring the window the start() method is called again. When the user exits the applet viewer, the destroy() method called that displays the message "destroy() called" in the command window.
Q5. Write an applet program in Java to create an event listener?
Ans. Following example demonstrates how to create a basic applet having buttons to add and Subtract two numbers. Methods used here are addActionListener() to listen to an event(click on a button) and Button() constructor to create a button.import java.applet.*;
import java.awt.event.*;
import java.awt.*;
public class EventListeners extends Applet
implements ActionListener
{
TextArea txtArea;
String Add, Subtract;
int i=10, j=20, sum=0, Sub=0;
public void int() {
txtArea = new TextArea (10,20);
txtArea.setEditable(false);
add (txtArea,"center");
Button b = new Button ("Add");
Button e = new Button("Subtract");
b.addActionListener(this);
c.addActionListener(this);
add (b);
add (c);
}
public void actionPerformed (ActionEvent e) {
sum = i+ j;
txtArea.setText("");
txtArea.append("i = "+ i +"\t" + "j = "+ j + "\n");
Button source = (Button) e.getSource();
if (source.getLabel() =="Add") {
txtArea.append("Sum : " + sum + "\n");
}
if(i > j)
{
Sub = i - j;
}
else {
Sub = j - i;
}
if(source.getLabel() == "Subtract") {
txtArea.append("Sub : "+ Sub +"\n");
}
} }
abcd
Join us on Facebook and Twitter to get the latest study material. You can also ask us any questions.
Facebook = @ studywow
(click on it or search "studywow" on facebook)
Twitter = @ studywow
(click on it or search "studywow" on Twitter)
Email= studywow.com@gmail.com
Send us your query anytime!