Anda di halaman 1dari 16

Java Applets

BY:

NEERAJ BHUSARI SACHIN SHUKLA

Content

Introduction of Applet
Applet life cycle Uses of applet

Limitations of applets
Advantages of applet Disadvantages of applet

Applet

HTML communicates with the user on internet ,it can use a special java program ,called APPLET to communicate and respond to the user. An applet is a Panel that allows interaction with a Java program A applet is typically embedded in a Web page and can be run from a browser

For security reasons, applets run in a sandbox: they have no access to the clients file system

Applet Life Cycle


Public void init() Public void start()

Public void stop()


Public void destroy()

Public void init()


This method is the first method to be called by the browser and it executes only once. Its an ideal place to initialize any variables, creating components and creating threads. Its the best place to define the GUI Components (buttons, text fields, scrollbars etc.), lay them out, and add listeners to them. Almost every applet you ever write will have an init( ) method When this method execution is completed browser looks for the next method start()

Public void start()


This method is called after init() method and each time the applet is revisited by user. Not always needed. Whenever it gets Called each time the page is loaded and restarted. Used mostly in conjunction with stop(). start() and stop( ) are used when the Applet is doing time-consuming calculations that you dont want to continue when the page is not in front.

Public void stop()


This method is called by the browser when the applet is to be stopped. If the user minimizes the web pages ,then this method is called and when the user again comes back to this page ,then start() method is called. This way start() and stop() methods can be called repeatedly. Not always needed. We may use stop( ) if the applet is doing heavy computation that you dont want to continue when the browser is on some other page. Used mostly in conjunction with start().

Called just before destroy( ).

Public void destroy()


This method is called when the applet is begin terminated from memory . The stop() method will always be called before destroy().

The code related to releasing the memory allocated to the applet and stopping any running threads should be written in this method.
Called after stop( )

Use to explicitly release system resources (like threads)


System resources are usually released automatically

Uses of applet

Applet are used for multiple purpose. Some of the use of applets are:
Applet

are used on internet for creating dynamic web pages .There are two types of web pages: Static Dynamic

Static:

static web pages provide some information to the user but user cannot interact with the web page other then viewing the information.

Dynamic: Dynamic web pages interact with the user at

the runtime.

Another use of applet is for creating animation and games where the image can be displayed or moved giving a visual impression that they are alive.

Limitations of applets

Applets are not permitted to use any system resources like file system as they are untrusted and can inject virus into the system Applets cannot read from ,or write to hard disk files. The JRE throws Security Exception if the applet violates the browser restrictions

Advantages of applet

Deployment of applets is easy in a Web browser and does not require any installation procedure in real time programming Writing and displaying (just opening in a browser) graphics and animations is easier than applications In GUI development, constructor, size of frame, window closing code etc. are not required (but are required in applications) Can be accessed from various platforms and various java enabled web browsers.

Can provide dynamic, graphics capabilities and visualizations

Disadvantages of applet

All java-created pop-up windows carry a warning message Stability depends on stability of the clients web server

Performance directly depend on clients machine


In real-time environment, the byte code of applet is to be downloaded from the server to the client machine Applets are treated as untrusted and for this reason they are not allowed, as a security measure, to access any system resources like file system etc. available on the client system

Example
//A simple program of applet
import java.awt.*; //import Abstract windows toolkit(awt) import java.applet.*; //import applet package Public class Myapp extends Applets

{ //set the background colour for the frame Public void init()

{
setBackground(Color.yellow); } //display message in applet window

Public void paint(Graphics g)


{ g.drawstring(HELLO Applets!!,50,100); } } //drawstring is member of graphics class

Questions?

Thank you!!!

Anda mungkin juga menyukai