0% found this document useful (0 votes)
25 views6 pages

Java Applet

Uploaded by

sweeti sah
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views6 pages

Java Applet

Uploaded by

sweeti sah
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Java Applet

Introduction to Java
JAVA offers a number of advantages to developers.
Java is simple
Java was designed to be easy to use and is therefore easy to write, compile, debug, and
learn than other programming languages. The reason that why Java is much simpler than
C++ is, because Java uses automatic memory allocation and garbage collection where
else C++ requires the programmer to allocate memory and to collect garbage.
Java is object-oriented
Java is object-oriented because programming in Java is centred on creating objects,
manipulating objects, and making objects work together. This allows you to create
modular programs and reusable code.
Java is platform-independent
One of the most significant advantages of Java is its ability to move easily from one
computer system to another. The ability to run the same program on many different
systems is crucial to World Wide Web software, and Java succeeds at this by being
platform-independent at both the source and binary levels.
Java is distributed
Distributed computing involves several computers on a network working together. Java is
designed to make distributed computing easy with the networking capability that is
inherently integrated into it. Writing network programs in Java is like sending and
receiving data to and from a file. For example, the diagram below shows three programs
running on three different systems, communicating with each other to perform a joint
task.
Java is interpreted
An interpreter is needed in order to run Java programs. The programs are compiled into
Java Virtual Machine code called bytecode. The bytecode is machine independent and is
able to run on any machine that has a Java interpreter. With Java, the program need only
be compiled once, and the bytecode generated by the Java compiler can run on any
platform.
Java is secure
Java is one of the first programming languages to consider security as part of its design.
The Java language, compiler, interpreter, and runtime environment were each developed
with security in mind.
Java is robust
Robust means reliable and no programming language can really assure reliability. Java
puts a lot of emphasis on early checking for possible errors, as Java compilers are able to
detect many problems that would first show up during execution time in other languages.
Java is multithreaded
Multithreaded is the capability for a program to perform several tasks simultaneously
within a program. In Java, multithreaded programming has been smoothly integrated into
it, while in other languages, operating system-specific procedures have to be called in
order to enable multithreading. Multithreading is a necessity in visual and network
programming.
Writing java Applets
Java applet is an applet delivered to users in the form of Java bytecode. Java applets can
run in a Web browser using a Java Virtual Machine (JVM), or in Sun's AppletViewer, a
standalone tool for testing applets.
Java applets are executed in a sandbox by most web browsers, preventing them from
accessing local data like clipboard or file system. The code of the applet is downloaded
from a web server and the browser either embeds the applet into a web page or opens a
new window showing the applet's user interface.
A Java applet extends the class java.applet. Applet, or in the case of a Swing applet,
javax.swing.JApplet. The class must override methods from the applet class to set up a
user interface inside itself (Applet is a descendant of Panel which is a descendant of
Container.
As applet inherits from container, it has largely the same user interface possibilities as an
ordinary Java application, including regions with user specific visualization. The applet
can be displayed on the web page by making use of the deprecated applet HTML element,
or the recommended object element.
Example
The following example is made simple enough to illustrate the essential use of Java
applets through its java.applet package. It also uses classes from the Java Abstract
Window Toolkit (AWT) for producing actual output (in this case, the "Hello, world!"
message).
import java.applet.Applet;
import java.awt.*;
// Applet code for the "Hello, world!" example.
// This should be saved in a file named as "HelloWorld.java".
public class HelloWorld extends Applet {
// This method is mandatory, but can be empty (i.e., have no actual code).
public void init() { }
// This method is mandatory, but can be empty.(i.e.,have no actual code).
public void stop() { }
// Print a message on the screen (x=20, y=10).
public void paint(Graphics g) {
g.drawString("Hello, world!", 20,10);
// Draws a circle on the screen (x=40, y=30).
g.drawArc(40,30,20,20,0,360);
}
}
For compiling, this code is saved on a plain-ASCII file with the same name as the class
and .java extension, i.e. HelloWorld.java. The resulting HelloWorld.class applet should
be placed on the web server and is invoked within an HTML page by using an
<APPLET> or an
<OBJECT> tag. For example:
<!DOCTYPE HTML PUBLIC
"-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<HTML>
<HEAD>
<TITLE>HelloWorld_example.html</TITLE>
</HEAD>
<BODY>
<H1>A Java applet example</H1>
<P>Here it is: <APPLET code="HelloWorld.class" WIDTH="200" HEIGHT="40">
This is where HelloWorld.class runs.</APPLET></P>
</BODY>
</HTML>
Displaying the HelloWorld_example.html page from a Web server, the result should look
as this:
A Java applet example
Here it is: Hello, world!
Advantages
A Java applet can have any or all of the following advantages:
o It is simple to make it work on Linux, Microsoft Windows and Mac OS X i.e. to make
it cross platform. Applets are supported by most web browsers.
o The same applet can work on "all" installed versions of Java at the same time, rather
than just the latest plug-in version only. However, if an applet requires a later version
of the Java Runtime Environment (JRE) the client will be forced to wait during the
large download.
o Most web browsers cache applets, so will be quick to load when returning to a web
page. Applets also improve with use: after a first applet is run, the JVM is already
running and starts quickly (the JVM will need to restart each time the browser starts
afresh).
o It can move the work from the server to the client, making a web solution more
scalable with the number of users/clients.
o If a standalone program (like Google Earth) talks to a web server, that server normally
needs to support all prior versions for users which have not kept their client software
updated. In contrast, a properly configured browser loads (and caches) the latest
applet version, so there is no need to support legacy versions.
o The applet naturally supports the changing user state, such as figure positions on the
Chessboard
o Developers can develop and debug an applet direct simply by creating a main routine
(either in the applet's class or in a separate class) and calling init() and start() on the
applet, thus allowing for development in their favorite Java SE development
environment. All one has to do after that is re-test the applet in the AppletViewer
program or a web browser to ensure it conforms to security restrictions.
o An untrusted applet has no access to the local machine and can only access the server
it came from. This makes such an applet much safer to run than a standalone
executable that it could replace. However, a signed applet can have full access to the
machine it is running on if the user agrees.
o Java applets are fast - and can even have similar performance to native installed
software.
Life cycle of An Applet
Introduction
In this Section you will learn about the lifecycle of an applet and different methods of an
applet. Applet runs in the browser and its lifecycle method are called by JVM when it is
loaded and destroyed. Here are the lifecycle methods of an Applet:
init(): This method is called to initialized an applet
start(): This method is called after the initialization of the applet.
stop(): This method can be called multiple times in the life cycle of an Applet.
destroy(): This method is called only once in the life cycle of the applet when applet
is destroyed.
init () method:
The life cycle of an applet is begin on that time when the applet is first loaded into the
browser and called the init() method. The init() method is called only one time in the life
cycle on an applet. The init() method is basically called to read the PARAM tag in the
html file. The init () method retrieve the passed parameter through the PARAM tag of
html file using get Parameter() method All the initialization such as initialization of
variables and the objects like image, sound file are loaded in the init () method .After the
initialization of the init() method user can interact with the Applet and mostly applet
contains the init() method.
Start () method:
The start method of an applet is called after the initialization method init(). This method
may be called multiples time when the Applet needs to be started or restarted. For
Example if the user wants to return to the Applet, in this situation the start Method() of an
Applet will be called by the web browser and the user will be back on the applet. In the
start method user can interact within the applet.
Stop () method:
The stop() method can be called multiple times in the life cycle of applet like the start ()
method. Or should be called at least one time. There is only miner difference between the
start() method and stop () method. For example the stop() method is called by the web
browser on that time When the user leaves one applet to go another applet and the start()
method is called on that time when the user wants to go back into the first program or
Applet.
destroy() method:
The destroy() method is called only one time in the life cycle of Applet like init() method.
This method is called only on that time when the browser needs to Shutdown.
Applet versus Application
Applets as previously described, are the small programs while applications are larger
programs. Applets don't have the main method while in an application execution starts
with the main method. Applets can run in our browser's window or in an appletviewer. To
run the applet in an appletviewer will be an advantage for debugging. Applets are
designed for the client site programming purpose while the applications don't have such
type of criteria. Applet are the powerful tools because it covers half of the java language
picture. Java applets are the best way of creating the programs in java. Applets are
designed just for handling the client site problems. While the java applications are
designed to work with the client as well as server. Applications are designed to exist in a
secure area while the applets are typically used. Applications and applets have much of
the similarity such as both have most of the same features and share the same resources.
Applets are created by extending the java.applet.Applet class while the java applications
start execution from the main method.
Applications are not too small to embed into a html page so that the user can view the
application in your browser. On the other hand applet have the accessibility criteria of the
resources. The key feature is that while they have so many differences but both can
perform the same purpose.
Review of Java Applets:
To create an applet just create a class that extends the java.applet.Applet class and inherit
all the features available in the parent class. The following programs make all the things
clear.
import java.awt.*;
import java.applet.*;
class Myclass extends Applet {
public void init() {
/* All the variables, methods and images initialize here
will be called only once because this method is called only
once when the applet is first initializes */
}
public void start() {
/* The components needed to be initialize more than once
in your applet are written here or if the reader
switches back and forth in the applets. This method
can be called more than once.*/
}
public void stop() {
/* This method is the counterpart to start(). The code,
used to stop the execution is written here*/
}
public void destroy() {
/* This method contains the code that result in to release the resources to the applet before
it is finished. This method is called only once. */ }
public void paint(Graphics g) {
/* Write the code in this method to draw, write, or color
things on the applet pane are */
}
}
In the above applet you have seen that there are five methods. In which two ( init() and
destroy) are called only once while remaining three (start() , stop() , and paint() ) can be
called any number of times as per the requirements. The major difference between the
two (applet and application) is that java applications are designed to work under the
homogenous and more secure areas. On contrary to that, java applets are designed to run
the heterogeneous and probably unsecured environment. Internet has imposed several
restrictions on it.
Applets are not capable of reading and writing the user's file system. This means that the
applet neither can access nor place anything locally. One more thing to point here is that
applets are unable to use the native methods, run any program on the user system or load
shared libraries. The major security concern here is that the local shared libraries and the
native methods may results in the loophole in the java security model.
Applets are not capable of communicating the server than one from which they are
originating. There are the cases in which an encryption key is used for the verification
purpose for a particular applet to a server. But accessing a remote server is not possible.
The conclusion is that the java applets provides a wide variety of formats for program
execution and a very tight security model on the open environment as on the Internet.
Introduction to Java Application:
Java applications have the majority of differences with the java applets. If we talk at the
source code level, then we don't extend any class of the standard java library that means
we are not restricted to use the already defined method or to override them for the
execution of the program. Instead we make set of classes that contains the various parts of
the program and attach the main method with these classes for the execution of the code
written in these classes. The following program illustrates the structure of the java
application.
public class MyClass {
/* Various methods and variable used by the class
MyClass are written here */
class myClass {
/* This contains the body of the class myClass */
}
public static void main(String args[]) {
/* The application starts it's actual execution from this place. **/
}
}
The main method here is nothing but the system method used to invoke the application.
The code that results an action should locate in the main method. Therefore this method is
more than the other method in any java application. If we don't specify the main method
in our application, then on running the application will through an exception like this one:
In the class MyClass: void main(String args[]) is undefined But at higher level major
concern is that in a typical java application security model, an application can access the
user's file system and can use native methods. On properly configuring the user's
environment and the java application it will allow access to all kind of stuff from the
Internet. In most of the cases it is seen that the java application seems like a typical
C/C++ application. Now we are going to create plenty of applications to exemplify some
of the methods and features of a specific Java application.

You might also like