SlideShare une entreprise Scribd logo
1  sur  31
APPLETS
BY
SANA MATEEN
APPLET
• 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.
• Advantage of Applet
• There are many advantages of applet. They are as follows:
• It works at client side so less response time.
• Secured
• It can be executed by browsers running under many platforms, including Linux,
Windows, Mac Os etc.
• Drawback of Applet
• Plugin is required at client browser to execute applet.
APPLET
• An applet is a Java class that extends the java.applet.Applet class.
• A main() method is not invoked on an applet, and an applet class will not
define main().
• Applets are designed to be embedded within an HTML page.
• When a user views an HTML page that contains an applet, the code for the
applet is downloaded to the user's machine.
• A JVM is required to view an applet. The JVM can be either a plug-in of
the Web browser or a separate runtime environment.
• The JVM on the user's machine creates an instance of the applet class and
invokes various methods during the applet's lifetime.
• Applets have strict security rules that are enforced by the Web browser. The
security of an applet is often referred to as sandbox security, comparing the
applet to a child playing in a sandbox with various rules that must be
followed.
• Other classes that the applet needs can be downloaded in a single Java
Archive (JAR) file.
Life Cycle of an Applet
• Four methods in the Applet class gives you the framework on which you build any
serious applet −
• It is important to understand the order in which the various methods shown in the
skeleton are called.
• When an applet begins, the following methods are called, in this sequence: 1. init( )
2. start( ) 3. paint( )
• When an applet is terminated, the following sequence of method calls takes place:
1. stop( ) 2. destroy( )
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() {
// initialization
}
start()
• 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 start() {
• // start or resume execution
• }
paint()
1. The paint( ) method is called each time an AWT-based applet’s output must
be redrawn.
2. The applet window may be minimized and then restored. paint( ) is also called
when the applet begins execution.
3. Whatever the cause, whenever the applet must redraw its output, paint( ) is
called.
4. The paint( ) method has one parameter of type Graphics.
5. This parameter will contain the graphics context, which describes the
graphics environment in which the applet is running.
6. This context is used whenever output to the applet is required
7. public void paint(Graphics g) {
8. // redisplay contents of window
9. }
stop()
• The stop( ) method is called when a
web browser leaves the HTML
document containing the applet—
when it goes to another page, for
example.
• When stop( ) is called, the applet is
probably running. You should use
stop( ) to suspend threads that don’t
need to run when the applet is not
visible.
• You can restart them when start( ) is
called if the user returns to the page.
• public void stop() {
• // suspends execution
• }
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( ).
• public void destroy() {
• // perform shutdown activities
• }
Applet class
• The Applet class is contained in the java.applet package.
• There are two varieties of applets based on Applet.
• The first are those based directly on the Applet class.These applets use the Abstract
Window Toolkit (AWT) to provide the graphical user interface (or use no GUI at all).
This style of applet has been available since Java was first created.
• The second type of applets are those based on the Swing class JApplet, which
inherits Applet.
• Swing applets use the Swing classes to provide the GUI.
• Swing offers a richer and often easier-to-use user interface than does the AWT.
Thus, Swing-based applets are now the most popular.
• However, traditional AWT-based applets are still used, especially when only a very
simple user interface is required
• AWT-based applets are subclasses of Applet. Applets are not stand-alone
programs. Instead, they run within either a web browser or an applet viewer.
deployment strategy
• Before an applet can be used, a deployment strategy must be chosen
• The second basic approach to deploying an applet is to specify the applet directly
in an HTML file.
• This is the original way that applets were launched when Java was created, and it is
still used today—especially for simple applets.
• Therefore, the APPLET tag is used in this book. (Be aware that the APPLET tag is
currently deprecated by the HTML specification. The alternative is the OBJECT
tag. You should check the JDK documentation in this regard for the latest
recommendations.)
• When an APPLET tag is encountered in the HTML file, the specified applet will be
executed by a Java-enabled web browser.
• The use of the APPLET tag offers a secondary advantage when developing applets
because it enables you to easily view and test the applet.
• To do so, simply include a comment at the head of your Java source code file that
contains the APPLET tag.
• This way, your code is documented with the necessary HTML statements needed by
your applet, and you can test the compiled applet by starting the applet viewer
with your Java source code file specified as the target. Here is an example of such a
comment: /*<applet code="compute" width=500 height=200></applet>*/
3.A.Develop an applet in Java that displays a simple message.
3.b) Develop an applet in Java that receives an integer in one text field, and
computes its factorial Value and returns it in another text field, when the
button named “Compute” is clicked.
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)
• Here, message is the string to be output beginning at x,y. In a Java window,
the upper-left corner is location 0,0.
• The drawString( ) method will not recognize newline characters.
• If you want to start a line of text on another line, you must do so manually,
specifying the precise X,Y location where you want the line to begin.
• To set the background color of an applet’s window, use setBackground( ).
• To set the foreground color (the color in which text is shown, for example),
use setForeground( ).
• These methods are defined by Component, and they have the following
general forms:
• void setBackground(Color newColor)
• void setForeground(Color newColor)
• Here, newColor specifies the new color.
The class Color defines the constants shown here that can be used to
specify colors:
You can obtain the current settings for the background and foreground
colors by calling getBackground( ) and getForeground( ), respectively.
They are also defined by Component and are shown here:
Color getBackground( )
Color getForeground( )
Requesting Repainting
• How can the applet itself cause its window to be updated when its
information changes?
• For example, if an applet is displaying a moving banner, what mechanism
does the applet use to update the window each time this banner scrolls?
• Remember, one of the fundamental architectural constraints imposed on an
applet is that it must quickly return control to the run-time system.
• It cannot create a loop inside paint( ) that repeatedly scrolls the banner, for
example.
• This would prevent control from passing back to the AWT. Given this
constraint, it may seem that output to your applet’s window will be difficult
at best.
• Fortunately, this is not the case. Whenever your applet needs to update the
information displayed in its window, it simply calls repaint( ).
• The repaint( ) method is defined by the AWT. It causes the AWT run-time
system to execute a call to your applet’s update( ) method, which, in its
default implementation, calls paint( )
• Thus, for another part of your applet to output to its window, simply store the
output and then call repaint( ).
• The AWT will then execute a call to paint( ), which can display the stored
information.
• For example, if part of your applet needs to output a string, it can store this string
in a String variable and then call repaint( ).
• Inside paint( ), you will output the string using drawString( ).
• The repaint( ) method has four forms. Let’s look at each one, in turn.
• The simplest version of 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)
• Here, the coordinates of the upper-left corner of the region are specified by left
and top, and the width and height of the region are passed in width and height.
• These dimensions are specified in pixels. You save time by specifying a region to
repaint. Window updates are costly in terms of time.
• If you need to update only a small portion of the window, it is more efficient
to repaint only that region.
• Calling repaint( ) is essentially a request that your applet be repainted
sometime soon. However, if your system is slow or busy, update( ) might not be
called immediately.
• Multiple requests for repainting that occur within a short time can be
collapsed by the AWT in a manner such that update( ) is only called
sporadically.
• This can be a problem in many situations, including animation, in which a
consistent update time is necessary.
• One solution to this problem is to use the following forms of repaint( ):
• void repaint(long maxDelay)
• void repaint(long maxDelay, int x, int y, int width, int height)
• Here, maxDelay specifies the maximum number of milliseconds that can
elapse before update( ) is called. Beware, though. If the time elapses before
update( ) can be called, it isn’t called. There’s no return value or exception
thrown, so you must be careful.
Using the Status Window
• In addition to displaying information in its window, an applet can also
output a message to the status window of the browser or applet viewer
on which it is running.
• To do so, call showStatus( ) with the string that you want displayed. The
status window is a good place to give the user feedback about what is
occurring in the applet, suggest options, or possibly report some types of
errors.
• The status window also makes an excellent debugging aid, because it gives
you an easy way to output information about your applet. The following
applet demonstrates showStatus( ):
The HTML APPLET Tag
• The syntax for a fuller form of the APPLET tag is shown here.
• Bracketed items are optional.
• < APPLET [CODEBASE = codebaseURL]
• CODE = appletFile
• [ALT = alternateText]
• [NAME = appletInstanceName]
• WIDTH = pixels
• HEIGHT = pixels
• [ALIGN = alignment ]
• [VSPACE = pixels]
• [HSPACE = pixels] >
• [< PARAM NAME = AttributeName VALUE = AttributeValue>]
• [< PARAM NAME = AttributeName2 VALUE = AttributeValue>]
• . . .
• [HTML Displayed in the absence of Java]
• < /APPLET>
• Let’s take a look at each part now.
• CODEBASE
• CODEBASE is an optional attribute that specifies the base URL of the applet
code, which is the directory that will be searched for the applet’s executable
class file (specified by the CODE tag). The HTML document’s URL directory is
used as the CODEBASE if this attribute is not specified.
• CODE
• CODE is a required attribute that gives the name of the file containing your
applet’s compiled .class file. This file is relative to the code base URL of the
applet, which is the directory that the HTML file was in or the directory
indicated by CODEBASE if set.
• ALT
• The ALT tag is an optional attribute used to specify a short text message that
should be displayed if the browser recognizes the APPLET tag but can’t
currently run Java applets. This is distinct from the alternate HTML you provide
for browsers that don’t support applets.
• NAME
• NAME is an optional attribute used to specify a name for the applet instance.
Applets must be named in order for other applets on the same page to find
them by name and communicate with them. To obtain an applet by name, use
getApplet( ), which is defined by the AppletContext interface.
• WIDTH and HEIGHT
• WIDTH and HEIGHT are required attributes that give the size (in pixels) of the
applet display area.
• ALIGN
• ALIGN is an optional attribute that specifies the alignment of the applet. This
attribute is treated the same as the HTML IMG tag with these possible values:
LEFT, RIGHT, TOP, BOTTOM, MIDDLE, BASELINE, TEXTTOP, ABSMIDDLE, and
ABSBOTTOM.
• VSPACE and HSPACE These attributes are optional.
• VSPACE specifies the space, in pixels, above and below the applet.
• HSPACE specifies the space, in pixels, on each side of the applet. They’re
treated the same as the IMG tag’s VSPACE and HSPACE attributes.
• PARAM NAME and VALUE
• The PARAM tag allows you to specify applet-specific arguments. Applets access
their attributes with the getParameter( ) method.
• Other valid APPLET attributes include ARCHIVE, which lets you specify one or
more archive files, and OBJECT, which specifies a saved version of the applet.
In general, an APPLET tag should include only a CODE or an OBJECT attribute,
but not both.
• The AudioClip Interface
• The AudioClip interface defines these methods:
• play( ) (play a clip from the beginning), stop( ) (stop playing the clip), and
loop( ) (play the loop continuously). After you have loaded an audio clip
using getAudioClip( ), you can use these methods to play it.
• The AppletStub Interface
• The AppletStub interface provides the means by which an applet and the
browser (or applet viewer) communicate. Your code will not typically
implement this interface.
• Outputting to the Console
• Although output to an applet’s window must be accomplished through GUI-
based methods, such as drawString( ), it is still possible to use console
output in your applet—especially for debugging purposes. In an applet,
when you call a method such as System.out.println( ), the output is not
sent to your applet’s window.

