--------------------------------------------------------------------------------------------------------------------------
Prepared By : Uday Shah (HOD - IT)
Contact No : 7600044051
E-Mail : rupareleducation@gmail.com
Programming With Java Unit 4
Introduction to
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.
Life Cycle of
an Applet
Four methods in the Applet class
gives 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.
·
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.
A "Hello,
World" Applet
Following is a simple applet
named HelloWorldApplet.java −
import java.applet.*;
import java.awt.*;
public class HelloWorldApplet extends Applet
{
public void paint (Graphics g)
{
g.drawString ("Hello World", 25, 50);
}
}
Parameter in Applet
We can get any
information from the HTML file as a parameter. For this purpose, Applet class
provides a method named getParameter(). Syntax:
public String getParameter(String parameterName)
Example of using parameter in
Applet:
import java.applet.Applet;
import java.awt.Graphics;
public class UseParam extends Applet
{
public void paint(Graphics g)
{
String str=getParameter("msg");
g.drawString(str,50, 50);
}
}
myapplet.html
<html>
<body>
<applet code="UseParam.class" width="300" height="300">
<param name="msg" value="Welcome to applet">
</applet>
</body>
</html>
Graphics Class
The Graphics class is the
abstract super class for all graphics contexts which allow an application to
draw onto components that can be realized on various devices, or onto
off-screen images as well.
A Graphics object encapsulates
all state information required for the basic rendering operations that Java
supports.
1.
public abstract void drawString(String
str, int x, int y): is used to draw the specified string.
2.
public void drawRect(int x, int y, int
width, int height): draws a rectangle with the specified width and
height.
3.
public abstract void fillRect(int x, int
y, int width, int height): is used to fill rectangle
with the default color and specified width and height.
4.
public abstract void drawOval(int x, int
y, int width, int height): is used to draw oval with
the specified width and height.
5.
public abstract void fillOval(int x, int
y, int width, int height): is used to fill oval with
the default color and specified width and height.
6.
public abstract void drawLine(int x1,
int y1, int x2, int y2): is used to draw line
between the points(x1, y1) and (x2, y2).
7.
public abstract void setColor(Color c): is
used to set the graphics current color to the specified color.
8.
public abstract void setFont(Font font): is
used to set the graphics current font to the specified font.
import java.applet.Applet;
import java.awt.*;
public class GraphicsDemo extends Applet
{
public void paint(Graphics g)
{
g.setColor(Color.red);
g.drawString("Welcome",50, 50);
g.drawLine(20,30,20,300);
g.drawRect(70,100,30,30);
g.fillRect(170,100,30,30);
g.drawOval(70,200,30,30);
g.setColor(Color.pink);
g.fillOval(170,200,30,30);
g.drawArc(90,150,30,30,30,270);
g.fillArc(270,150,30,30,0,180);
}
}
Layout Manager
The layout manager automatically
positions all the components within the container. If we do not use layout
manager then also the components are positioned by the default layout manager.
It is possible to layout the controls by hand but it becomes very difficult
because of the following two reasons.
·
It is very tedious to handle a large
number of controls within the container.
·
Oftenly the width and height information
of a component is not given when we need to arrange them.
Java provide us with various
layout manager to position the controls. The properties like size, shape and
arrangement varies from one layout manager to other layout manager. When the
size of the applet or the application window changes the size, shape and
arrangement of the components also changes in response i.e. the layout managers
adapt to the dimensions of appletviewer or the application window.
The layout manager is associated
with every Container object. Each layout manager is an object of the class that
implements the LayoutManager interface.
1
|
The borderlayout arranges
the components to fit in the five regions: east, west, north, south and
center. Each region is can contain only one component and
each component in each region is identified by the corresponding constant
NORTH, SOUTH, EAST, WEST, and CENTER.
|
2
|
The CardLayout object
treats each component in the container as a card. Only one card is visible at
a time. and the container acts as a stack of cards.
|
3
|
The FlowLayout is the
default layout.It layouts the components in a directional flow.
|
4
|
The GridLayout manages the
components in form of a rectangular grid.
|
5
|
This is the most flexible
layout manager class.The object of GridBagLayout aligns the component
vertically,horizontally or along their baseline without requiring the
components of same size.
Gridx , Gridy
Specify the row and column at the upper left of the component. The
leftmost column has address
gridx=0 , and the top row has
address gridy=0 . Gridwidth , Gridheight
Specify the number of columns (for
gridwidth ) or rows
(for gridheight ) in the
component's display area. Fill
Used when the component's display area is larger than the component's
requested size to determine whether and how to resize the component.
Ipadx , Ipady
Specifies the internal padding: how much to add to the minimum size
of the component. The default value is zero.
Insets
Specifies the external padding of the component the minimum amount of space between the
component and the edges of its display area.
Anchor
Used when the component is smaller than its display area to determine
where (within the area) to place the component.
Weightx , Weighty
Specifying weights is an
art that can have a significant impact on the appearance of the components a
GridBagLayout controls. Weights are used to determine how to distribute space
among columns (
weightx ) and among rows (weighty ); |
6
|
Java BoxLayout
The BoxLayout is used to
arrange the components either vertically or horizontally. For this purpose,
BoxLayout provides four constants.
The BoxLayout manager is constructed with an axis
parameter that specifies the type of layout that will be done. There are four
choices:
X_AXIS -
Components are laid out horizontally from left to right.
Y_AXIS -
Components are laid out vertically from top to bottom.
LINE_AXIS -
Components are laid out the way words are laid out in a line, based on the
container's ComponentOrientation property.
PAGE_AXIS -
Components are laid out the way text lines are laid out on a page, based on
the container's ComponentOrientation property.
|
7
|
Java
SpringLayout
A SpringLayout arranges
the children of its associated container according to a set of constraints.
Constraints are nothing but horizontal and vertical distance between two
component edges. Every constrains are represented by a
SpringLayout.Constraint object.
Each child of a
SpringLayout container, as well as the container itself, has exactly one set of
constraints associated with them.
Each edge position is
dependent on the position of the other edge. If a constraint is added to
create new edge than the previous binding is discarded. SpringLayout doesn't
automatically set the location of the components it manages.
|
8
|
GroupLayout
GroupLayout groups its
components and places them in a Container hierarchically. The
grouping is done by instances of the Group class.
Group is an abstract class
and two concrete classes which implement this Group class are SequentialGroup
and ParallelGroup.
SequentialGroup positions
its child sequentially one after another where as ParallelGroup aligns its
child on top of each other.
The GroupLayout class
provides methods such as createParallelGroup() and createSequentialGroup() to
create groups.
GroupLayout treats each
axis independently. That is, there is a group representing the horizontal
axis, and a group representing the vertical axis. Each component must exists
in both a horizontal and vertical group, otherwise an IllegalStateException
is thrown during layout, or when the minimum, preferred or maximum size is
requested.
|
9
|
Using NO LAYOUT Manager (Absolute Manager)
Although it is possible to do without a layout
manager, you should use a layout manager if at all possible. A layout manager
makes it easier to adjust to look-and-feel-dependent component appearances,
to different font sizes, to a container's changing size, and to different
locales. Layout managers also can be reused easily by other containers, as
well as other programs.
We know that in Java Layout Managers that two styles of placing components in
containers – pixel format and position format.
We also know that, the default layout manager for
Frame is BorderLayout and for Panel and
Applet, it is FlowLayout. To use pixel format, first
we must set the default layout manager of the container to null. To give the size and
location, setBounds() method
is used.
|