An applet is a special kind of Java program that a browser enabled with Java technology can download from the internet and run. An applet is typically embedded inside a web page and runs in the context of a browser. An applet must be a subclass of the java.applet class.
All applets are subclasses of Applet. Thus, all applets must import java.applet. Applets must also import java.awt. AWT stands for the Abstract Window Toolkit. Since all applets run in a window, it is necessary to include support for that window. Applets are not executed by the console-based Java run-time interpreter. Rather, they are executed by either a Web browser or an applet viewer.
Showing posts with label Applets. Show all posts
Showing posts with label Applets. Show all posts
Sunday, July 4, 2010
Applet Lifecycle
When an applet begins, the AWT calls the following methods, in this sequence:
- init( )
- start( )
- paint( )
- stop( )
- destroy( )
Applet Methods
init( )
The init( ) method is the first method to be called. This is where variables are initialized. This method is called only once during the run time of your applet.
start( )
The start( ) method is called after init( ). It is also called to restart an applet after it has been stopped. start( ) is called each time an applet’s HTML document is displayed onscreen. So, if a user leaves a web page and comes back, the applet resumes execution at start( ).
paint( )
paint( ) is called after start(). The paint( ) method is called each time your applet’s output must be redrawn due to the applet window being overwritten by another window and then uncovered or the applet window may be minimized and then restored. The 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. This context is used whenever output to the applet is required.
stop( )
The stop( ) method is called when a web browser navigates away from the page containing the applet. stop( ) is used to suspend threads that don’t need to run when the applet is not visible. You can restart them when start( ) is called if the user returns to the page.
destroy( )
The destroy( ) method is called when the environment determines that your applet needs to be removed completely from memory. At this point, you should free up any resources the applet may be using. The stop( ) method is always called before destroy( ).
The init( ) method is the first method to be called. This is where variables are initialized. This method is called only once during the run time of your applet.
start( )
The start( ) method is called after init( ). It is also called to restart an applet after it has been stopped. start( ) is called each time an applet’s HTML document is displayed onscreen. So, if a user leaves a web page and comes back, the applet resumes execution at start( ).
paint( )
paint( ) is called after start(). The paint( ) method is called each time your applet’s output must be redrawn due to the applet window being overwritten by another window and then uncovered or the applet window may be minimized and then restored. The 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. This context is used whenever output to the applet is required.
stop( )
The stop( ) method is called when a web browser navigates away from the page containing the applet. stop( ) is used to suspend threads that don’t need to run when the applet is not visible. You can restart them when start( ) is called if the user returns to the page.
destroy( )
The destroy( ) method is called when the environment determines that your applet needs to be removed completely from memory. At this point, you should free up any resources the applet may be using. The stop( ) method is always called before destroy( ).
Difference between an applet and a program
• Applets do not have a main() method.
• Applets cannot run independently. They are run from within a browser.
• Applets cannot read from or write to files on the local computer.
• Applets cannot run any program from the local computer.
• Applets are restricted from using libraries of other languages.
• Applets cannot communicate with other computers on the network.
• Applets cannot run independently. They are run from within a browser.
• Applets cannot read from or write to files on the local computer.
• Applets cannot run any program from the local computer.
• Applets are restricted from using libraries of other languages.
• Applets cannot communicate with other computers on the network.
Applet Html Tag
This is the Html Tag for an Applet :
< APPLET
[CODEBASE = codebaseURL] < ! - - optional, specifies the base URL of the applet code - ->
CODE = appletFile < ! - - required, name of the file containing your applet’s compiled .class file - ->
[ALT = alternateText] < ! - - optional, message to be displayed if the browser supports the APPLET tag but can’t currently run Java applets. - ->
[NAME = appletInstanceName] < ! - - optional , name for the applet instance - ->
WIDTH = pixels HEIGHT = pixels < ! - - required size in pixels - ->
[ALIGN = alignment] < ! - - optional, alignment: LEFT, RIGHT, TOP, BOTTOM, MIDDLE, BASELINE, TEXTTOP, ABSMIDDLE, and ABSBOTTOM- ->
[VSPACE = pixels] [HSPACE = pixels] < ! - - optional the space, in pixels, above and below the applet or left and right of the applet - ->
>
[< PARAM NAME = AttributeName VALUE = AttributeValue>] < ! - - specify applet specific arguments in an HTML page - ->
[< PARAM NAME = AttributeName2 VALUE = AttributeValue>] < ! - - Applets access their attributes with the getParameter( ) method - ->
. . .
[HTML Displayed in the absence of Java]
/APPLET >
< APPLET
[CODEBASE = codebaseURL] < ! - - optional, specifies the base URL of the applet code - ->
CODE = appletFile < ! - - required, name of the file containing your applet’s compiled .class file - ->
[ALT = alternateText] < ! - - optional, message to be displayed if the browser supports the APPLET tag but can’t currently run Java applets. - ->
[NAME = appletInstanceName] < ! - - optional , name for the applet instance - ->
WIDTH = pixels HEIGHT = pixels < ! - - required size in pixels - ->
[ALIGN = alignment] < ! - - optional, alignment: LEFT, RIGHT, TOP, BOTTOM, MIDDLE, BASELINE, TEXTTOP, ABSMIDDLE, and ABSBOTTOM- ->
[VSPACE = pixels] [HSPACE = pixels] < ! - - optional the space, in pixels, above and below the applet or left and right of the applet - ->
>
[< PARAM NAME = AttributeName VALUE = AttributeValue>] < ! - - specify applet specific arguments in an HTML page - ->
[< PARAM NAME = AttributeName2 VALUE = AttributeValue>] < ! - - Applets access their attributes with the getParameter( ) method - ->
. . .
[HTML Displayed in the absence of Java]
/APPLET >
Applet Example
This is an example Applet Java Code:
import java.awt.*;
import java.applet.*;
public class Sample extends Applet
{
String msg;
// set the foreground and background colors.
public void init()
{
setBackground(Color.cyan);
setForeground(Color.red);
msg = "Inside init( ) ";
}
public void start()
{
msg += " Inside start( ) ";
}
// Display msg in applet window.
public void paint(Graphics g)
{
msg += " Inside paint( )";
g.drawString(msg, 10, 30);
}
}
import java.awt.*;
import java.applet.*;
public class Sample extends Applet
{
String msg;
// set the foreground and background colors.
public void init()
{
setBackground(Color.cyan);
setForeground(Color.red);
msg = "Inside init( ) ";
}
public void start()
{
msg += " Inside start( ) ";
}
// Display msg in applet window.
public void paint(Graphics g)
{
msg += " Inside paint( )";
g.drawString(msg, 10, 30);
}
}
Subscribe to:
Posts (Atom)
