0% found this document useful (0 votes)
27 views

Java 1

Uploaded by

2105915
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views

Java 1

Uploaded by

2105915
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 18

Applet

Chittaranjan Pradhan

Web Technology 14 Applet


<APPLET> Tag

Applet Applet Class


Applet Architecture
Applet Lifecycle
Applet Display Methods
Requesting Repainting
Status Window
Parameter passing to
Applet
getDocumentBase() and
getCodeBase()

Chittaranjan Pradhan
School of Computer Engineering,
KIIT University
14.1
Applet
Applet
Chittaranjan Pradhan
Applet
Applet
• Applets are small applications that are accessed on an <APPLET> Tag

Internet server, transported over the Internet, Applet Class


Applet Architecture

automatically installed, and run as part of a Web document Applet Lifecycle


Applet Display Methods
Requesting Repainting
Status Window
Parameter passing to
Applet
getDocumentBase() and
getCodeBase()

• Applets interact with the user through the AWT(Abstract


Window Toolkit)
• paint() method is defined by the AWT and must be
overridden by the applet. It is called each time that the
applet must redisplay its output
• drawString() is a member of the Graphics class. It outputs
a string beginning at the specified X,Y location
14.2
Applet
Applet...
Chittaranjan Pradhan
Applet...
Applet
• Notice that the applet does not have a main() method. <APPLET> Tag

Unlike Java programs, applets do not begin execution at Applet Class


Applet Architecture

main().In fact, most applets don’t even have a main() Applet Lifecycle
Applet Display Methods
method Requesting Repainting
Status Window

• An applet begins execution when the name of its class is Parameter passing to
Applet

passed to an appletviewer or to a web browser getDocumentBase() and


getCodeBase()

• Compile the source code in the same way that you have
been compiling programs
• java.lang.Object → java.awt.Component →
java.awt.Container → java.awt.Panel → java.applet.Applet
• An applet’s class must be public
• Running of applet involves different process:
• Executing the applet within a Java-compatible Web browser
• Using an applet viewer, such as the standard SDK tool,
appletviewer. An appletviewer executes your applet in a
window. This is generally the fastest and easiest way to test
your applet
14.3
Applet
Applet...
Chittaranjan Pradhan
Executing an applet in a Web browser
Applet
• Here, you need to write a short HTML text file that <APPLET> Tag
Applet Class
contains the appropriate APPLET tag Applet Architecture
Applet Lifecycle
Applet Display Methods
Requesting Repainting
Status Window
Parameter passing to
Applet
• After you create this file, you can execute your browser getDocumentBase() and
getCodeBase()
and then load this file, which causes SimpleApplet to be
executed

• To execute SimpleApplet with an appletviewer, the


command is:
appletviewer a.html

14.4
Applet
Applet...
Chittaranjan Pradhan
Executing an applet using appletviewer
Applet
• Include a comment at the head of your Java source code <APPLET> Tag
Applet Class
file that contains the APPLET tag Applet Architecture
Applet Lifecycle
Applet Display Methods
Requesting Repainting
Status Window
Parameter passing to
Applet
getDocumentBase() and
getCodeBase()

• By doing so, your code is documented with a prototype of


the necessary HTML statements, and you can test your
compiled applet merely by starting the applet viewer with
your Java source code file like
appletviewer SimpleApplet.java

14.5
Applet
<APPLET> Tag
Chittaranjan Pradhan

Applet
<APPLET> Tag
Applet Class
Applet Architecture
Applet Lifecycle

<APPLET> Tag Applet Display Methods


Requesting Repainting
Status Window
<APPLET> tag is used to embed an applet into an HTML page Parameter passing to
Applet
getDocumentBase() and
getCodeBase()
<APPLET CODE=äpplet class name"
CODEBASE=äpplet class file path"
HEIGHT=maximum height"WIDTH=maximum width"
ALIGN=älignmentÄLT=älternate text»
<PARAM NAME=parameter name"VALUE="value»
</APPLET>

14.6
Applet
Applet Class
Chittaranjan Pradhan

Applet Class
Applet
<APPLET> Tag
Applet Class
Applet Architecture
Applet Lifecycle
Applet Display Methods
Requesting Repainting
Status Window
Parameter passing to
Applet
getDocumentBase() and
getCodeBase()

14.7
Applet
Applet Architecture
Chittaranjan Pradhan

Applet

Applet Architecture <APPLET> Tag


Applet Class
Applet Architecture
Applet Lifecycle
• Applets are event driven. An applet waits until an event Applet Display Methods

occurs. The runtime system notifies the applet about an Requesting Repainting
Status Window

event by calling an event handler that has been provided Parameter passing to
Applet

by the applet. Once this happens, the applet must take getDocumentBase() and
getCodeBase()

appropriate action and the quickly returns


• User initiates interaction with an applet. These interactions
are sent to the applet as events to which the applet must
respond

• Applet extends the AWT class Panel. In turn, Panel


extends Container, which extends Component, which
extends Object

14.8
Applet
Applet Lifecycle
Chittaranjan Pradhan

Applet Lifecycle Applet


<APPLET> Tag
When an applet begins, the AWT calls the following methods, Applet Class
Applet Architecture
in this sequence: Applet Lifecycle
Applet Display Methods
• public void init(): it is the first method to be called. This is Requesting Repainting
Status Window
used to initialized the Applet. It is invoked only once Parameter passing to
Applet
getDocumentBase() and
getCodeBase()