Contenu connexe

Tendances

Hospital Management System
Hospital Management SystemHospital Management System
Hospital Management SystemRup Chowdhury
 
Understanding react hooks
Understanding react hooksUnderstanding react hooks
Understanding react hooksMaulik Shah
 
Hospital management system in java
Hospital management system in javaHospital management system in java
Hospital management system in javaVarun Yadav
 
The Benefits of Using React JS for Web Development!
The Benefits of Using React JS for Web Development!The Benefits of Using React JS for Web Development!
The Benefits of Using React JS for Web Development!Baharika Sopori
 
introduction about REST API
introduction about REST APIintroduction about REST API
introduction about REST APIAmilaSilva13
 
REST API and CRUD
REST API and CRUDREST API and CRUD
REST API and CRUDPrem Sanil
 
Internship presentation for E-Commerce Website
Internship presentation for E-Commerce WebsiteInternship presentation for E-Commerce Website
Internship presentation for E-Commerce WebsiteArvind Singh Rawat
 
React state managmenet with Redux
React state managmenet with ReduxReact state managmenet with Redux
React state managmenet with ReduxVedran Blaženka
 
Calculator using Java
Calculator using JavaCalculator using Java
Calculator using JavaGarvit Anand
 
What Is Virtual DOM In React JS.pptx
What Is Virtual DOM In React JS.pptxWhat Is Virtual DOM In React JS.pptx
What Is Virtual DOM In React JS.pptxAkrati Rawat
 
