Showing posts with label Applets. Show all posts
Showing posts with label Applets. Show all posts

Sunday, 25 March 2012

Other Methods in Applets

•update() – This method is available in the “Component” class which is available in “java.awt” package. This method is used to update a specific region or a part of the applet.

•repaint() – This method is used to self update the applet’s display area. repaint() implicitly invokes the update() method. repaint() is also declared in the “Component” class.

•getDocumentBase() – This method returns the location of the applet file (.class file). The return type of this method is the predefined class “URL”.

•getCodeBase() – This method returns the directory path in which the applet file (.class file) is present. The return type of this method is “URL”.


•showDocument() – This method is declared in the “AppletContext” interface which is available in “java.applet” package.

AppletContext allows you to gather information from the applet’s environment.

Using the showDocument() method we can view other types of files in the browser.

There are two versions of this method as shown below:
1) void showDocument(URL url)
2) void showDocument(URL url, String location)
The valid values for the “location” parameter are “_self”, “_parent”, “_top” and “_blank”.

Applet Tag

<applet> tag

•The <applet> tag in HTML is used to identify the applets in a webpage by the browser and by applet viewer.
•If there are multiple <applet> tags, applet viewer opens each applet in a separate window. Where as browser can load both the applets in a single page.

<applet> tag attributes: 

•Following are the attributes or options supported by the <applet> tag:

1) codebase: This attribute is used to specify the base URL where the applet file(.class file) is located. By default this property will be set to the location where the .html file is available.

2) code: This attribute is used to specify the name of the applet file(.class file)

3) width: Used to specify the width(in pixels) of the applet’s display area.

4) height: Used to specify the height(in pixels) of the applet’s display area.


5) name: This attribute is used to specify the name of the applet. It is used to identify one applet from another, if there are multiple applets in the same webpage.

6) alt: This attribute is used display a short message when the browser identifies the <applet> tag, but is not able to load the applet into browser.

7) align: This attribute is used to specify the alignment of the applet in the browser.

8) vspace: Used to specify some gap above and below the applet’s display area.

9) hspace: Used to specify some gap left and right to the applet’s display area.

10) param: Used to specify the parameters for the applet. This param tag has attributes, “name” and “value”. We use “name” attribute to specify the name of the parameter and “value” attribute to specify the value of the parameter.

Some other attributes of the <applet> tag are “archive” and “object”

Applet to Applet Communication

•An applet can communicate with another applet by using the “getApplet()” or “getApplets()” method available in the “AppletContext” interface.
•The “Applet” class implements the “AppletContext” interface and provided implementation for these methods.
•getApplet() returns the object of the specified applet.

 Syntax: Applet getApplet(String name)

•getApplets() returns a collection of all the applets present in the document.

Syntax: Enumeration getApplets()


•To use the getApplet() method, we must use another method in the “Applet” class namely the “getAppletContext()”.
•getAppletContext() returns the current context of the applet i.e., the current document(web page).
•Use the following syntax for using the getApplet() method:
Applet obj = getAppletContext().getApplet(“Name of the Applet”);
•After obtaining the object to the required applet, you can access any public methods in that applet class.

Passing Parameters to Applet

•The <applet> tag in html allows us to pass parameters to the applet.
•The <param> tag is used to specify parameter name and value.
•The “name” attribute/option of the <param> tag is used to specify the name of the parameter.
•The “value” attribute/option of the <param> tag is used to specify the value for the parameter.
•The “getParameter()” method in the “Applet” class is used to access the parameters from the <applet> tag. This method returns the parameter value in the form of “String”.

•Syntax of getParameter() method is:
 String getParameter(String name)

Types of Applets

•There are two types of applets:
1) Unsigned applets
2) Signed applets

Unsigned Applets:

•They do not contain any digital signature.
•Unsigned applets have very limited access to the client environment.
•They cannot execute external commands on client side.
•Using unsigned applets servers can be exploited as they bypass the firewalls.
•Unsigned applets can be used to download/spread malware originating from the server.

Signed Applets:

•A signed applet contains a digital signature attached to it.
•Before using a signed applet, the client must approve the digital signature.
•The digital signature states that whatever happens to the client system due to the signed applet, they take responsibility to it.
•When the client approves the signature, the signed applet becomes a standalone program and can access local resources on the client system.
•We need other tools to generate the digital signature, which is a complex process.
•The disadvantage of signed applets is that they impose security threats.

