Post on 19-May-2015
description
APPLET PROGRAMMING
2
IntroductionWe can write two kinds of program in JAVA
they are
APPLICATION BASED
APPLET BASED
The application based programs are the programs which are used in the places such as super markets, hospitals etc.
The applet based program is used for the development of Graphics .
APPLETSAPPLET is a small application
embedded in HTML page, which is
accessed & transported over the
Internet , automatically installed
into client machines and run as
part of web page.
Applet are great for creating
dynamic & interactive web
applications .
Applet is a self executing program .
Init() : initializes applet before loading.
Start(): starts execution of applet
Paint(): graphics created
Stop(): stops any operation
Destroy(): applet completes execution
To create applet , Applet class from java. applet
package should be imported so tht they are
automatically run by any applet program
Applet Life Cycle
The init() method
The init method is invoked after the applet is created or recreated.
A subclass of Applet should overrides this method if the subclass has an initialization to perform.
Usually includes creating new thread, loading images, setting up GUI components
Start() method
The start method is invoked after the init
method.
Called whenever the applet become active
A subclass of Applet overrides this method
if it has any operation that needs to be
performed whenever the webpage is
visited.
An applet with animation might use the start
method to resume animation
Stop() method
The stop method is the opposite of the start method.
Is invoked when the user leave the page.
A subclass of Applet overrides this method if it has any operation that needs to be performed
When the user leaves the page, any threads that applet has started but not completed will continue to run. You should override the stop method to suspend the running threads so that the applet does not take up system resources when it is inactive
Destroy() method
The destroy() method is invoked when the browser exits normally to inform the applet that is no longer needed and should release any resources it has allocated.
The stop method is always called before the destroy method
A subclass of Applet overrides this method if it has any operation that needs to be performed before it is destroyed
Usually you won’t need to override this method unless you wish to release specific resources, such as threads that the applet created
A SIMPLE APPLETimport java.applet.*;import java.awt.*;
public class MyApplet extends Applet{
public void paint (Graphics g){
g.drawString ("Hi this is my first Applet program", 25, 50);}
}
11
Adding applet to Html File
• You can only run an applet in an HTML page
• The HTML looks something like this:• <html>
<body><h1>Myapplet Applet</h1><applet code=“Myapplet.class"
width="250" height="200"> </applet>
</body></html>
Running Applet in AppletViewer• Compiling
javac Myapplet.java
• Running
appletviewer Myapplet.java
Applet viewer is a command line program to run Java applets.