Oracle HRMS Payroll Table Overview
Oracle HRMS Payroll Table OverviewOracle HRMS Payroll Table Overview
Oracle HRMS Payroll Table OverviewChris Martin
 
AngularJS - Services
AngularJS - ServicesAngularJS - Services
AngularJS - ServicesNir Kaufman
 
Web development using javaScript, React js, Node js, HTML, CSS and SQL
Web development using javaScript, React js, Node js, HTML, CSS and SQLWeb development using javaScript, React js, Node js, HTML, CSS and SQL
Web development using javaScript, React js, Node js, HTML, CSS and SQLJayant Surana
 

Tendances (20)

Hospital Management System
Hospital Management SystemHospital Management System
Hospital Management System
 
Understanding react hooks
Understanding react hooksUnderstanding react hooks
Understanding react hooks
 
Exploring HL7 CDA & Its Structures
Exploring HL7 CDA & Its StructuresExploring HL7 CDA & Its Structures
Exploring HL7 CDA & Its Structures
 
Hospital management system in java
Hospital management system in javaHospital management system in java
Hospital management system in java
 
The Benefits of Using React JS for Web Development!
The Benefits of Using React JS for Web Development!The Benefits of Using React JS for Web Development!
The Benefits of Using React JS for Web Development!
 
React js
React jsReact js
React js
 
introduction about REST API
introduction about REST APIintroduction about REST API
introduction about REST API
 
REST API and CRUD
REST API and CRUDREST API and CRUD
REST API and CRUD
 
Internship presentation for E-Commerce Website
Internship presentation for E-Commerce WebsiteInternship presentation for E-Commerce Website
Internship presentation for E-Commerce Website
 
React state managmenet with Redux
React state managmenet with ReduxReact state managmenet with Redux
React state managmenet with Redux
 
React JS - Introduction
React JS - IntroductionReact JS - Introduction
React JS - Introduction
 
C# REST API
C# REST APIC# REST API
C# REST API
 
Simple React Todo List
Simple React Todo ListSimple React Todo List
Simple React Todo List
 
Calculator using Java
Calculator using JavaCalculator using Java
Calculator using Java
 
What Is Virtual DOM In React JS.pptx
What Is Virtual DOM In React JS.pptxWhat Is Virtual DOM In React JS.pptx
What Is Virtual DOM In React JS.pptx
 
Oracle HRMS Payroll Table Overview
Oracle HRMS Payroll Table OverviewOracle HRMS Payroll Table Overview
Oracle HRMS Payroll Table Overview
 
AngularJS - Services
AngularJS - ServicesAngularJS - Services
AngularJS - Services
 
Oracle Payables Reference Guide
Oracle Payables Reference GuideOracle Payables Reference Guide
Oracle Payables Reference Guide
 