Friday, 16 March 2012

Executing Applets

•Before executing applets, we must compile the program in the same way as you compile a console application. Let the name of my file be “MyApplet.java”. So, for compiling we use:
javac MyApplet.java

•Now, execution of applets is different from executing console applications as applets do not have a main() method. Applets can be executed in two ways:
1) By using any standard web browser( like IE, firefox, chrome)
2) By using “applet viewer” tool provided by Java SDK.

Executing Applets (Using Browser):



Executing Applets (Using applet viewer):
•By now, you have created two files: MyApplet.java and MyApplet.html.
•applet viewer is a command line tool provided by Java SDK to test the applets before deploying them to web pages.
•For executing applets using applet viewer, open command prompt, navigate to the location where your files are present and type the following:
appletviewer MyApplet.html

(or)

If you don't want to create a html page separately, you can embed the <applet> tag within in the source program file, which is in my example:  MyApplet.java

You can now execute your applet program by using the following command at command prompt:
 appletviewer MyApplet.java

Thursday, 8 March 2012

Creating and executing applets

•Before learning about how to create applets, let us see the basic methods that help us to control the execution of an applet.
•Most of the applets override a set of methods which provide the basic functionality for the applets. Those methods are:
1) init()
2) start()
3) stop()
4) destroy()
5) paint()
•The first four methods are available in the “Applet” class.
•The paint() method is available in the “Component” class.


•When an applet begins, the following methods are executed in sequence:
1) init()
2) start()
3) paint()
•When an applet terminates, the following methods are executed in sequence:
1) stop()
2) destroy()



•init() – The init() method is the first method to be executed when an applet is loaded into the web browser or applet viewer. This method is used to initialize the variables. This method is called only once during the execution of an applet.
•start() – This method is called immediately after the init() method. This method is also called when the applet stops and restarts its execution.
•stop() – This method is called when the web browser leaves the document containing the applet i.e., when the execution of the applet stops. This method is also executed before the destroy() method is invoked.
•destroy() – This method is called when the JVM decides to remove the applet completely from the memory. stop() method is always called before destroy() method.

•paint() – This method is called after the execution of the start() method. paint() is also called whenever the output needs to be redrawn, i.e., whenever the window is minimized and maximized etc… The paint() method contains only one parameter of the type “Graphics”. This parameter represents the current graphics context. We can use this to output data or draw objects like circle, square, rectangle etc…




•In the previous program, the first import statement is required to import the “Applet” class.
•The second import is required as we are using the “paint()” method which is present in the “Component” class available in “java.awt” package. Also for using the “Graphics” class.
•Every applet class must extend the predefined class “Applet”.
•The class must be declared as “public” as it will be accessed from outside the program.
•The drawString() method is available in “Graphics” class which is used to print a string to the GUI. Its syntax is:
void drawString(String message, int x-coordinate, int y-coordinate);

Applets Basics

•An applet is a small Java application that can be accessed from an internet server, downloaded automatically, and run along as a part within the webpage.
•Applets provide security. All the applets run in a sandbox like environment. They have limited access to the client’s system resources. Hence there is no threats of viruses and other malicious programs.
•Applets allow programmers to develop GUI applications.
•Applets provide GUI through java’s AWT or Swing classes.

Differences between applets and console applications: 

Applet Class:

•Every applet application extends the “Applet” class which is available in “java.applet” package.
•“Applet” class contains several methods which allows us to control the applet.
•“java.applet” package also contains three other interfaces namely: AppletContext, AppletStub and AudioClip.
•Applets provide GUI through AWT(Abstract Window ToolKit) or Swing classes.
•Swing package provides JApplet class. By extending this class we can create applets.
•Swing classes provide extended and easy-to-use functionality over the AWT classes.

•All applets are subclasses of “Applet” class in “java.applet” package or subclasses of “JApplet” class in “javax.swing” package.
•Applets are not standalone programs that contain a main() method.
•Applets run within a web browser or by using appletviewer.
•appletviewer is a tool provided by the JDK to run applets.


Applet Class Methods:





Applet Life Cycle: