Sie sind auf Seite 1von 18

Topic covered by:Ayush Thakur (1201101264) Shivanshu Bansal (1201101267)

Java applets are java programs,which are launched from an HTML page and run in a web browser. When a Web page with an applet is downloaded using a java enabled browser,the browser downloads that applet from Web server and executes it on local system.There is no limit to size and complexity of a java applet.Java applets are more powerful than java applications.However,with the Internet,where communication speed is limited and download times are long,most java applets are necessarily small.

It involves the following five stages:1. Initialization 2. Starting 3. Stopping 4. Destroying 5. Painting

Initialization occurs when the applet is first loaded(or reloaded).The initialisation of an applet includes reading and parsing any parameters of an applet,creating any helper objects it needs,setting up an initial state or loading images and fonts.To control behaviour of an applet,override the init() method in the APPLET CLASS. Syntax: public void init() { // original signature overriden. } }

After an applet is initialized,it is started.Starting is different from initialization because it can happen many times during an applets lifetime,whereas initialization can happen only once. Starting can also occur if the applet was previously stopped. Syntax:Public void start { //original signature overriden }

Stopping and starting go hand in hand.Stopping occurs when the reader leaves the page that contains a currently running applet or by calling the stop method.By default,if the reader leaves the page any thread the applet had started will continue running.By iverriding the stop method,one can suspend the execution of these threads and then restart them if the applet is viewed again. Syntax:Public void stop() { //original signature overriden }

Destroying enables the applet to perform a cleanup job just before it is destroyed or the browser exists.Generally,the destroy() method is overriden if there are specific objects that need to be destroyed. Syntax:Public void destroy() { //original signature is overriden }

Painting is the way an applet actually draws something on the screen,be it text,a line,a colored background or an image.Painting may occur a number of times during an applets lifecycle.The paint) method gets executed every time the applet is minimized or maximized or when the applet is initialized and the browser is placed behind another window on the screen and then brought forward again. Syntax:Public void paint(Graphics grph) { //original signature overriden }

To create a java applet,a SUBCLASS of the APPLET CLASS needs to be creted.The applet class which is the part of java.applet package, provides much of the behaviour the applet needs to work inside the java enabled browser.Applets also take strong advantage of AWT,which provides behaviour fo creating GUI based applets and applications.

import java.applet.Applet; import java.awt.Graphics; import java.awt.Color; /* <html> <head><title> Applet Test </title></head> <body> <applet code="AppletTest.class" width=300 height=200> <param name="name" value="Sharanam Shah"> </applet> </body> </html> */

public class AppletTest extends Applet { String param=""; public void init( ) { // The parameter name is the name of the parameter and the value passed is present in the param variable param = this.getParameter("name"); } public void paint(Graphics grph) { grph.setColor(Color.red); grph.drawString("Welcome " + param + " !",80,80); } }

Java can b used to create two types of programs: 1. Applications 2. Applets

An application is a program that runs on your computer, under the operating system of that computer whereas, an applet is an application designed to be transmitted over the internet and executed by a java compatible web browser. An applet is actually a tiny java program, dynamically downloaded across the network just like an image, audio file or video play. The important difference is that an applet is an intelligent program(a program that can react to user inputs and dynamically change)

import java.awt.Graphics; import java.awt.Image; import java.applet.Applet; /*<html> <head> <title> Applet Image Test </title> </head> <body> <applet code="SizeImage.class" width=300 height=200></applet> </body> </html> */

public class SizeImage extends Applet { Image img;


}

public void init() { img = getImage(getCodeBase(), abc.jpg"); } public void paint(Graphics grph) { grph.drawImage(img,10,20,this); }

File name: abc.jpg

Das könnte Ihnen auch gefallen