HOSPITAL MANAGEMENT SYSTEM project report
HOSPITAL MANAGEMENT SYSTEM project reportHOSPITAL MANAGEMENT SYSTEM project report
HOSPITAL MANAGEMENT SYSTEM project report
 
Web development using javaScript, React js, Node js, HTML, CSS and SQL
Web development using javaScript, React js, Node js, HTML, CSS and SQLWeb development using javaScript, React js, Node js, HTML, CSS and SQL
Web development using javaScript, React js, Node js, HTML, CSS and SQL
 

Similaire à Applets (20)

27 applet programming
27  applet programming27  applet programming
27 applet programming
 
Java applet
Java appletJava applet
Java applet
 
L18 applets
L18 appletsL18 applets
L18 applets
 
Applet in java
Applet in javaApplet in java
Applet in java
 
Applet.pptx
Applet.pptxApplet.pptx
Applet.pptx
 
Java applet
Java appletJava applet
Java applet
 
applet.pptx
applet.pptxapplet.pptx
applet.pptx
 
Applets 101-fa06
Applets 101-fa06Applets 101-fa06
Applets 101-fa06
 
Applet and graphics programming
Applet and graphics programmingApplet and graphics programming
Applet and graphics programming
 
Till applet skeleton
Till applet skeletonTill applet skeleton
Till applet skeleton
 
Applet in java new
Applet in java newApplet in java new
Applet in java new
 
PROGRAMMING IN JAVA- unit 4-part I
PROGRAMMING IN JAVA- unit 4-part IPROGRAMMING IN JAVA- unit 4-part I
PROGRAMMING IN JAVA- unit 4-part I
 
Applets
AppletsApplets
Applets
 
Slide8appletv2 091028110313-phpapp01
Slide8appletv2 091028110313-phpapp01Slide8appletv2 091028110313-phpapp01
Slide8appletv2 091028110313-phpapp01
 
Applet intro
Applet introApplet intro
Applet intro
 
Appletjava
AppletjavaAppletjava
Appletjava
 
Java applet
Java appletJava applet
Java applet
 
Java applet-basics
Java applet-basicsJava applet-basics
Java applet-basics
 
Java applet-basics
Java applet-basicsJava applet-basics
Java applet-basics
 
Unit 7 Java
Unit 7 JavaUnit 7 Java
Unit 7 Java
 

Plus de Nuha Noor