• public void start(): it is invoked after the init() method or


browser is maximized. It is used to start the Applet.
Whereas init() is called once, start() is called each time an
applet’s HTML document is displayed on screen

• public void paint(Graphics g): it is used to paint the


Applet. It provides Graphics class object that can be used
for drawing shapes. It is called each time your applet’s
output must be redrawn. Whenever the applet must redraw
its output, paint() is called

14.9
Applet
Applet Lifecycle...
Chittaranjan Pradhan

Applet Lifecycle...
Applet
<APPLET> Tag
When an applet is terminated, the following sequence of Applet Class

method calls takes place: Applet Architecture


Applet Lifecycle

• public void stop(): it is used to stop the Applet. It is Applet Display Methods
Requesting Repainting

invoked when Applet is stop or browser is minimized Status Window


Parameter passing to
Applet
getDocumentBase() and
getCodeBase()
• public void destroy(): it is used to destroy the Applet. It is
invoked only once. The stop() method is always called
before destroy()

update() method is called when your applet has requested that


a portion of its window be redrawn

14.10
Applet
Applet Lifecycle...
Chittaranjan Pradhan

Applet
<APPLET> Tag
Applet Lifecycle... Applet Class
Applet Architecture
Applet Lifecycle
Applet Display Methods
Requesting Repainting
Status Window
Parameter passing to
Applet
getDocumentBase() and
getCodeBase()

14.11
Applet
Applet Display Methods
Chittaranjan Pradhan
Applet Display Methods
Applet
• The drawString() method will not recognize newline <APPLET> Tag
Applet Class
characters Applet Architecture
Applet Lifecycle
• setBackground() and setForeground() used to set the Applet Display Methods

color. These methods are defined by Component Requesting Repainting


Status Window

void setBackground(Color newColor) Parameter passing to


Applet

void setForeground(Color newColor) getDocumentBase() and


getCodeBase()

where, newColor specifies the new color. The class Color


defines the constants shown here that can be used to
specify colors: {Color.black, Color.blue, Color.cyan
Color.magenta, Color.gray Color.green,
Color.lightGray, Color.orange, Color.pink, Color.red,
Color.white, Color.yellow}
• A good place to set the foreground and background colors
is in the init() method
• You can obtain the current settings for the background and
foreground colors by calling getBackground() and
getForeground() 14.12
Applet
Example 1
Chittaranjan Pradhan

Applet
<APPLET> Tag
Applet Class
Applet Architecture
Applet Lifecycle
Applet Display Methods
Requesting Repainting
Status Window
Parameter passing to
Applet
getDocumentBase() and
getCodeBase()

14.13
Applet
Example 2
Chittaranjan Pradhan

Applet
<APPLET> Tag
Applet Class
Applet Architecture
Applet Lifecycle
Applet Display Methods
Requesting Repainting
Status Window
Parameter passing to
Applet
getDocumentBase() and
getCodeBase()

14.14
Applet
Requesting Repainting
Chittaranjan Pradhan

Requesting Repainting
Applet
<APPLET> Tag
• As a general rule, an applet writes to its window only when Applet Class

its update() or paint() method is called by the AWT Applet Architecture


Applet Lifecycle
Applet Display Methods
• Whenever your applet needs to update the information Requesting Repainting

displayed in its window, it simply calls repaint() Status Window


Parameter passing to
Applet
• The repaint() method is defined by the AWT. It causes the getDocumentBase() and
getCodeBase()
AWT run-time system to execute a call to your applet’s
update() method, which, in its default implementation, calls
paint():
• void repaint(): It causes the entire window to be repainted
• void repaint(int left, int top, int width, int height): It
specifies a region that will be repainted. These dimensions
are specified in pixels
• void repaint(long maxDelay): maxDelay specifies the
maximum number of milliseconds that can elapse before
update() is called
• void repaint(long maxDelay, int x, int y, int width, int
height)

14.15
Applet
Status Window
Chittaranjan Pradhan
Status Window
An applet can also output a message to the status window of Applet
<APPLET> Tag
the browser or applet viewer on which it is running by using Applet Class

showStatus() Applet Architecture


Applet Lifecycle
Applet Display Methods
Requesting Repainting
Status Window
Parameter passing to
Applet
getDocumentBase() and
getCodeBase()

14.16
Applet
Parameter passing to Applet
Chittaranjan Pradhan
APPLET tag allows to pass parameters to the applet
Applet
<APPLET> Tag
Applet Class
Applet Architecture
Applet Lifecycle
Applet Display Methods
Requesting Repainting
Status Window
Parameter passing to
Applet
getDocumentBase() and
getCodeBase()

14.17
Applet
getDocumentBase() and getCodeBase()
Chittaranjan Pradhan
getDocumentBase() and getCodeBase()
DocumentBase gives the detailed path of the applet’s class file. Applet
<APPLET> Tag
CodeBase gives the directory from which the applet’s class file Applet Class
Applet Architecture
was loaded Applet Lifecycle
Applet Display Methods
Requesting Repainting
Status Window
Parameter passing to
Applet
getDocumentBase() and
getCodeBase()

14.18

You might also like