Sie sind auf Seite 1von 48

Applets

visit www.muenggg.blogspot.com for more study material.

Introduction : Applets
A special kind of Java program that is designed to be transmitted over the Internet & automatically executed by a Java-compatible Web browser.
Downloaded on demand, just like an image, sound file, or video clip.
visit www.muenggg.blogspot.com for more study material.

Introduction : Applets
An intelligent program, not just an animation or media file. Can react to user input and dynamically changenot just run the same animation or sound over and over.

visit www.muenggg.blogspot.com for more study material.

Applets & Applications


Applets do not use the main() for initiating the execution of the code. Applets, when loaded automatically call certain methods of the Applet class to start and execute the applet code. Unlike stand alone applications , applets cannot run independently. Run inside the Web page using a special feature known as HTML tag.
visit www.muenggg.blogspot.com for more study material.

Applets & Applications


Applets cannot read from or write to files in the local computer. Applets cannot run any program from the local computer. Applets are restricted from using libraries from other languages such as C or C++.

visit www.muenggg.blogspot.com for more study material.

Example
import java.awt.*; import java.applet.*; public class SimpleApplet extends Applet { public void paint(Graphics g) { g.drawString("A Simple Applet", 20, 20); } }
visit www.muenggg.blogspot.com for more study material.

Explanation
Two import statements. The first imports the Abstract Window Toolkit (AWT) classes. Applets interact with the user through the AWT, not through the console-based I/O classes. The AWT contains support for a windowbased, graphical interface.
visit www.muenggg.blogspot.com for more study material.

Explanation
Next import statement imports the package. This package contains the class Applet. Every applet that you create must be a subclass of Applet.
visit www.muenggg.blogspot.com for more study material.

applet

Explanation
declares the class SimpleApplet. This class must be declared as public because it will be accessed by outside code. Inside SimpleApplet, paint( ) is declared.

visit www.muenggg.blogspot.com for more study material.

paint()
paint () is defined by the AWT Component class (which is a superclass of Applet) and must be overridden by the applet.

paint( ) is called each time the applet must redisplay its output. paint( ) is also called when the applet begins execution.
visit www.muenggg.blogspot.com for more study material.

paint()
Whenever the applet redraws its output, paint( ) is called. paint( ) method has one parameter of type Graphics. This parameter will contain the graphics context, which describes the graphics environment in which the applet is running.

visit www.muenggg.blogspot.com for more study material.

paint()
Inside paint( ), there is a call to drawString( ), which is a member of the Graphics class. This method outputs a string beginning at the specified X,Y location. Syntax: void drawString(String message, int x, int y)

visit www.muenggg.blogspot.com for more study material.

Running an applet
Two ways : inside a browser appletviewer : a special development tool provided with standard Java JDK that displays applets.
visit www.muenggg.blogspot.com for more study material.

Execute an applet
A short HTML text file that contains the appropriate APPLET tag. <APPLET code="SimpleApplet" width=200 height=60> </ APPLET > width & height statements specify the dimensions of the display area used by the applet.
visit www.muenggg.blogspot.com for more study material.

Applet tag
supplies the name of the applet to be loaded How much space an applet requires. <APPLET CODE = WIDTH = HEIGHT = </APPLET>
visit www.muenggg.blogspot.com for more study material.

Alternative way
import java.awt.*; import java.applet.*; /* <APPLET code="SimpleApplet" width=200 height=60> </ APPLET > */ public class SimpleApplet extends Applet { public void paint(Graphics g) { g.drawString("Java makes applets easy.", 20, 20); } }
visit www.muenggg.blogspot.com for more study material.

Alternative way
Execute the applet by passing the name of its source file to appletviewer. For example: appletviewer SimpleApplet.java

visit www.muenggg.blogspot.com for more study material.

Applet life cycle


Every Java applet inherits a set of default behaviors from the Applet class. Applet states Born or initialization state Running state Idle state Dead or destroyed state
visit www.muenggg.blogspot.com for more study material.

initialization state
Enters this state when first loaded achieved by calling init() of Applet class. Only once in the applet life cycle. Can do the following create objects needed by the applet set up initial values load images or fonts set up colors
visit www.muenggg.blogspot.com for more study material.

