Applets: Interactive Feature To Web Application
Applets: Interactive Feature To Web Application
Applet
Small programs that runs inside a we browser it has limited access to the resource on the client Provides security It transforms passive web pages into programs that interact with web users
Applet
Applet does not have a main() function Web browser responsible for starting the JVM life cycle of an Applet. Simple program HTML
Applet
Applets are java program that are integrated into web page The applets output is displayed within a subset of browsers display area
Browser window
Applet
applet class should be declared as public after compilation the class file is included in the HTML tag using APPLET tag paint is called each time that the applet must redisplay its output
Applet class
java.lang.Object
java.awtComponent
java.awt.Container
java.awt.Panel
java.applet.Applet
COMPONENT
CONTAINER
WINDOW
PANEL
FRAME
APPLET
Component
Top of the AWT hierarchy Represents elements - included in GUI Abstract class Components are elementary UI entities such as buttons, labels, text fields, and check boxes.
Container
Subclass of Component a component that may contain other components Responsible for laying out any components Frames, Dialogs, Windows and Panels a component must be kept in a container.
java.applet.*;
one of the smallest package It consist of a single class Applet and 3 interfaces
URL getCodeBase() URL getDocumentBase() void resize(int width, int height) String getParameter(String paramname) AppletContext getAppletContext() String getAppletInfo()
Returns the URL associates with the invoking applet Returns the URL of the HTML document that invokes the applet Resize the applet Returns the parameter associated with the paramname Returns the context associated with the applet Returns the string that describes the applet
public void init() public void start() public void stop() public void destroy() String getParameter(String pname) Image getImage(URL url); Returns the value of parameter pname Returns an Image object for the image at url
Applet architecture
is a window based event driven program resembles a set of interrupt service routine an applet waits until an event occurs.
An applet skeleton
it consist of methods
init()
start() stop() destroy() paint()
init()
is the first method to be called to initialize variables called only once during the runtime of our applet
start()
called after init() also called to restart an applet after it has been stopped
stop()
is called when a we browser leaves the HTML page can use to suspend threads that dont need to run when the applet is not visible
destroy()
is called when the environment determines that our applet needs to be removed completely from memory free up all resources stop() method is always called before destroy
Graphics
Encapsulate a set of methods that can perform graphics output The Graphics class is the abstract base class for all graphics contexts Allows to draw lines oval, characters etc drawLine() fillRect() drawImage()
Graphics
public abstract class Graphics extends Object A Graphics object encapsulates state information needed for the basic rendering operations Includes
The
Component object on which to draw. The current color. The current font
Graphics
Color getColor() Font getFont() FontMetrics getFontMetrics() void setColor(Color c) void setFont(Font font)
Using Colors
Color(int red, int green, int blue); Color(int rgb); Several constants blue, black, red, green etc Program1 Program2
Font
Determines the size and appearance of characters in strings( java.awt.Font) Font (String name, int style, int ps);
Serif,
setFont(Font f) of Graphics
PROGRAM
Applet Dimension
Describes how to dynamically determine the dimensions of an applet If resized position at its center Dimension getSize()
dim.width
, dim.height
<APPLET [CODEBASE=codebaseURL] CODE=appletFile [ALT=alternateText] [NAME=appletInstanceName] WIDTH=pixels HEIGHT=pixels [ALIGN=alignment] [VSPACE=pixels HSPACE=pixels] > [<PARAM NAME=attributeName1 VALUE=attributevalue] [<PARAM NAME=attributeName2 VALUE=attributevalue] </APPLET>
Applet class
paint()
is
called each time our applets output must be redrawn is called by the JVM implicitly in two circumstances
one
when the first time frame is created and displayed and the other when the frame is resized
one
parameter Graphics graphics context which describes the graphics environment in which the applet is running
Requesting Repainting
Whenever an applet needs to update the information displayed in its window it simply calls repaint() It is defined in AWT to explicitly call the paint method in your applet Their are 4 versions of this method void repaint() void repaint(int x,int y, int width, int height)
repaint() ==> update() ==> paint() Event handlers that need to modify the screen according to input events, usually store the state information in instance variables and call repaint().
AppletContext interface
Defines method that allow to interact with the environment This context is provided by web browser means to control the browser environment where the applet is running
the browser to retrieve and display the web page identified by url str specify where the web page is displayed web frame -> _self, _parent, _blank
URL getDocumentBase() gives the Url from which the HTML file is loaded URL getCodeBase() gives the url from from which the applet class file was loaded program
AudioClip interface
use the AudioClip interface and instantiate an AudioClip object with it invoke the getAudioClip() method of the Applet class play() play a clip from the beginning stop() stop playing a clip loop() play the loop continuously Load audio clip using getAudioClip() program
Java Sound supports the following audio file formats: AIFF, AU and WAV. It also supports the following MIDI based song file formats: SMF type 0 (Standard MIDI File, aka .mid files), SMF type 1 and RMF. The Java Sound engine can render 8 or 16 bit audio data, in mono or stereo, with sample rates from 8KHz to 48KHz, that might be found in streaming audio or any of the supported file formats.