Java 5th UNIT (Chapter-1)
Java 5th UNIT (Chapter-1)
UNIT-5(Chapter-1)
Applets
Introduction to an Applet:
An applet is a Java program that runs in a Web browser. An applet can be a fully functional
Java application because it has the entire Java API at its disposal.
There are some important differences between an applet and a standalone Java application,
including the following:
● A main() method is not invoked on an applet, and an applet class will not define main().
● When a user views an HTML page that contains an applet, the code for the applet is
downloaded to the user's machine.
● A JVM is required to view an applet. The JVM can be either a plug-in of the Web browser or
a separate runtime environment.
● The JVM on the user's machine creates an instance of the applet class and invokes various
methods during the applet's lifetime.
● Applets have strict security rules that are enforced by the Web browser. The security of an
applet is often referred to as sandbox security, comparing the applet to a child playing in a
sandbox with various rules that must be followed.
● Other classes that the applet needs can be downloaded in a single Java Archive (JAR) file.
Applet Class:
An applet is a small program that is intended not to be run on its own, but rather to be
embedded inside another application.
JAVA PROGRAMMING
The Applet class must be the superclass of any applet that is to be embedded in a Web page
or viewed by the Java Applet Viewer. The Applet class provides a standard interface between
applets and their environment.
The Applet class doesn't provide any genuine applicability on its own. Its purpose is to
provide a super class from which custom applet classes are extended.
void paint(Graphics g)
void update(Graphics g) paint() is not called directly. This is the method that calls
paint(). You may need to override this method for animation
JAVA PROGRAMMING
AccessibleContext
Gets the AccessibleContext associated with this Applet.
getAccessibleContext()
Locale getLocale() Gets the Locale for the applet, if it has been set.
String
Returns the value of the named parameter in the HTML tag.
getParameter(String
Case insensitive.
name)
Methods that you would call from within the methods that
you create. Not all of these are defined at the "Applet" class.
Utility
Many of these methods are defined higher in the Applet
ancestry.
AudioClip
Returns the AudioClip object specified by the URL and
getAudioClip(URL url,
name arguments.
String name)
Color getBackground()
Color getForeground()
Image getImage(URL url, Returns an Image object that can then be painted on the
String name) screen.
static AudioClip
Get an audio clip from the given URL.
newAudioClip(URL url)
void repaint()
void repaint(long
Requests that the paint() method get called either as soon as
millisec)
possible, or within a period of time.
void repaint(x,y,w,h)
Dimension getSize() [JDK returns the size of the applet on the screen.
1.1]
void showStatus(String Requests that the argument string be displayed in the "status
msg) window". Most browsers provide a status window, but the
JAVA PROGRAMMING
Called by the browser or applet viewer to inform this applet that it has been loaded into the
system. It is always called before the first time that the start method is called. A subclass of
Applet should override this method if it has initialization to perform. For example, an applet
with threads would use the init method to create the threads and the destroy method to kill
them.
Called by the browser or applet viewer to inform this applet that it should start its execution.
It is called after the init method and each time the applet is revisited in a Web page. A
subclass of Applet should override this method if it has any operation that it wants to perform
each time the Web page containing it is visited. For example, an applet with animation might
want to use the start method to resume animation, and the stop method to suspend the
animation.
Called by the browser or applet viewer to inform this applet that it should stop its execution.
It is called when the Web page that contains this applet has been replaced by another page,
and also just before the applet is to be destroyed. A subclass of Applet should override this
method if it has any operation that it wants to perform each time the Web page containing it
is no longer visible. For example, an applet with animation might want to use the start
method to resume animation, and the stop method to suspend the animation.
Called by the browser or applet viewer to inform this applet that it is being reclaimed and that
it should destroy any resources that it has allocated. The stop method will always be called
before destroy. A subclass of Applet should override this method if it has any operation that it
wants to perform before it is destroyed. For example, an applet with threads would use the
init method to create the threads and the destroy method to kill them.
Returns information about this applet. An applet should override this method to return a
String containing information about the author, version, and copyright of the applet. The
default implementation of this method provided by the Applet class returns null.
Returns information about the parameters than are understood by this applet. An applet
should override this method to return an array of Strings describing these parameters. Each
element of the array should be a set of three Strings containing the name, the type, and a
description. For example:
String pinfo[][] = {
{"fps", "1-10", "frames per second"},
{"repeat", "boolean", "repeat image loop"},
{"imgs", "url", "images directory"}
};
● init: This method is intended for whatever initialization is needed for your applet. It is called
after the param tags inside the applet tag have been processed.
JAVA PROGRAMMING
● start: This method is automatically called after the browser calls the init method. It is also
called whenever the user returns to the page containing the applet after having gone off to
other pages.
● stop: This method is automatically called when the user moves off the page on which the
applet sits. It can, therefore, be called repeatedly in the same applet.
● destroy: This method is only called when the browser shuts down normally. Because applets
are meant to live on an HTML page, you should not normally leave resources behind after a
user leaves the page that contains the applet.
● paint: Invoked immediately after the start() method, and also any time the applet needs to
repaint itself in the browser. The paint() method is actually inherited from the java.awt.
Applet Structure:
// init - method is called the first time you enter the HTML site with the applet
public void init() {... }
// start - method is called every time you enter the HTML - site with the applet
public void start() {... }
// stop - method is called if you leave the site with the applet
public void stop() {... }
// destroy method is called if you leave the page finally (e. g. closing browser)
JAVA PROGRAMMING
/** paint - method allows you to paint into your applet. This method is called e.g. if you
move your browser window or if you call repaint() */
public void paint (Graphics g) { }
}
import java.applet.*;
import java.awt.*;
iii)repaint()
The repaint() method is sent to a Component when it needs to be repainted. This happens
when a window is moved, or resized, or unhidden. It also happens when a webpage contains
an image and the pixels of the image are arriving slowly down the wire.
When a Container, like a Frame, is painted, all of its Components (Buttons, TextFields,
whatever) must be repainted. This is accomplished (roughly), in Java, by sending repaint() to
every Component in the Container, in the order they were added to the container.
The action of repaint() is to spawn a new Thread (see Chapter 9, Simple Java), which
schedules update(Graphics) in 100 milliseconds. If another repaint()happens before the 100
milliseconds elapses, the previous update() is cancelled (since screen flicker is ugly and
refreshing over and over ( like for every line in an image coming down down from the Net)
looks horrible), and a new one is scheduled.
7a)What is AWT? what are the different types of controls available in AWT?
JAVA PROGRAMMING