initialization state
public void init() {

visit www.muenggg.blogspot.com for more study material.

Running state
Enters this state when the system calls the start() of Applet class. automatically after the applet is initialized. can be called more than once. public void start() { }
visit www.muenggg.blogspot.com for more study material.

Idle or stopped state


becomes idle when it is stopped from running. occurs automatically when we leave the page containing the currently running applet. call the stop() explicitly. public void stop() { }
visit www.muenggg.blogspot.com for more study material.

Dead or destroyed state


is said to be dead when it is removed from memory. invoke the destroy() when we quit the browser. Only once in the life cycle of the applet. public void destroy() { }

visit www.muenggg.blogspot.com for more study material.

Display state
moves to display state when it has to perform some output operations on the screen. happens immediately after the applet enters the running state. paint() is called to accomplish the task. Default version of paint() does nothing Override the paint() if anything is to be displayed on the screen.
visit www.muenggg.blogspot.com for more study material.

Applet : state transition diagram


Initialization Born Load applet start() Display Running paint() start() Destroyed Dead Idle Stopped stop()

end destroy()

visit www.muenggg.blogspot.com for more study material.

MU : Creating an executable Applet


Executable applet is the .class file obtained by compiling source code of an applet. Process is same as compiling an application Example : Consider an applet HelloJava.java

visit www.muenggg.blogspot.com for more study material.

MU : Creating an executable Applet


Steps : (a) Move to the directory containing the source code and type javac HelloJava.java

(b) Compiled output file is HelloJava.class is in the same directory as the source file.
(c) Check for errors if any , correct them and compile again
visit www.muenggg.blogspot.com for more study material.

MU :Designing a Web page which references an Applet


Web page is made up of text and HTML tags that can be interpreted by a Web browser or a appletviewer. Can use any ASCII text editor. Stored with extension .html HTML files should be stored in the same directory as compiled code of applets.

visit www.muenggg.blogspot.com for more study material.

MU :Designing a Web page which references an Applet


Web page is marked by an opening HTML tag <HTML> and a closing HTML tag </HTML> 3 major sections Comment section (optional) Head section (optional) Body section

visit www.muenggg.blogspot.com for more study material.

Comment section
Optional section can be located anywhere in the Web page. Contains comments about the Web page. begins with <! And ends with a> Web browsers ignore any text between them.

visit www.muenggg.blogspot.com for more study material.

Head section
optional starts with <HEAD> and ends with </HEAD> contains title for the web page. Example: <HEAD> <TITLE> Welcome to Java applets </TITLE> </HEAD> Text enclosed in TITLE tags will appear in the title bar of the Web browser.
visit www.muenggg.blogspot.com for more study material.

Body section
contains entire information about the Web page and its behavior. Example <BODY> <CENTER> <H1> Welocme to Applets </H1> </CENTER> <BR> </BODY>
visit www.muenggg.blogspot.com for more study material.

MU :Adding Applets to HTML File


<HTML> <! > <HEAD> <TITLE> Welcome to Java applets </TITLE> </HEAD>
visit www.muenggg.blogspot.com for more study material.

MU :Adding Applets to HTML File


<BODY> <CENTER> <H1> Welcome to world of Applets </H1> </CENTER> <BR> <CENTER>

visit www.muenggg.blogspot.com for more study material.

Running the applet


Three files in current directory .java files .class file .html file

Use either Java enabled Web browser or appletviewer utility to run an applet
visit www.muenggg.blogspot.com for more study material.

Passing parameters to applet


Supply user defined parameters to an applet using <PARAM> tag. Each <PARAM > tag has a name attribute and a value attribute. Inside the applet code the applet can refer to that parameter by name and find its value.

visit www.muenggg.blogspot.com for more study material.

Passing parameters to applet


Example: <APPLET> <PARAM = color VALUE = red> </APPLET> Also we can change the text to be displayed by an applet by supplying new text to the applet.
visit www.muenggg.blogspot.com for more study material.

Passing parameters to applet


<PARAM NAME = text VALUE = JAVA > To pass parameters we need two things Include appropriate <PARAM> tags in HTML document provide code in the applet to parse these parameters.
visit www.muenggg.blogspot.com for more study material.

Passing parameters to applet : Example


import java.awt.*; import java.applet.*; public class HelloJavaParam extends Applet { String s ; public void init() { s = getParameter(string);
visit www.muenggg.blogspot.com for more study material.

Passing parameters to applet : Example


if ( s == null) s = Java ; s = Hello + s ; } public void paint(Graphics g) { g.drawString(s,10,100); }
visit www.muenggg.blogspot.com for more study material.

Passing parameters to applet : Example


<HTML> <HEAD> <TITLE> Welcome to Java applets </TITLE> </HEAD> <BODY> <APPLET code = HelloJavaParam.class WIDTH = 400 HEIGHT = 200>
visit www.muenggg.blogspot.com for more study material.

Passing parameters to applet : Example


<PARAM NAME = string VALUE = Applet > </APPLET> </BODY> </HTML>

visit www.muenggg.blogspot.com for more study material.

Displaying numerical values


import java.awt.*; import java.applet.*; public class NumValues extends Applet { public void paint(Graphics g) { int value1 = 10; int value2 = 20;
visit www.muenggg.blogspot.com for more study material.

Displaying numerical values


int sum = value1 + value2; String s = sum + String.valueOf(sum); g.drawString(s,100,100);

} }

visit www.muenggg.blogspot.com for more study material.

Displaying numerical values


<html> <APPLET code = NumValues.class width = 300 height = 300 > </APPLET> </html>
visit www.muenggg.blogspot.com for more study material.

Getting input from the user


Applets treat inputs as text strings create an area of the screen in which the user can type and edit items TextField Once text fields are created for receiving input , type values in the fields and edit them if necessary.

visit www.muenggg.blogspot.com for more study material.

Programs
Userin.java User.html

visit www.muenggg.blogspot.com for more study material.

Programs
HTML code that initializes the name and age of the person . Java program to display the name and age of the person on applet and display if the person is major or minor.

visit www.muenggg.blogspot.com for more study material.

Das könnte Ihnen auch gefallen