Beruflich Dokumente
Kultur Dokumente
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.
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.
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.
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)
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
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() {
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.
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.
end destroy()
(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.
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.
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.
Use either Java enabled Web browser or appletviewer utility to run an applet
visit www.muenggg.blogspot.com for more study material.
} }
Programs
Userin.java User.html
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.