JAVA
UNIT 3
APPLET PROGRAMMING
► Applets are small Java programs that are primarily used in Internet
computing. They can be transported over the Internet from one
computer to another and run using the Applet Viewer or any Web
browser that supports Java.
► An applet, like any application program, can perform arithmetic
operations, display graphics, play sounds, accept user input, create
animation, and play interactive games.
APPLET PROGRAMMING
Local and Remote Applets
An applet developed locally and stored in a local system is called a local applet.
local applet does not require the Internet connection. It simply searches the
directories in the local system and locates and loads the specified applet.
A remote applet is that which is developed by someone else and stored on a
remote computer connected to a Internet. In order to locate and load remote
applet, we must know the applet’s address on the Web. This address is known as
Uniform Resource Locator(URL).
CODEBASE = http://www.netserve.com/applets
APPLET PROGRAMMING
Difference Between Applets and Applications
APPLET PROGRAMMING
Preparing Applets
1. When we need something dynamic to be included in the display of the Web
page.
2. When we require some ”flash” outputs. For example, applets that produce
sounds, animations or some special effects would be useful when displaying
certain pages.
3. When we want to create a program and make it available on the Internet for us
by others on their computers.
APPLET PROGRAMMING
The steps involved in developing and testing in applet are:
APPLET PROGRAMMING
Building Applet Code
The Applet class which is contained in the java.applet package provides methods
such as init( ), start( ) and paint( ).
The paint( ) method of the Applet class, when it is called, actually displays the result
of the applet code on the screen. The output may be text, graphics, or sound. The
paint( ) method, which requires a Graphics object as an argument, is defined as
follows:
APPLET PROGRAMMING
General format of Applet code
The appletname is the main class for the applet.
When the applet is loaded, Java creates an
instance of this class, and then a series of Applet
class methods are called on that instance to
execute the code.
APPLET PROGRAMMING
APPLET LIFE CYCLE
Applet Life Cycle
When an applet is loaded it undergoes a series of changes in its state. The applet
state include:
1. Born or Initialization State
2. Running State
3. Idle State
4. Dead or Destroyed State
APPLET LIFE CYCLE
APPLET LIFE CYCLE
Initialization State
Applet enters the initialization state when it is first loaded. This is achieved by
calling the init( ) method of Applet Class. Theinitialization occurs only once in the
applet’s life cycle.
General form
APPLET LIFE CYCLE
Running State
Appletenterstherunningstatewhenthesystemcallsthestart()methodofApplet class.
This occurs automatically after the applet is initialized. unlike init( ) method, the
start( ) method may be called more than once.
General form:
APPLET LIFE CYCLE
Idle or Stopped State
An applet becomes idle when it is stopped from running. Stopping occurs
automatically when we leave the page containing the currently running applet. We
can do by calling the stop( ) method explicitly.
General form:
APPLET LIFE CYCLE
Dead State
An applet is said to be dead when it is removed from memory. This occurs
automatically by invoking the destroy( ) method when we quit the browser.
General form:
APPLET LIFE CYCLE
► Display State
Applet moves to the display state whenever it has to perform some output
operations on the screen. This happens immediately after the applet enters into the
running state. The paint( ) method is called to accomplish this task.
General form:
APPLET TAG
APPLET TAG
This HTML code tells the browser to
load the compiled Java applet
Hello.class, which is in the same
directory as the HTML file. And also
specifies the display area for the
applet output as 400 pixels width and
200 pixels height
ADDING APPLET TO HTMLFIL
RUNNING THE APPLET
► To run an applet we require one of the following tools:
► Java-enabled Web browser (such as HotJava or Netscape).
► Java appletviewer
PASSING PARAMETERS TO
APPLET
► User-defined parameters can be given to an applet using PARAM... tags. Each
PARAM... Tag hasa NAME attribute and a VALUE attribute. Insidethe applet
code, the applet can refer to that parameter by name to find its value
PASSING PARAMETERS TO
APPLET
To set up and handle parameters,
► Include appropriate PARAM... tags in the HTML document.
► Provide Code in the applet to parse these parameters
Parameters are passed on an applet when it is loaded. This is done using the
getParameter( ) method, which takes one string argument representing the name
of the parameter and returns a string containing the value of that parameter
PASSING PARAMETERS TO
APPLET – Ex:
PASSING PARAMETERS TO
APPLET – Summary
GETTING INPUT FROM THE USER
► Applets work in a graphical environment. Therefore, applets treat inputs as text
strings.
► Get input by using the TextField class of the applet package. Once text fields are
created for receiving input, type the values in the fields and edit them.
► Next step is to retrieve the items from the fields for display of calculations, if
any. The text fields contain items in string form.
► They need to be converted to the right form, before they are used in any
computations. The results are then converted back to strings for display.
GETTING INPUT FROM THE USER – Ex:
GETTING INPUT FORM THE USER
EVENT HANDLING
1. ActionEvent is triggered whenever a user interface element is activated,
such as selection of a menu item.
2. ItemEvent is triggered at the selection or deselection of an itemized or list
element, such as check box.
3. TextEvent is triggered when a text field is modified.
4. WindowEvent is triggered whenever a window-related operation is
performed, such as closing or activating a window.
5. KeyEvent is triggered whenever a key is pressed on the keyboard.
EVENT HANDLING
Each event is associated with an event source and an event handler.
Event source refers to the object that is responsible for generating an
event whereas event listener is the object that is referred by event
source at the time of occurrence of an event.
Event Sources
It is not necessary that a source generates only one type of event:
it can generate events of multiple types.
public void add <type> Listener(< type > Listener EveList)
EVENT HANDLING
EVENT HANDLING
EVENT HANDLING
I/O STREAMS, FILE STREAMS,
STRINGS
REFER PDF CHAPTER 7,8