0% found this document useful (0 votes)
23 views

Java 5th UNIT (Chapter-1)

java
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views

Java 5th UNIT (Chapter-1)

java
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

JAVA PROGRAMMING

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:

● An applet is a Java class that extends the java.applet.Applet class.

● A main() method is not invoked on an applet, and an applet class will not define main().

● Applets are designed to be embedded within an HTML page.

● 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.

Applet Method Summary

These methods should be overridden by the custom applet


Overridable
class in order to do anything useful. The default behavior for
these methods is to do nothing.

Called by the browser or applet viewer to inform this applet


void init()
that it has been loaded into the system.

Called by the browser or applet viewer to inform this applet


void start()
that it should start its execution.

Called by the browser or applet viewer to inform this applet


void stop()
that it should stop its execution.

Called by the browser or applet viewer to inform this applet


void destroy() that it is being reclaimed and that it should destroy any
resources that it has allocated.

String getAppletInfo() Returns information about this applet.

String[][] Returns information about the parameters than are


getParameterInfo() understood by this applet.

void paint(Graphics g)

Methods that are known to be usefully overridden, but you


Semi-overridable
usually do not have to do so.

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

and double buffering graphics.

Call these methods to learn about the environment in which


Informational
the applet is running.

AccessibleContext
Gets the AccessibleContext associated with this Applet.
getAccessibleContext()

Determines this applet's context, which allows the applet to


AppletContext query and affect the environment in which it runs. For
getAppletContext() example, you'll need to know the applet context in order to
make the web browser go to another web page.

URL getCodeBase() Gets the base URL.

Returns an absolute URL naming the directory of the


URL getDocumentBase()
document in which the applet is embedded.

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)

Determines if this applet is active.An applet is marked active


boolean isActive() just before its start method is called. It becomes inactive just
before its stop method is called.

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


getAudioClip(URL url) argument.
JAVA PROGRAMMING

AudioClip
Returns the AudioClip object specified by the URL and
getAudioClip(URL url,
name arguments.
String name)

Color getBackground()

void setBackground(Color Defines the Background color property.


x)

Color getForeground()

void setForeground(Color Defines the Foreground color property.


x)

Returns an Image object that can then be painted on the


Image getImage(URL url)
screen.

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 play(URL url)


Plays the audio clip at the specified absolute URL, or a
void play(URL url, String
relative URL and a specifier that is relative to it.
name)

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)

void repaint(milli, x,y,w,h)

Dimension size()[JDK 1.0]

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

appletviewer does not. You should not overuse this method.

public void init()

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.

The default implementation of this method does nothing.

public void start()

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.

The default implementation of this method does nothing.

public void stop()

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.

The implementation of this method does nothing.


JAVA PROGRAMMING

public void destroy()

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.

The default implementation of this method does nothing.

public String getAppletInfo()

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.

public String[][] getParameterInfo()

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"}
};

The default implementation of this method returns null.

Life Cycle of an Applet:


Four methods in the Applet class give you the framework on which you build any serious
applet:

● 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:

// import necessary packages


import java.applet.*;
import java.awt.*;

// Inherit the applet class from the class Applet


public class FirstApplet extends Applet
{

// Now you should implement the following methods

// 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

public void destroy {... }

/** 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) { }
}

An Example applet Program:

import java.applet.*;

import java.awt.*;

public class HelloWorldApplet extends Applet

public void paint (Graphics g)

g.drawString ("Hello World", 25, 50);

repaint(), paint() and update()

Many Java programmers are befuddled by the three methods repaint(),paint(Graphics),


and update(Graphics). This is because they are designed to work in a wide variety of
circumstances, and they interact in a non-obvious fashion. This happens in several contexts in
Java, but GUIs are the most obvious. The designers of Java wanted Java programs to be able
to run on any machine that had a Java VM. So, a particular program might be running on a
desktop machine, or a laptop, or a hand-held machine, like a personal assistant or a phone.
This presents quite a challenge for the designer of an abstract windows toolkit (AWT). It also
makes the job of a novice programmer more difficult than it might otherwise be. So it goes.
JAVA PROGRAMMING

i) public void update(Graphics)


By default update(Graphics) fills the drawable area of a Component with its background
color, and then sends paint(Graphics) to the object. Thus, flickerthat comes from redrawing
the background over and over, can sometimes be fixed by overriding update() (see Code
Example 9 on page 238, Simple Java).

ii)public void paint(Graphics)


Every Java Component implements paint(Graphics), which is responsible for painting that
component in the Graphics context passed in the parameter. When you extend a Component
(like when you write a Applet), if you want to display it differently than its superclass, you
override public void paint(Graphics) . This was first illustrated in Chapter 3, Simple Java.

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.

iv) paint(Graphics) or repaint()?


When you wish to redisplay a component, should you send it paint(Graphics)or repaint()?
The answer is almost always repaint(). Only use paint(Graphics)if you understand what
you are doing and have a good reason.

7a)What is AWT? what are the different types of controls available in AWT?
JAVA PROGRAMMING

You might also like