0% found this document useful (0 votes)
31 views23 pages

Unit 6.1

This document discusses applets in Java. It begins by defining an applet and explaining that applets are small Java programs that can be embedded in web pages. It then covers the applet class hierarchy, the differences between applets and applications, the lifecycle of an applet including the init(), start(), stop(), and destroy() methods, types of applets including AWT and Swing applets, how to structure an applet code example, and how to build and run a simple "Hello World" style applet.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views23 pages

Unit 6.1

This document discusses applets in Java. It begins by defining an applet and explaining that applets are small Java programs that can be embedded in web pages. It then covers the applet class hierarchy, the differences between applets and applications, the lifecycle of an applet including the init(), start(), stop(), and destroy() methods, types of applets including AWT and Swing applets, how to structure an applet code example, and how to build and run a simple "Hello World" style applet.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 23

UNIT-6

Advantages of GUI over CUI


The AWT class hierarchy
Component, Frame
Event handling: Delegation Event model
Closing a frame, mouse and keyboard events
Adapter classes, User interface components
Labels, button, scrollbars
Text components, check box, check box groups, choices
Lists panels – scrollpane, menubar, graphics
Layout manager – layout manager types
Concepts of Applets, differences between applets and
Applications
Life cycle of an applet
Types of applets, creating applets, passing parameters to applets
What is an applet?
Applet: A small Java program that can be inserted into a web page and
run by loading that page in a browser.

An applet is a special kind of Java program that is designed to be


transmitted over the Internet and automatically executed by a Java-
compatible web browser.

Applets are small applications that are accessed on an Internet server,


transported over the Internet, automatically installed, and run as part of
a web document.
Applet classes in Java
java.lang.Object
java.awt.Component
java.awt.Container
java.awt.Panel
java.applet.Applet
javax.swing.JApplet
How Applets Differ from Applications
Although both the Applets and stand-alone applications are Java
programs, there are certain restrictions are imposed on Applets due to
security concerns:

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.

The above restrictions ensures that an Applet cannot do any damage to


the local system.
1. Applets can be embedded in HTML pages and downloaded over the
Internet whereas Applications have no special support in HTML for
embedding or downloading.

2. Applets can only be executed inside a java compatible web browser or


appletviewer whereas Applications are executed at command line by java.

3. After an applet arrives on the client, it has limited access to resources on


local computer. Applications have no restriction to access resources.

4. Applets don’t have the main() method as in applications. Instead they


operate on an entirely different mechanism where they are initialized by
init(),started by start(),stopped by stop() or destroyed by destroy().

5. A Java Applet is made up of at least one public class that has to be


subclasses from java.applet.Applet. Whereas, A Java application is made
up of a main() method declared as public static void that accepts a string
array argument, along with any other classes that main() calls.
Life cycle of an Applet

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.

 When an applet is started , the following sequence of method calls takes


place:
1. init( )
2. start( )
3. paint( )

 When an applet is terminated, the following sequence of method calls takes


place:
1. stop( )
2. destroy( )
Applet States
Initialisation
 The init( ) method is the first method to be called.
 This is where you should initialize variables.
This method is called only once during the run time of your applet.

Running – more than once


The start( ) method is called after init( ).
It is also called to restart an applet after it has been stopped.
It is called each time an applet’s HTML document is displayed on screen.
So, if a user leaves a web page and comes back, the applet resumes execution at start( ).

Display – more than once


 paint() happens immediately after the applet enters into the running state.
It is responsible for displaying output.
 paint( ) is also called when the applet begins execution.
 The paint( ) method is called each time your applet’s output must be redrawn.
 The paint( ) method has one parameter of type Graphics.
Idle
 The stop( ) method is called when a web browser leaves the HTML document containing
the applet—when it goes to another page.

Dead/Destroyed State – only once


 The destroy( ) method is called when the environment determines that your applet needs
to be removed completely from memory.

 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

There are two types of applets:

 First is based on the Applet class


 These applets use the AWT to provide the GUI.
 This style of applet has been available since java was created.
 It is used for simple GUI’s.

 The second is based on the Swing class JApplet.


 These applets use the Swing classes to provide the GUI.
 Swing offers a richer and easy to use interface than AWT .
 Swing based applets are more popular.
Structure of an applet
// An Applet AppletStructure // Called when the applet is stopped.
import java.awt.*; public void stop() {
import java.applet.*; // suspends execution
}
/* <applet code="AppletStructure"
width=300 height=100> </applet> */ /* Called when applet is terminated.
This is the last method executed. */
public class AppletStructure extends Applet { public void destroy() {
// perform shutdown activities
}
// Called first.
public void init() {
// Called whenever an applet's output
// initialization must be redisplayed.
} public void paint(Graphics g) {
// redisplay contents of window
/* Called second, after init(). Also called }
whenever the applet is restarted. */ }
public void start() {
// start or resume execution
}
Building Applet Code: An Example
import java.awt.*;
import java.applet.Applet;
public class SimpleApplet extends Applet {
public void paint(Graphics g) {
g.drawString (“A Simple Applet",20, 20);
}
}

 Begins with two import classes.


 java.awt.* -- required for GUI
 java.applet.* -- every applet you create must be a subclass of
Applet, which is in java.applet package.
 The class should start with public, because is accessed from
outside.
Contd..
 Applets do not begin execution at main().
 An applet begins its execution when the name of its class is passed to
an applet viewer or to a java compatible browser.
 Compile the applet in the same way that we have been compiling
programs.
 Running an applet involves a different process.
Running an Applet
There are two ways in which you can run an applet:

 Executing the applet within a Java-compatible browser.

 Using a tool called , appletviewer. An applet viewer executes your


applet in a window. This is generally the fastest and easiest way to
test your applet.
Executing in a web browser.
 To execute an applet in a web browser, you need to write a short
HTML file that contains a tag ( Applet ) that loads the applet.

HTML file that contains a SimpleApplet

<APPLET code=“SimpleApplet“ width=400 height=300>


</APPLET>

 Save the file with .html extension (Example: Simple.html)

 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.

 Another, paint() is defined by the AWT Component class.

 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.

 To retrieve a parameter, use the getParameter( ) method.

 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> */

public class HelloAppletMsg extends Applet {


String msg;
public void init(){
msg = getParameter("Greetings");
if( msg == null)
msg = "Hello";
} This is name of parameter specified in PARAM tag;
This method returns the value of paramter.
public void paint(Graphics g) {
g.drawString (msg,10, 100);
}
}
Simple Swing Application

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();
}
}

You might also like