Quiz app(j tabbed pane,jdialog,container,actionevent,jradiobutton,buttongroup...
Quiz app(j tabbed pane,jdialog,container,actionevent,jradiobutton,buttongroup...Quiz app(j tabbed pane,jdialog,container,actionevent,jradiobutton,buttongroup...
Quiz app(j tabbed pane,jdialog,container,actionevent,jradiobutton,buttongroup...Nuha Noor
 
Understanding layout managers
Understanding layout managersUnderstanding layout managers
Understanding layout managersNuha Noor
 
Polymorphism
PolymorphismPolymorphism
PolymorphismNuha Noor
 
Exceptionhandling
ExceptionhandlingExceptionhandling
ExceptionhandlingNuha Noor
 
Http request and http response
Http request and http responseHttp request and http response
Http request and http responseNuha Noor
 
Jsp elements
Jsp elementsJsp elements
Jsp elementsNuha Noor
 
Using cookies and sessions
Using cookies and sessionsUsing cookies and sessions
Using cookies and sessionsNuha Noor
 
Jdbc in servlets
Jdbc in servletsJdbc in servlets
Jdbc in servletsNuha Noor
 
Reading init param
Reading init paramReading init param
Reading init paramNuha Noor
 

Plus de Nuha Noor (11)

Quiz app(j tabbed pane,jdialog,container,actionevent,jradiobutton,buttongroup...
Quiz app(j tabbed pane,jdialog,container,actionevent,jradiobutton,buttongroup...Quiz app(j tabbed pane,jdialog,container,actionevent,jradiobutton,buttongroup...
Quiz app(j tabbed pane,jdialog,container,actionevent,jradiobutton,buttongroup...
 
Understanding layout managers
Understanding layout managersUnderstanding layout managers
Understanding layout managers
 
Events1
Events1Events1
Events1
 
Polymorphism
PolymorphismPolymorphism
Polymorphism
 
Packages
PackagesPackages
Packages
 
Exceptionhandling
ExceptionhandlingExceptionhandling
Exceptionhandling
 
Http request and http response
Http request and http responseHttp request and http response
Http request and http response
 
Jsp elements
Jsp elementsJsp elements
Jsp elements
 
Using cookies and sessions
Using cookies and sessionsUsing cookies and sessions
Using cookies and sessions
 
Jdbc in servlets
Jdbc in servletsJdbc in servlets
Jdbc in servlets
 
Reading init param
Reading init paramReading init param
Reading init param
 

Dernier

Design For Accessibility: Getting it right from the start
Design For Accessibility: Getting it right from the startDesign For Accessibility: Getting it right from the start
Design For Accessibility: Getting it right from the startQuintin Balsdon
 
Block diagram reduction techniques in control systems.ppt
Block diagram reduction techniques in control systems.pptBlock diagram reduction techniques in control systems.ppt
Block diagram reduction techniques in control systems.pptNANDHAKUMARA10
 
1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf
1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf
1_Introduction + EAM Vocabulary + how to navigate in EAM.pdfAldoGarca30
 
Moment Distribution Method For Btech Civil
Moment Distribution Method For Btech CivilMoment Distribution Method For Btech Civil
Moment Distribution Method For Btech CivilVinayVitekari
 
Computer Lecture 01.pptxIntroduction to Computers
Computer Lecture 01.pptxIntroduction to ComputersComputer Lecture 01.pptxIntroduction to Computers
Computer Lecture 01.pptxIntroduction to ComputersMairaAshraf6
 
Standard vs Custom Battery Packs - Decoding the Power Play
Standard vs Custom Battery Packs - Decoding the Power PlayStandard vs Custom Battery Packs - Decoding the Power Play
Standard vs Custom Battery Packs - Decoding the Power PlayEpec Engineered Technologies
 
Bhubaneswar🌹Call Girls Bhubaneswar ❤Komal 9777949614 💟 Full Trusted CALL GIRL...
Bhubaneswar🌹Call Girls Bhubaneswar ❤Komal 9777949614 💟 Full Trusted CALL GIRL...Bhubaneswar🌹Call Girls Bhubaneswar ❤Komal 9777949614 💟 Full Trusted CALL GIRL...
Bhubaneswar🌹Call Girls Bhubaneswar ❤Komal 9777949614 💟 Full Trusted CALL GIRL...Call Girls Mumbai
 
DC MACHINE-Motoring and generation, Armature circuit equation
DC MACHINE-Motoring and generation, Armature circuit equationDC MACHINE-Motoring and generation, Armature circuit equation
DC MACHINE-Motoring and generation, Armature circuit equationBhangaleSonal
 
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...Arindam Chakraborty, Ph.D., P.E. (CA, TX)
 
S1S2 B.Arch MGU - HOA1&2 Module 3 -Temple Architecture of Kerala.pptx
S1S2 B.Arch MGU - HOA1&2 Module 3 -Temple Architecture of Kerala.pptxS1S2 B.Arch MGU - HOA1&2 Module 3 -Temple Architecture of Kerala.pptx
S1S2 B.Arch MGU - HOA1&2 Module 3 -Temple Architecture of Kerala.pptxSCMS School of Architecture
 
A CASE STUDY ON CERAMIC INDUSTRY OF BANGLADESH.pptx
A CASE STUDY ON CERAMIC INDUSTRY OF BANGLADESH.pptxA CASE STUDY ON CERAMIC INDUSTRY OF BANGLADESH.pptx
A CASE STUDY ON CERAMIC INDUSTRY OF BANGLADESH.pptxmaisarahman1
 
Thermal Engineering Unit - I & II . ppt
Thermal Engineering  Unit - I & II . pptThermal Engineering  Unit - I & II . ppt
Thermal Engineering Unit - I & II . pptDineshKumar4165
 
Hostel management system project report..pdf
Hostel management system project report..pdfHostel management system project report..pdf
Hostel management system project report..pdfKamal Acharya
 
Thermal Engineering-R & A / C - unit - V
Thermal Engineering-R & A / C - unit - VThermal Engineering-R & A / C - unit - V
Thermal Engineering-R & A / C - unit - VDineshKumar4165
 
NO1 Top No1 Amil Baba In Azad Kashmir, Kashmir Black Magic Specialist Expert ...
NO1 Top No1 Amil Baba In Azad Kashmir, Kashmir Black Magic Specialist Expert ...NO1 Top No1 Amil Baba In Azad Kashmir, Kashmir Black Magic Specialist Expert ...
NO1 Top No1 Amil Baba In Azad Kashmir, Kashmir Black Magic Specialist Expert ...Amil baba
 
Orlando’s Arnold Palmer Hospital Layout Strategy-1.pptx
Orlando’s Arnold Palmer Hospital Layout Strategy-1.pptxOrlando’s Arnold Palmer Hospital Layout Strategy-1.pptx
Orlando’s Arnold Palmer Hospital Layout Strategy-1.pptxMuhammadAsimMuhammad6
 
Verification of thevenin's theorem for BEEE Lab (1).pptx
Verification of thevenin's theorem for BEEE Lab (1).pptxVerification of thevenin's theorem for BEEE Lab (1).pptx
Verification of thevenin's theorem for BEEE Lab (1).pptxchumtiyababu
 
AIRCANVAS[1].pdf mini project for btech students
AIRCANVAS[1].pdf mini project for btech studentsAIRCANVAS[1].pdf mini project for btech students
AIRCANVAS[1].pdf mini project for btech studentsvanyagupta248
 

Dernier (20)

Design For Accessibility: Getting it right from the start
Design For Accessibility: Getting it right from the startDesign For Accessibility: Getting it right from the start
Design For Accessibility: Getting it right from the start
 
Block diagram reduction techniques in control systems.ppt
Block diagram reduction techniques in control systems.pptBlock diagram reduction techniques in control systems.ppt
Block diagram reduction techniques in control systems.ppt
 
1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf
1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf
1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf
 
Moment Distribution Method For Btech Civil
Moment Distribution Method For Btech CivilMoment Distribution Method For Btech Civil
Moment Distribution Method For Btech Civil
 
Computer Lecture 01.pptxIntroduction to Computers
Computer Lecture 01.pptxIntroduction to ComputersComputer Lecture 01.pptxIntroduction to Computers
Computer Lecture 01.pptxIntroduction to Computers
 
Standard vs Custom Battery Packs - Decoding the Power Play
Standard vs Custom Battery Packs - Decoding the Power PlayStandard vs Custom Battery Packs - Decoding the Power Play
Standard vs Custom Battery Packs - Decoding the Power Play
 
Integrated Test Rig For HTFE-25 - Neometrix
Integrated Test Rig For HTFE-25 - NeometrixIntegrated Test Rig For HTFE-25 - Neometrix
Integrated Test Rig For HTFE-25 - Neometrix
 
Bhubaneswar🌹Call Girls Bhubaneswar ❤Komal 9777949614 💟 Full Trusted CALL GIRL...
Bhubaneswar🌹Call Girls Bhubaneswar ❤Komal 9777949614 💟 Full Trusted CALL GIRL...Bhubaneswar🌹Call Girls Bhubaneswar ❤Komal 9777949614 💟 Full Trusted CALL GIRL...
Bhubaneswar🌹Call Girls Bhubaneswar ❤Komal 9777949614 💟 Full Trusted CALL GIRL...
 
Call Girls in South Ex (delhi) call me [🔝9953056974🔝] escort service 24X7
Call Girls in South Ex (delhi) call me [🔝9953056974🔝] escort service 24X7Call Girls in South Ex (delhi) call me [🔝9953056974🔝] escort service 24X7
Call Girls in South Ex (delhi) call me [🔝9953056974🔝] escort service 24X7
 
DC MACHINE-Motoring and generation, Armature circuit equation
DC MACHINE-Motoring and generation, Armature circuit equationDC MACHINE-Motoring and generation, Armature circuit equation
DC MACHINE-Motoring and generation, Armature circuit equation
 
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...
 
S1S2 B.Arch MGU - HOA1&2 Module 3 -Temple Architecture of Kerala.pptx
S1S2 B.Arch MGU - HOA1&2 Module 3 -Temple Architecture of Kerala.pptxS1S2 B.Arch MGU - HOA1&2 Module 3 -Temple Architecture of Kerala.pptx
S1S2 B.Arch MGU - HOA1&2 Module 3 -Temple Architecture of Kerala.pptx
 
A CASE STUDY ON CERAMIC INDUSTRY OF BANGLADESH.pptx
A CASE STUDY ON CERAMIC INDUSTRY OF BANGLADESH.pptxA CASE STUDY ON CERAMIC INDUSTRY OF BANGLADESH.pptx
A CASE STUDY ON CERAMIC INDUSTRY OF BANGLADESH.pptx
 
Thermal Engineering Unit - I & II . ppt
Thermal Engineering  Unit - I & II . pptThermal Engineering  Unit - I & II . ppt
Thermal Engineering Unit - I & II . ppt
 
Hostel management system project report..pdf
Hostel management system project report..pdfHostel management system project report..pdf
Hostel management system project report..pdf
 
Thermal Engineering-R & A / C - unit - V
Thermal Engineering-R & A / C - unit - VThermal Engineering-R & A / C - unit - V
Thermal Engineering-R & A / C - unit - V
 
NO1 Top No1 Amil Baba In Azad Kashmir, Kashmir Black Magic Specialist Expert ...
NO1 Top No1 Amil Baba In Azad Kashmir, Kashmir Black Magic Specialist Expert ...NO1 Top No1 Amil Baba In Azad Kashmir, Kashmir Black Magic Specialist Expert ...
NO1 Top No1 Amil Baba In Azad Kashmir, Kashmir Black Magic Specialist Expert ...
 
Orlando’s Arnold Palmer Hospital Layout Strategy-1.pptx
Orlando’s Arnold Palmer Hospital Layout Strategy-1.pptxOrlando’s Arnold Palmer Hospital Layout Strategy-1.pptx
Orlando’s Arnold Palmer Hospital Layout Strategy-1.pptx
 
Verification of thevenin's theorem for BEEE Lab (1).pptx
Verification of thevenin's theorem for BEEE Lab (1).pptxVerification of thevenin's theorem for BEEE Lab (1).pptx
Verification of thevenin's theorem for BEEE Lab (1).pptx
 
AIRCANVAS[1].pdf mini project for btech students
AIRCANVAS[1].pdf mini project for btech studentsAIRCANVAS[1].pdf mini project for btech students
AIRCANVAS[1].pdf mini project for btech students
 

Applets

  • 2. APPLET • 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. • Advantage of Applet • There are many advantages of applet. They are as follows: • It works at client side so less response time. • Secured • It can be executed by browsers running under many platforms, including Linux, Windows, Mac Os etc. • Drawback of Applet • Plugin is required at client browser to execute applet.
  • 3. APPLET • An applet is a Java class that extends the java.applet.Applet class. • A main() method is not invoked on an applet, and an applet class will not define main(). • Applets are designed to be embedded within an HTML page. • When a user views an HTML page that contains an applet, the code for the applet is downloaded to the user's machine. • A JVM is required to view an applet. The JVM can be either a plug-in of the Web browser or a separate runtime environment. • The JVM on the user's machine creates an instance of the applet class and invokes various methods during the applet's lifetime. • Applets have strict security rules that are enforced by the Web browser. The security of an applet is often referred to as sandbox security, comparing the applet to a child playing in a sandbox with various rules that must be followed. • Other classes that the applet needs can be downloaded in a single Java Archive (JAR) file.
  • 4. Life Cycle of an Applet • Four methods in the Applet class gives you the framework on which you build any serious applet − • It is important to understand the order in which the various methods shown in the skeleton are called. • When an applet begins, the following methods are called, in this sequence: 1. init( ) 2. start( ) 3. paint( ) • When an applet is terminated, the following sequence of method calls takes place: 1. stop( ) 2. destroy( )
  • 5. 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() { // initialization } start() • 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 start() { • // start or resume execution • }
  • 6. paint() 1. The paint( ) method is called each time an AWT-based applet’s output must be redrawn. 2. The applet window may be minimized and then restored. paint( ) is also called when the applet begins execution. 3. Whatever the cause, whenever the applet must redraw its output, paint( ) is called. 4. The paint( ) method has one parameter of type Graphics. 5. This parameter will contain the graphics context, which describes the graphics environment in which the applet is running. 6. This context is used whenever output to the applet is required 7. public void paint(Graphics g) { 8. // redisplay contents of window 9. }
  • 7. stop() • The stop( ) method is called when a web browser leaves the HTML document containing the applet— when it goes to another page, for example. • When stop( ) is called, the applet is probably running. You should use stop( ) to suspend threads that don’t need to run when the applet is not visible. • You can restart them when start( ) is called if the user returns to the page. • public void stop() { • // suspends execution • } 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( ). • public void destroy() { • // perform shutdown activities • }
  • 8. Applet class • The Applet class is contained in the java.applet package. • There are two varieties of applets based on Applet. • The first are those based directly on the Applet class.These applets use the Abstract Window Toolkit (AWT) to provide the graphical user interface (or use no GUI at all). This style of applet has been available since Java was first created. • The second type of applets are those based on the Swing class JApplet, which inherits Applet. • Swing applets use the Swing classes to provide the GUI. • Swing offers a richer and often easier-to-use user interface than does the AWT. Thus, Swing-based applets are now the most popular. • However, traditional AWT-based applets are still used, especially when only a very simple user interface is required • AWT-based applets are subclasses of Applet. Applets are not stand-alone programs. Instead, they run within either a web browser or an applet viewer.
  • 9.
  • 10. deployment strategy • Before an applet can be used, a deployment strategy must be chosen • The second basic approach to deploying an applet is to specify the applet directly in an HTML file. • This is the original way that applets were launched when Java was created, and it is still used today—especially for simple applets. • Therefore, the APPLET tag is used in this book. (Be aware that the APPLET tag is currently deprecated by the HTML specification. The alternative is the OBJECT tag. You should check the JDK documentation in this regard for the latest recommendations.) • When an APPLET tag is encountered in the HTML file, the specified applet will be executed by a Java-enabled web browser. • The use of the APPLET tag offers a secondary advantage when developing applets because it enables you to easily view and test the applet. • To do so, simply include a comment at the head of your Java source code file that contains the APPLET tag. • This way, your code is documented with the necessary HTML statements needed by your applet, and you can test the compiled applet by starting the applet viewer with your Java source code file specified as the target. Here is an example of such a comment: /*<applet code="compute" width=500 height=200></applet>*/
  • 11. 3.A.Develop an applet in Java that displays a simple message. 3.b) Develop an applet in Java that receives an integer in one text field, and computes its factorial Value and returns it in another text field, when the button named “Compute” is clicked.
  • 12.
  • 13.
  • 14.
  • 15. 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) • Here, message is the string to be output beginning at x,y. In a Java window, the upper-left corner is location 0,0. • The drawString( ) method will not recognize newline characters. • If you want to start a line of text on another line, you must do so manually, specifying the precise X,Y location where you want the line to begin. • To set the background color of an applet’s window, use setBackground( ). • To set the foreground color (the color in which text is shown, for example), use setForeground( ). • These methods are defined by Component, and they have the following general forms: • void setBackground(Color newColor) • void setForeground(Color newColor) • Here, newColor specifies the new color.
  • 16. The class Color defines the constants shown here that can be used to specify colors: You can obtain the current settings for the background and foreground colors by calling getBackground( ) and getForeground( ), respectively. They are also defined by Component and are shown here: Color getBackground( ) Color getForeground( )
  • 17.
  • 18. Requesting Repainting • How can the applet itself cause its window to be updated when its information changes? • For example, if an applet is displaying a moving banner, what mechanism does the applet use to update the window each time this banner scrolls? • Remember, one of the fundamental architectural constraints imposed on an applet is that it must quickly return control to the run-time system. • It cannot create a loop inside paint( ) that repeatedly scrolls the banner, for example. • This would prevent control from passing back to the AWT. Given this constraint, it may seem that output to your applet’s window will be difficult at best. • Fortunately, this is not the case. Whenever your applet needs to update the information displayed in its window, it simply calls repaint( ). • The repaint( ) method is defined by the AWT. It causes the AWT run-time system to execute a call to your applet’s update( ) method, which, in its default implementation, calls paint( )
  • 19. • Thus, for another part of your applet to output to its window, simply store the output and then call repaint( ). • The AWT will then execute a call to paint( ), which can display the stored information. • For example, if part of your applet needs to output a string, it can store this string in a String variable and then call repaint( ). • Inside paint( ), you will output the string using drawString( ). • The repaint( ) method has four forms. Let’s look at each one, in turn. • The simplest version of 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) • Here, the coordinates of the upper-left corner of the region are specified by left and top, and the width and height of the region are passed in width and height. • These dimensions are specified in pixels. You save time by specifying a region to repaint. Window updates are costly in terms of time.
  • 20. • If you need to update only a small portion of the window, it is more efficient to repaint only that region. • Calling repaint( ) is essentially a request that your applet be repainted sometime soon. However, if your system is slow or busy, update( ) might not be called immediately. • Multiple requests for repainting that occur within a short time can be collapsed by the AWT in a manner such that update( ) is only called sporadically. • This can be a problem in many situations, including animation, in which a consistent update time is necessary. • One solution to this problem is to use the following forms of repaint( ): • void repaint(long maxDelay) • void repaint(long maxDelay, int x, int y, int width, int height) • Here, maxDelay specifies the maximum number of milliseconds that can elapse before update( ) is called. Beware, though. If the time elapses before update( ) can be called, it isn’t called. There’s no return value or exception thrown, so you must be careful.
  • 21.
  • 22.
  • 23. Using the Status Window • In addition to displaying information in its window, an applet can also output a message to the status window of the browser or applet viewer on which it is running. • To do so, call showStatus( ) with the string that you want displayed. The status window is a good place to give the user feedback about what is occurring in the applet, suggest options, or possibly report some types of errors. • The status window also makes an excellent debugging aid, because it gives you an easy way to output information about your applet. The following applet demonstrates showStatus( ):
  • 24. The HTML APPLET Tag • The syntax for a fuller form of the APPLET tag is shown here. • Bracketed items are optional. • < APPLET [CODEBASE = codebaseURL] • CODE = appletFile • [ALT = alternateText] • [NAME = appletInstanceName] • WIDTH = pixels • HEIGHT = pixels • [ALIGN = alignment ] • [VSPACE = pixels] • [HSPACE = pixels] > • [< PARAM NAME = AttributeName VALUE = AttributeValue>] • [< PARAM NAME = AttributeName2 VALUE = AttributeValue>] • . . . • [HTML Displayed in the absence of Java] • < /APPLET>
  • 25. • Let’s take a look at each part now. • CODEBASE • CODEBASE is an optional attribute that specifies the base URL of the applet code, which is the directory that will be searched for the applet’s executable class file (specified by the CODE tag). The HTML document’s URL directory is used as the CODEBASE if this attribute is not specified. • CODE • CODE is a required attribute that gives the name of the file containing your applet’s compiled .class file. This file is relative to the code base URL of the applet, which is the directory that the HTML file was in or the directory indicated by CODEBASE if set. • ALT • The ALT tag is an optional attribute used to specify a short text message that should be displayed if the browser recognizes the APPLET tag but can’t currently run Java applets. This is distinct from the alternate HTML you provide for browsers that don’t support applets. • NAME • NAME is an optional attribute used to specify a name for the applet instance. Applets must be named in order for other applets on the same page to find them by name and communicate with them. To obtain an applet by name, use getApplet( ), which is defined by the AppletContext interface.
  • 26. • WIDTH and HEIGHT • WIDTH and HEIGHT are required attributes that give the size (in pixels) of the applet display area. • ALIGN • ALIGN is an optional attribute that specifies the alignment of the applet. This attribute is treated the same as the HTML IMG tag with these possible values: LEFT, RIGHT, TOP, BOTTOM, MIDDLE, BASELINE, TEXTTOP, ABSMIDDLE, and ABSBOTTOM. • VSPACE and HSPACE These attributes are optional. • VSPACE specifies the space, in pixels, above and below the applet. • HSPACE specifies the space, in pixels, on each side of the applet. They’re treated the same as the IMG tag’s VSPACE and HSPACE attributes. • PARAM NAME and VALUE • The PARAM tag allows you to specify applet-specific arguments. Applets access their attributes with the getParameter( ) method. • Other valid APPLET attributes include ARCHIVE, which lets you specify one or more archive files, and OBJECT, which specifies a saved version of the applet. In general, an APPLET tag should include only a CODE or an OBJECT attribute, but not both.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31. • The AudioClip Interface • The AudioClip interface defines these methods: • play( ) (play a clip from the beginning), stop( ) (stop playing the clip), and loop( ) (play the loop continuously). After you have loaded an audio clip using getAudioClip( ), you can use these methods to play it. • The AppletStub Interface • The AppletStub interface provides the means by which an applet and the browser (or applet viewer) communicate. Your code will not typically implement this interface. • Outputting to the Console • Although output to an applet’s window must be accomplished through GUI- based methods, such as drawString( ), it is still possible to use console output in your applet—especially for debugging purposes. In an applet, when you call a method such as System.out.println( ), the output is not sent to your applet’s window.