Java Applet & its life Cycle By Iqtidar Ali
Java Applet Basic An applet is a java program that runs in a Web browser. An applet can be said as a fully functional java application. When browsing the Web, often the graphical user interface and animation you see are developed by the use of java.
Java Applet Basic An applet is a java class that extends the java.applet.applet class. They are executed either by Web browser or an Applet Viewer. Applets are designed to be embedded within an HTML page.
Java Applet Basic (Cont--) A JVM is required to view an applet. The JVM on the user's machine creates an instance of the applet class The JVM invokes various methods during the applet's lifetime.
Java Application VS Java Applet The way of writing about java application is same as java Applet. Applet and application shares many common features but still have some differences. Application must have main method while applet do not need that. Applet run in Web Browser environment bcoz they are invoked by Web Page.
Java Application VS Java Applet (Cont--) Java provide special features that enables applets to be run from a Web browser. The applet class is an AWT class and is not designed to work with Swing components. To use Swing component in java applet, you need to create a java applet that extends javax.swing.japplet, which is a subclass of java.applet.applet.
Life Cycle of an Applet
Life Cycle of an Applet Four methods in the Applet class give you the framework on which you build any serious applet. init method Start method Stop method Destroy method Paint method // all the above are from an java.applet
Life Cycle of an Applet (Cont--) The init method The init method is invoked after the applet is created. Initialization of methods and variables take place. It is called only once in the life cycle. The Start method The start method is invoked after the init method. It can be call any number of time. It is also called when the user returns to the Web page containing the applet after surfing other pages.
Life Cycle of an Applet (Cont--) The Stop method The stop method is the opposite of the start method. The start method is called when the user moves back to the page that contains the applet. The stop method is invoked when the user leaves the page. It can be called any number of time. The Destroy method The destroy method is invoked when the browser exits normally to inform the applet that it is no longer needed and should release any resources it has allocated. The stop method is always called before the destroy method. It perform memory clean up process.
Life Cycle of an Applet (Cont--) The Paint method Invoked immediately after the start() method, and also any time the applet needs to repaint itself in the browser. The paint() method is actually inherited from the java.awt. General form of paint() is public void paint(graphics g) { // drawstring() of Graphics The drawstring is a member of Graphics class.
import java.applet.*; import java.awt.*; Example public class W_firstapplet extends Applet { public void paint (Graphics g) { g.drawstring("welcome to java Applet World",100,50); /*<applet code= W_firstapplet.class width 1016 height=752> </applet> */
Applet Life Cycle Example
import java.awt.*; import java.applet.*; /* <applet code="appletskel" width=300 height=100> </applet> */ public class AppletSkel extends Applet { // Called first. public void init() { // initialization /* Called second, after init(). Also called whenever the applet is restarted. */ public void start() { // start or resume execution // Called when the applet is stopped. public void stop() { // suspends execution /* Called when applet is terminated. This is the last method executed. */ public void destroy() { // perform shutdown activities // Called when an applet's window must be restored. public void paint(graphics g) { // redisplay contents of window
import java.applet.*; import java.awt.*; public class FirstApp1 extends Applet { public void init() { System.out.println(" I m init"); public void start() { System.out.println(" I m start"); public void paint() { System.out.println(" I m paint"); public void stop() { System.out.println(" I m stop"); public void destory() { System.out.println(" I m destory");
import java.awt.*; import java.applet.*; /* This is HTML Code you have to implement it in HTML <applet code="sample" width=300 height=50> </applet> */ public class Sample extends Applet{ String msg; // set the foreground and background colors. public void init() { setbackground(color.blue); setforeground(color.white); msg = "Inside init( ) --"; // Initialize the string to be displayed. public void start() { msg += " Inside start( ) --"; // Display msg in applet window. public void paint(graphics g) { msg += " Inside paint( )."; g.drawstring(msg, 10, 30);
Example Applet import java.applet.*; import java.awt.*; public class HelloWorldApplet extends Applet { public void paint (Graphics g) { g.drawstring ("Hello World",25,50);
Implementing Applet in HTML <html> <body> <Applet code=testapp width=200 height=200> </Applet> </body> </html> Save the File named TestApp.html Run the file using browser or AppletViewer Appletviewer TestApp.html
import java.awt.*; import java.applet.*; public class OvalApp extends Applet { public void paint(graphics g) { g.setcolor(color.red); g.drawoval(40,40,160,160); g.setcolor(color.blue); g.drawoval(60,60,120,120); g.setcolor(color.green); g.drawoval(65,65,110,110); g.setcolor(color.red); g.drawstring("hello world",100,120);
HTML coding for implementing Applet <html> <body> <Applet code=ovalapp width=200 height=200> </Applet> </body> </html>
The End
Java Applet & its Control By Iqtidar Ali
Control Fundamentals The awt supports the following types of controls: Label Push buttons Check boxes Choice lists Lists Scroll bars Text editing
Button The most widely used control is the push button. A push button is a component that contains a label and that generates an event when it is pressed. Push buttons are objects of type Button.
Button (Cont--) Button defines these two constructors: Button( ):- The first version creates an empty button. Button(String):- The second creates a button that contains string as a label.
Button Applet import java.awt.*; public class week2_2 extends java.applet.applet{ public void init() { Button b1=new Button ("OK"); add(b1);
Text Fields Text Fields provide an area where one can edit or enter a single line of text. Text Fields are usually used for getting inputs from a user. It is created using any of the following constructor. TextField():- Blank TextField(int):- int indicate minimum no s of character to be displayed. TextField(String):- Welcome msg to be displayed by default. TextField(String, int):- Combination of two.
TextField Applet import java.awt.*; public class week2_2 extends java.applet.applet{ public void init() { TextField t1=new TextField(20); add(t1);
LABEL To include a control in a window, you must add it to the window To do this, you must first create an instance of the desired control Then add it to a window by calling add() The syntax is Label compobj=new label() add(compobj); Here, compobj is an instance of the control that you want to add.
Label The easiest control to use is a label A label is an object of type Label, and it contains a string, which it displays Labels are passive controls that do not support any interaction with the user Label defines the following constructors: -Label( ) :- create empty label Label(String):- create label with given string Label(String, int):- Label with string & alignment
Label import java.awt.*; public class week2_2 extends java.applet.applet{ public void init() { Label l1=new Label("Student Name"); add(l1);
Adding Three Label import java.awt.*; import java.applet.*; public class LabelDemo extends Applet { public void init() { Label one = new Label("One"); Label two = new Label("Two"); Label three = new Label("Three"); add(one); add(two); add(three); /* The sample output is shown here<applet code="labeldemo" width=300 height=200></applet>*/
CheckBox import java.awt.*; public class week2_2 extends java.applet.applet{ public void init() { Checkbox c1=new Checkbox ("Male"); add(c1); Checkbox c2=new Checkbox ("Female"); add(c2);
Combine all in one applet import java.awt.*; public class week2_2 extends java.applet.applet{ public void init() { Button b1=new Button ("OK"); TextField t1=new TextField(20); Label l1=new Label("Student Name"); add(b1); add(t1); add(l1);
The END