[go: up one dir, main page]

0% found this document useful (0 votes)
37 views24 pages

VIPS OOPS Unit 3 Applet

Uploaded by

nishtha.kansal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
37 views24 pages

VIPS OOPS Unit 3 Applet

Uploaded by

nishtha.kansal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 24

Applet

Faculty : Dr. Nishtha Kansal


Assistant Professor
VIPS-TC
Applet
What is Applet?
An applet is a Java program that can be embedded into a web page.
 It runs inside the web browser and works at client side.
 An applet is embedded in an HTML page using the APPLET or
OBJECT tag and hosted on a web server.
Applets are used to make the website more dynamic and entertaining.
Applets are small applications that are accessed on an Internet server,
transported over the Internet, automatically installed, and run as part
of a webdocument.
Important Funtion of Applet :

1. All applets are sub-classes (either directly or indirectly) of


java.applet.Applet class.
2. Applets are not stand-alone programs. Instead, they run within either a
web browser or an applet viewer.
3. JDK provides a standard applet viewer tool called applet viewer.
4. In general, execution of an applet does not begin at main() method.
5. Output of an applet window is not used System.out.println(). Rather
it is handled with various AWT methods, such as drawString().
Java Applet class Hierarchy
Class Hierarchy

import java.lang.Object
 import java.awt.Component
(implementsjava.awt.image.ImageObserver,java.awt.MenuCont
ainer,java.io.Serializable)
 import java.awt.Container
 import java.awt.Panel
import java.applet.Applet
Applet Statements
• This applet begins with two import statements.
 The first imports the Abstract WindowToolkit (AWT) classes.
Applets interact with the user (either directly or indirectly) through the AWT,
not through the console-based I/O classes.
Abstract Windowing Toolkit AWT
Earlier versions of Java
Applet class is one of the AWT components
Java Foundation Classes JFC
Extension to Java in 1997
Has a collection of Swing components for enhanced GUIs
Swing component classes begin with J
Life Cycle of Applet
Applet method
public void init () Also:
public void start () public void repaint()
public void stop () public void update (Graphics)
public void destroy () public void showStatus(String)
public void paint (Graphics) public String
getParameter(String)
Life Cycle of Applet
Life Cycle of Applet consists of four phases:

1. Applet Initialized:Objects regarding Applet get initialized in this phase. An init() method is called for the
same purpose.

2. Applet Running:Applets are embedded in a webpage. When a part of webpage which consists of applet is
shown on a screen, then applet is in Running phase. A start() method is called, which takes applet to the
Running phase.

3. Applet Stopped:When a webpage in minimized or webpage is scrolled down/up; so that, applet is


disappeared from the screen, then applet is in Stopped phase. Stop() method is called when applet is
disappeared from the screen.

4. Applet Destroyed:When you closed the webpage which consists of applet, applet is get destroyed. A
destroy() method is called when applet is destroyed.
public void init()

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.
public void init(): is used to initialized the Applet.
It is invoked only once.
public void start()
 It is invoked after the init() method or browser is maximized.
 It is used to start the Applet.
The start( ) method is called after init( ).
 It is also called to restart an applet after it has been stopped.
Whereas init( ) is called once—the first time an applet is loaded—
start( ) is called each time an applet’s HTML document is displayed
onscreen.
So, if a user leaves a web page and comes back, the applet resumes
execution at start( ).
public void stop()
The stop( ) method is called when a web browser leaves the HTML
document.
When stop( ) is called, the applet is probably running.
You can use stop( ) method if the applet is doing heavy computation
that you don’t want to continue when the browser is on some other
page.
You can restart them when start( ) is called if the user returns to the
page.
public void stop(): is used to stop the Applet. It is invoked when
Applet is stop or browser is minimized.
public void destroy()

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.
 It is used to destroy the Applet. It is invoked only once.
Use to explicitly release system resources (like threads).
System resources are usually released automatically.
public void paint(Graphics g)
 It is used to paint the Applet. It provides Graphics class object that can be used for drawing oval, rectangle,
arc etc.
 The paint( ) method is called each time your applet’s output must be redrawn. This situation can occur for
several reasons.
 paint( ) is also called when the applet begins execution.
 g.drawString("STRING ",x,y);
 Whatever the cause, whenever the applet must redraw its output, paint( ) is called.
 The paint( ) method has one parameter of type Graphics. This parameter will contain the graphics context,
 which describes the graphics environment in which the applet is running. This context is used whenever
