0% found this document useful (0 votes)
5 views33 pages

Applet Programming

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
5 views33 pages

Applet Programming

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

Applet Programming

• small Java programs


• used in Internet computing
• can be transported over the Internet from one computer to another
• run using the Applet Viewer or any Web browser that supports Java.
• can perform
➢arithmetic operations
➢display graphics
➢play sounds
➢accept user input
➢create animation
➢play interactive games
Local and Remote Applets
• We can embed applets into Web pages in two ways.
1. write our own applets and embed them into Web pages.
2. download an applet from a remote computer system and then
embed it into a Web page.

• local applet - developed locally and stored in a local system


• remote applet - developed by someone else and stored on a remote
computer connected to the Internet.
Locate and Load a remote applet
• applet's address on the Web.
• This address is known as Uniform Resource Locator (URL)
• specified in the applet's HTML document as the value of the
CODEBASE attribute

CODEBASE = https:// www.netserve.com / applets


How Applets are different from Applications
• do not use the main() method for initiating the execution of the code.
• cannot be run independently
• cannot read from or write to the files in the local computer.
• cannot communicate with other servers on the network.
• cannot run any program from the local computer.
• restricted from using libraries from other languages such as C or C++.
Preparing to Write Applets
• When to use applets
1. When we need something dynamic to be included in the display of
a Web page.
e.g. Games embedded in web pages.

2. When we require some "flash" outputs.


e.g. Animations and special effects.

3. When we want to create a program and make it available on the


Internet for us by others on their computers.
e.g. Small utility programs (calculators, file converters)
Steps to develop and test Applets
1. Building an applet code (.java file)
2. Creating an executable applet (.class file)
3. Designing a Web page using HTML tags
4. Preparing <APPLET> tag
5. Incorporating <APPLET> tag into the Web page
6. Creating HTML file
7. Testing the applet code
Building an Applet code
• 2 classes
➢Applet
init() - initializes the Applet and sets its default values
start() - starts the execution of the Applet
paint() - drawing tasks

➢Graphics
public void paint (Graphics g)
Chain of classes inherited by Applets

java.lang.Object

java.awt.Component

java.awt.Container

java.awt.Panel

java.applet.Applet
Applet Life Cycle
• Born on initialization state
• Running state
• Idle state
• Dead or destroyed state
Initialization state
• Enters initialization state when
first loaded
• init() method
• Occurs only once

➢Create objects needed by applet


➢Set up initial values
➢Load images or fonts
➢Set up colors
Running state
• start() method
• Occurs automatically after
initialization or
• If applet is already in
“stopped”(idle) state
• May be called more than once
Idle or Stopped state
• Stopped from running
• Stopping occurs automatically
when we leave the page
containing the currently running
applet.
• or stop() method
Dead state
• When removed from memory
• Occurs automatically by invoking
destroy() method when quitting
the browser.
• Occurs only once
Display state
• perform some output operations
on the screen.
• paint() method
• We must override this method if
we want anything to be
displayed on the screen.
Creating an Executable Applet
• Executable applet is the .class file of the applet
• Java compiler is used to compile the applet
• The steps required for compiling the helloJava applet.
1. Move to the directory containing the source code and type the following
command:
javac HelloJava.java
2. The compiled output file called helloJava.class is placed in the same directory
as the source.
3. If any error message is received, then we must check for errors, correct them
and compile the applet again.
Designing a web page
Applet Tag
• The <APPLET ... > tag supplies the name of the applet to be loaded and tells the
browser how much space the applet requires.

<APPLET
CODE = helloJava.class
WIDTH = 400
HEIGHT = 200 >
</APPLET >

• <APPLET> tag should contain 3 things


1. Name of the applet
2. Width of the applet (in pixels)
3. Height of the applet (in pixels)
Adding applet to HTML file
Running the applet
• To run an applet, we require one
of the following tools:
1. Java-enabled Web browser
(such as HotJava or Netscape)

2. Java appletviewer
Passing Parameters to Applets
• We can supply user-defined parameters to an applet using <PARAM ... > tags.
• Each <PARAM ... > tag has
➢a name attribute such as color, and
➢a value attribute such as red.
<APPLET>
<PARAM NAME= color VALUE= "red"> <PARAM NAME = text VALUE = "I love Java">
</APPLET>

• To set up and handle parameters, we need to do two things:


1. Include appropriate <PARAM ... > tags in the HTML document.
2. Provide Code in the applet to parse these parameters.
Aligning the Display
• using the ALIGN attribute.
• Has 9 attributes
LEFT RIGHT TOP
TEXT TOP MIDDLE ABSMIDDLE
BASELINE BOTTOM ABSBOTTOM
Displaying Numerical Values
• first convert numerical values into strings
• use the drawString() method of Graphics class.
Getting Input from the User
• Applets treat inputs as text strings.
1. Create an area of the screen in which user can type and edit input
items (which may be any data type).
• Using TextField class of the applet package.

2. Retrieve the items from the fields for display of calculations, if any.
• The text fields contain items in string form.
• Converted to the right form, before using in any computations.
• The results are then converted back to strings for display.
Run the applet
1. Type and save the program (.java file)
2. Compile the applet (.class file)
3. Write a HTML document (.html file)

4. Use the appletviewer to display the results

You might also like