Seminar Paper On Java Applets
Seminar Paper On Java Applets
ON
PRESENTED BY
SUBMITTED TO
MARCH, 2010.
1
Ogbodo Anthony Emeka +2348068573605
ABSTRACT
Web design has moved to the next level with the introduction of java technology in the
computing and programming industry. This introduction has made a tremendous impact in
the capabilities and functionalities of web pages. Now using not only HTML codes but java
scripts and java applets, more interactivities and functionalities can be added to web pages.
This seminar paper looks at what a java applet is, how to write applet codes and how to
2
Ogbodo Anthony Emeka +2348068573605
INTRODUCTION
This seminar paper discusses the basics of applets, how to deploy them. These applets
An applet is a special type of java program that a browser enabled with java technology can
download from the internet and run. An applet must be a sub class of the java.applet.Applet
class. The applet class provides the standard interface between the applet and the browser
environment. Swing provides a special sub class of the Applet class called
javax.Swing.JApplet. The JApplet class should be used for all applets that use the swing
components to construct their graphical user interface. The browser’s java plug-in software
Most of today’s browsers can run java applets. The ones that cannot run are not of much
importance since very few users have such outdated browsers. To be more precise, applets
can be embedded in pages viewed by netcape2 and newer versions, internet explore 3 and
newer version, and others. However, some people have turned off the ability to run applets in
their web browsers. These in most cases are companies with more or less paranoid ideas of
potential hacking. No matter what their motivation is, the fact is that there are minor people
who will not view your applet. This should be taken into consideration before deciding to add
3
Ogbodo Anthony Emeka +2348068573605
DEFINING A SUB CLASS OF AN APPLET
Every applet must define a sub class of Applet or JApplet class. Every java applet is a
graphical user interface on which you can place graphical user interface (GUI) components.
They inherit the significant functionality from the applet or JApplet class including the
capability to communicate with the browsers and present a graphical user interface (GUI) to
8 super.paint (g);
Line 1 imports graphics to enable the applet to draw graphics such as lines, rectangles, ovals
and strings of characters. The class JApplet (imported at line 2) from package javax.Swing is
used to create applet. As applications, every java applet contains at least one public class
4
Ogbodo Anthony Emeka +2348068573605
declaration. An applet container can create only objects of classes that are public and extends
JApplet (or the applet class from earlier versions of java). For this reason, class DisplayText
An applet container expects every java applet to have method named init, start, paint, stop
and destroy each of which is declared in the class JApplet. These methods can be overridden
(refined) to perform tasks that are specific to your applet. When an applet container loads
class DisplayText, the container creates an object of type DisplayText then calls three of the
applet methods. In sequence, these three methods are init, start and paint. If you do not
declare the methods in your applet, the applet container calls the inherited versions. The
super class methods init and start have empty bodies so, they do not perform any task. It is
necessary for the applet container to inherit these methods init, start and paint even when
some do not make any use of them so that it will have a consistent start up sequence as other
applications start execution with the main method. This inheritance ensures that the applet
container executes each applet uniformly. Also, inheriting the default implementation of
these methods allows the programmer to concentrate on drafting only the methods required
for a particular applet. The method paint() was overridden in order to draw the string (line 6-
11) by placing statements in the body of the paint that draws a message on the screen. Line 8
calls the super class method paint that was inherited from JApplet. This statement should be
the first in any applet paint method, omitting it can cause subtle errors in applets that
combine drawing and GUI components. Line 10 uses graphics to draw the string “This is my
5
Ogbodo Anthony Emeka +2348068573605
APPLET LIFE-CYCLE METHODS
An applet class provides a frame work for execution, defining methods that the system call
when milestone occur. Milestones are major events in an applet life cycle. Most applets
These methods are explained below. Other than method paint (), these methods have empty
bodies by default. If you want to declare any of these methods and have the applet container
call them, you must use the method headers shown below. If you modify the header (e.g. by
changing the names or by providing parameters), the applet container will not call your
methods instead it will call the super class method inherited from JApplet.
This method is called once by the applet container when an applet is loaded for execution.
This method initializes an applet. The action performed her are purely initialization of field,
creating GUI components, loading sound to play, loading images to display and creating
threads. Keep your initialization method so short that your applet may load quickly.
Every applet that perform tasks after initialization (except in direct response to user actions.)
must override the start method. It is good to practice to return quickly from the start method.
If you need to perform computionally intensive operations, it might be better to start a new
thread for this purpose. This method is called by the applet container after the initialization
method. Action performed might include starting an animation or starting other threads of
execution.
6
Ogbodo Anthony Emeka +2348068573605
public void paint (Graphics g)
This method is called after init () and start () methods. The method is also called when an
applet needs to repainted. Example is if a user minimizes or cover an open page containing
applet and uncovers it, the paint method is called. Action performed involves drawing with
the graphic object g that is passed to the paint method by the applet container.
Most applets that override the start method should also override the stop method. The stop
method should stop the applet’s execution to save memory and resources when the user is not
viewing it. Example, an applet that draws animation should stop drawing it when the user is
not viewing it. Typically, action performed here would stop the execution and threads.
This method is called by the applet container when the applet is removed from the memory.
There is no need to override the destroy method because the stop method which is called
before destroy will perform tasks necessary to shut down the applet’s execution. This occurs
when the user exits the session by closing all the browser windows. The method performs
any task that is required to release additional resources allocated to the applet.
7
Ogbodo Anthony Emeka +2348068573605
APPLET’S EXECUTION ENVIRONMENT.
An applet runs in the context of browser. Java plug-in software in the browser controls the
loading and execution of the applet. The brower also has the JavaScript interpreter which
runs the JavaScript code on a web page.
Java plug-in
The java plug-in created a working thread for every applet. It loads the applet in an instance
of a java runtime environment (JRE) software. Normally all applets run in the same instance
of JRE. The java pug-in software starts a new instance of the JRE in the following cases.
1. When an applet requests to be executed in a specific version of the JRE.
2. When an applet specifies its own JRE start up parameters. For example, the heap size.
A new applet uses an existing JRE when its requirements are a subset of the existing
JRE otherwise, a new instance is started.
Java plug-in and java script interpreter interaction.
Applets can invoke JavaScript function present in the web page. JavaScript functions are also
allowed to invoke methods of an applet embedded on the same web page. The java plug-in
software and the java interpreter calls from JavaScript code to java code and from calls from
java code to JavaScript code. The java plug-in software is multithreaded while the java
interpreter runs on a single thread. Hence, to avoid thread related issues especially when
multi applets are running simultaneously. Keep the calls between java code and JavaScript
code short to avoid round trip.
8
Ogbodo Anthony Emeka +2348068573605
object class implements the runnable interface. Accordingly, applets that run animation by
means of thread normally implement runnable.
The applet starts execution in line 10 with its init() method. It sets the size of the applet
container, instantiates the main panel, and adds it to the applet’s content pane. Then the
applet’s start() method invoked(automatically by the browser). Since the thread is initially
null, this creates a new thread and starts it in line 29. Since the applet class implement
runnable, the thread’s start() method invokes the applet’s run() method which invokes the
panel’s repaint() method(at line 22) every 10 milliseconds.
The JPanel class’s repaint() method calls its paint() method which calls its paintComponent()
method. This is overridden in MainPanel class at line 42. It calls the JPanel class’s
paintComponent() method at line 43, sets a big and bold font in line 44, and then finally
draws the current time at line 45.
10
Ogbodo Anthony Emeka +2348068573605
TESTING AN APPLET
For testing purpose, an applet viewer is most convenient. The java software development kit
(java SDK) includes an applet viewer named “applet viewer.exe”. it is located in the bin
directory of the SDK. To run the program example above, first, compile the program to get
the clock.class bytecode file, then enter this at the command prompt
appletviewer clock.html
The applet class should launch and display the applet. You can also use a web browser to test
your applet.
DEPLOYMENT OF AN APPLET
After the compilation of your applet which can be done in two ways:
1. By using the command prompt: Type your applet code in word editor like note pad
and save it with .java. Enter the command prompt and type “javac filename.java”. the
compiler will generate a class file (i.e. filename.class).
2. By using an integrated development environment (IDE): After typing code in a text
editor of the IDE, use the compile feature of the IDE to compile the source code to a
class format.
Design your web page as you like and put the code as below;
11
Ogbodo Anthony Emeka +2348068573605
The essential codes are lines 6-7. This is an applet tag with three attributes: code, width, and
height. The values for these three attributes are “clock.class”, 300 and 60, respectively each
is assigned on line 6. This tells the HTML environment (browser or applet viewer) to run the
clock.class applet in a from 300 pixels wide and 60 pixel high. Note that the program is
enclosed between the <html> tag(line) and </html> tag (line10). The web page has two main
part, its title and its body (line 2 and 3-9 respectively)
12
Ogbodo Anthony Emeka +2348068573605
o They can access the system-wide clipboard.
o They can access printing functions.
o They can store data in the client; decide how applets should be
downloaded and cached, and much more.
SIGNED APPLETS.
They do not have the security restrictions imposed on unsigned ones and can run outside the
security sandbox.
Note: when a signed applet is accessed from JavaScript code in an HTML page, the applet is
executed within the sandbox. This implies that signed applet essentially behaves like
unsigned applet.
DISADVANTAGES OF APPLETS.
Before allowing an applet to run, make sure you know and trust the source as it may harm
your system because some programmers use it as a harking tool, a means to introduce
viruses, and can act as a spyware or adware to your system. It needs a java plug-in on your
browser before it can be able to run.
ADVANTAGES OF APPLETS.
1. Java applets add interactivity to web pages.
2. Allow application to run on web pages like playing games while browsing, chatting
on web pages as applets have network connectivity ability.
13
Ogbodo Anthony Emeka +2348068573605
CONCLUSION
Having strategically discussed java applets, its development, deployment, advantages and
disadvantages, it can be seen that writing java applet codes are very simple to do as well as
14
Ogbodo Anthony Emeka +2348068573605
RECOMMENDATIONS
When designing a web page that you need that some logical applications run on, using java
applet is more convenient. Finally, when designing a java applet, bear in mind that there are
some fractions of people that will not view your applet, also before allowing an applet to run
on your browser, make sure that you know and trust its source or that it is signed.
15
Ogbodo Anthony Emeka +2348068573605
REFERENCES
Deitel (2007): Java How to Program Seventh Edition, Pearson Education, Inc.
USA.
John .R. Hubbard, Ph.D (2004): Programming with Java Second Edition. The
http://java.sun.com/doc/books/tutorial/deployment/applet/,
(Retrieved 15/02/2010)
16
Ogbodo Anthony Emeka +2348068573605