output to the applet is required.
 Needed if you do any drawing or painting other than just using standard GUI Components.
 Any painting you want to do should be done here, or in a method you call from here.
 Painting that you do in other methods may or may not happen.
 Never call paint(Graphics), call repaint( ).
public void repaint()
Call repaint( ) when you have changed something and want your
changes to show up on the screen
repaint( ) is a request--it might not happen
When you call repaint( ), Java schedules a call to update(Graphics g)
• The repaint( ) method has four forms. repaint( ) is shown here:
• void repaint( )
• This version causes the entire window to be repainted. The following
version specifies a region that will be repainted:
• void repaint(int left, int top, int width, int height)
 java.awt.Component class
The Component class provides 1 life cycle method of applet.

 public void paint(Graphics g): is used to paint the Applet. It provides Graphics class object
that can be used for drawing oval, rectangle, arc etc.
Q Who is responsible to manage the life cycle of an applet?
Java Plug-in software.

Q How to run an Applet?


There are two ways to run an applet
 By html file.
 By appletViewer tool (for testing purpose).
public void paint(Graphics g)
Note: public void init(), public void start(), public void stop(), public
void destroy() methods are given by Applet class. Applet class is given
by package java.applet.

One more method, public void paint(Graphics g) , is used in life cycle


of applet. It is used to redraw applet. It is given by Component class.
Component class is given by package java.awt.
Simple Applet Display Methods
To output a string to an applet, use drawString( ), which is a member of
the Graphics class. Typically, it is called from within either update( ) or
paint( ).
It has the following general form:
• void drawString(String message, int x, int y)
These methods are defined by Component, and they have the following
general forms:
• void setBackground(Color newColor)
• void setForeground(Color newColor)
Applet Simple Program
Output:
//Firstapplet.java
import java.applet.Applet;
import java.awt.Graphics;
public class Firstapplet extends Applet{

public void paint(Graphics g){


g.drawString("welcome to applet",150,150);
} }
/* <applet code="Firstapplet.class" width="300"
height="300">
</applet> */
Simple Applet Display Methods
/* A simple applet that sets the foreground and // Initialize the string to be displayed.
background colors and outputs a string. */ public void start() {
import java.awt.*; msg += " Inside start( ) --";
import java.applet.*; }
/*<applet code="Sample" width=300 height=50> // Display msg in applet window.
</applet>*/ public void paint(Graphics g) {
public class Sample extends Applet{ msg += " Inside paint( ).";
String msg; g.drawString(msg, 10, 30);
// set the foreground and background colors. }}
public void init() { Output:
setBackground(Color.cyan);
setForeground(Color.red);
msg = "Inside init( ) --";
}
The HTML APPLET Tag
Syntax for Applet tag
< APPLET
APPLET tag be used to start an [CODEBASE = codebaseURL]
applet from both an HTML CODE = appletFile
document and from an applet [ALT = alternateText]
viewer. [NAME = appletInstanceName]
WIDTH = pixels HEIGHT = pixels
An applet viewer will execute each [ALIGN = alignment]
APPLET tag that it finds in a [VSPACE = pixels] [HSPACE = pixels] >
separate window, while web [< PARAM NAME = AttributeName VALUE =
AttributeValue>]
browsers will allow many applets [< PARAM NAME = AttributeName2 VALUE =
on a single page. AttributeValue>]
So far, we have been using only a . . .[HTML Displayed in the absence of Java]
</APPLET>
simplified form of the APPLET tag.
The HTML APPLET Tag

• CODEBASE: CODEBASE is an optional • ALT The ALT tag is an optional


attribute that specifies the base URL of the attribute used to specify a short text
applet code, which is the directory that will message that should be displayed if the
be searched for the applet’s executable class
file (specified by the CODE tag). The
browser recognizes the APPLET tag
HTML document’s URL directory is used but can’t currently run Java applets.
as the CODEBASE if this attribute is not • NAME:To obtain an applet by name,
specified. use getApplet( ), which is defined
• CODE CODE is a required attribute that
• by the AppletContext interface.
gives the name of the file containing your
applet’s compiled .class file. This file is • WIDTH and HEIGHT: Width and
relative to the code base URL of the applet, Height are required attributes that give
which is the directory that the HTML file the size (in pixels) of the applet display
was in or the directory indicated by area.
CODEBASE if set.

You might also like