Java Notes Unit V
Java Notes Unit V
Applets are small Java programs that are primarily used in internet computing. They
can be transported over the internet from one computer to another and run using
the Applet Viewer. It can perform arithmetic operations, display graphics, play
sounds, accept user input, create animations.
1.Types of Applets:
There are two types of applets in java:
i) Local Applet
ii) Remote Applet
i) Local Applet:
An applet developed locally and stored in a local system is known as a local applet.
When a web page is trying to find a local applet, it does not need to use the internet
and therefore the local system does not require the internet connection.
Applet enters the initialization state when it is first loaded. This is achieved by
calling the init() method of the Applet class. The initialization occurs only once in
the applet's life cycle.
2. Running State:
The start() method contains the actual code of the applet that should run. The
start() method executes immediately after the init() method. It also executes
whenever the applet is restored, maximized or moving from one tab to another tab
in the browser.
The stop() method stops the execution of the applet. The stop() method executes
when the applet is minimized or when moving from one tab to another in the
browser.
4. Dead State: The destroy() method executes when the applet window is closed
or when the tab containing the web page is closed. The stop() method executes just
before when destroy() method is invoked. The destroy() method removes the
applet object from memory.
public void destroy(){
.........
......... (action)
}
5. Display State:
The paint() method is used to redraw the output on the applet display area. The
paint() method executes after the execution of start() method and whenever the
applet or browser is resized.
Graphics Programming:-
Java Graphics class:-
To perform graphics like shapes, lines, textbox, label, checkbox, frames, and menu
bars in java, the Graphics class is used. The Graphics class defines a number of
drawing functions, Each shape can be drawn edge-only or filled. To draw shapes
on the screen, we may call one of the methods available in the Graphics class. The
most commonly used drawing methods included in the graphics class are listed
below.1. Lines:
The simplest shape we can draw with the graphics class is a line. The drawLine()
method takes two pairs of coordinates, (x1, y1) and (x2, y2) as arguments and
draws a line between them. The g is the Graphics object passed to the paint()
method.
syntax:
// Lines.java
//Drawing Lines
import java.awt.*;
import java.applet.*;
//Lines.html
<html>
<head>
<applet code=Lines.class
width=300 Height=250>
</applet>
</head>
</html>
2. Rectangle:
The drawRect() and fillRect() methods display an outlined and filled rectangle,
respectively. The upper-left corner of the rectangle is at(top, left). The dimensions
of the rectangle are specified by width and height. Use drawRoundRect() or
fillRoundRect() to draw a rounded rectangle. A rounded rectangle has rounded
corners.
syntax:
Example:
//Rectangle.java
import java.awt.*;
import java.applet.*;
//Rectangle.html
<html>
<head>
<applet code=Rectangle.class width=300 Height=250>
</applet>
</head>
</html>
3. Circles and Ellipses:
The Graphics class does not contain any method for circles or ellipses. To draw an
ellipse, use drawOval(). To fill an ellipse, use fillOval(). To draw a circle, specify a
square as the bounding rectangle i.e get height = width. The ellipse is drawn within
a bounding rectangle whose upper-left corner is specified by (top, left) and whose
width and height are specified by width and height.
syntax:
Example:
//Ellipses.java
import java.awt.*;
import java.applet.*;
An arc is a part of the oval. Arcs can be drawn with draw Arc() and fillArc()
methods. The arc is bounded by the rectangle whose upper-left corner is specified
by (top, left) and whose width and height are specified by width and height. The
arc is drawn from the start Angle through the angular distance specified by the
sweep Angle. Angles are specified in degree. The arc is drawn counterclockwise if
sweep Angle is positive, and clockwise if sweet Angle is negative.
syntax:
void drawArc(int top, int left, int width, int height, int startAngle, int sweetAngle);
void fillArc(int top, int left, int width, int height, int startAngle, int sweetAngle);
Example:
//Arcs.java
import java.awt.*;
import java.applet.*;
Polygons are shapes with many sides. It may be considered a set of lines
connected. Use drawPolygon() and fillPolygon() to draw arbitrarily shaped figures.
The polygon's endpoints are specified by the coordinate pairs contained within the
x and y arrays. The number of points defined by x and y is specified by numPoints.
Obviously, x and y arrays should be of the same size and we must repeat the first
point at the end of the array to close the polygon.
syntax:
Example:
//Polygon.java
import java.awt.*;
import java.applet.*;
//Polygon.html
<html>
<head>
<applet code=Polygon.class
width=300 Height=250>
</applet>
</head>
</html>