Java Applet
Java Applet
Java Lecture-60
Topic: Chapter-10 Applet
Applets are small programs that are primarily used in Internet computing.
They can be transported over the Internet from one computer to another and
run using the AppletViewer or any Web browser that supports Java.
An applet, like any application program, can do many things for us. It can
perform arithmetic operations, display graphics, play sounds, accept user
input, create animation etc.
Applet Basics:
Before Java, you used HTML to describe the layout of a web page. The basic idea
of how to use applets in a web page is simple, the HTML page must tell the
browser which applets to load and then where to put each applet on the web
page. As you might expect, the tag needed to use an applet must tell the browser
the following.
From where to get the class files.
How the applet sits on the web page. (size, location etc)
The browser then retrieves the class files from the Net ( or from the directory on
the user’s machine) and automatically runs the applet, using its Java Virtual
Machine.
All these restrictions and limitations are placed in the interest of security of
systems. These restrictions ensure that an applet cannot do any damage to the
local system.
Object
Component
Container
Window Panel
`
Frame Applet
As above mentioned, an applet is a Java class that runs in the web browser or
appletviewer.
Running the Applet:
To execute the applet, you need to carry out two steps.
Compile your source files into class files.
And then type Appletviewer <AppletCode.java> on command prompt and hit
enter key to start applet.
A Web page basically made up of text and HTML tags that can be interpreted
by a Web browser or an applet viewer.
Web pages are stored using a file extension .html such as MyApplet.html.
Such files are referred as HTML files. HTML files should be stored in the same
directory as the compiled code of the applets.
Applet tag:
The <APPLET> tag supplies the name of the applet to be loaded and tells the
browser how much space the applet requires. So test the above applet program,
we have to write the following
Hello.html file.
<HTML>
<APPLET CODE=”HelloWorldApplet.class” WIDTH=300 HEIGHT=300>
</APPLET>
</HTML>
Before viewing the applet in a browser, it is a good idea to test it in the
appletviewer program that is a part of the Java SDK. To use the applet viewer in
our example, enter
appletviewer Hello.html
at the command line.
The HelloWorldApplet implements just one method, the paint method. Most
applets need to handle the paint event.
This event occurs whenever a part of applet’s visible area is uncovered and
needs to be drawn again.
The paint method is passed a Graphics object which we have chosen to call g.
The Graphics class is defined in the java.awt.Graphics package which we have
imported.
Within the paint method we call Graphics class’s drawString( ) method to
draw the string “Hello World” at the coordinate (50,25). That’s 50 pixels across
and twenty five pixels down from the upper left hand corner of the applet.
Java Lecture-61
Topic: Methods of Graphics Class
The Graphics Class:
Java’s Graphics class includes methods for drawing many different types of
shapes, from simple lines to polygons to text in a variety of fonts.
To draw a shape on the screen, we may call one of the methods available in
the Graphics class.
All the drawing methods have arguments representing end points, corners, or
starting locations of a shape as values in the Frame or Applet coordinate
system.
To draw a shape, we only need to use the appropriate method with the
required arguments. Following is the list of Graphics class methods.
Method Description
clearRect(int x, int y, int width, int height) Clears the specified rectangle by
filling it with the background color
of the current drawing surface.
copyArea(int x,int y,int w, int height, int dx, Copies an area of the component
int dy) by a distance specified by dx and
dy.
drawArc(int x, int y, int width, int height, int Draws the outline of a circular or
startAngle, int arcAngle) elliptical arc covering the
specified rectangle.
drawLine(int x1, int y1, int x2, int y2) Draws a line, using the current
color, between the points (x1, y1)
and (x2, y2) in this graphics
context's coordinate system.
drawOval(int x, int y, int width, int height) Draws the outline of an oval.
Java Lecture-62
Topic: Draw a Polygon in Java Applet
Polygon is a closed figure with finite set of line segments joining one vertex
to the other.
The polygon comprises of set of (x, y) coordinate pairs where each pair is the
vertex of the polygon.
The side of the polygon is the line drawn between two successive coordinate
pairs and a line segment is drawn from the first pair to the last pair.
Syntax:
drawPolygon(int[] xPoints, int[] yPoints, int numOfPoints);
import java.applet.Applet;
import java.awt.Graphics;
(100,50) (200,50)
1 2
(250,150) 6 3 (250,150)
5 4
(100,250) (200,250)
import java.applet.Applet;
import java.awt.Graphics;
Output
Output
Course: Java Programming, Prepared By: Atul Kabra, 9422279260
Java Lecture-63
Topic: Color and Font
Color:
We can also change the draw of fill color using setColor() method of Graphics
class.
Syntax:
void setColor (Color clr)
To set color we need to pass object of Color class which is declared in AWT
package. There are two methods to pass this object
(i) By calling color class constructor
Color (int red, int green, int blue)
Course: Advanced Java, Info Planet Programming Classes Prepared By: Atul Kabra, 9579460114
Course: Java Programming, Prepared By: Atul Kabra, 9422279260
g.setColor(Color.YELLOW);
g.fillOval(50,180,100,100);
}
}
Font:
Course: Advanced Java, Info Planet Programming Classes Prepared By: Atul Kabra, 9579460114
Course: Java Programming, Prepared By: Atul Kabra, 9422279260
We can also change the font of text using setFont() method of Graphics class.
Syntax:
void setFont (Font font)
To set font we need to pass the object of Font class which is declared in AWT
package. The constructor of Font class takes three arguments.
Font(String fontName, int style , int size)
import java.awt.*;
import java.applet.*;
g.setFont(new Font("Arial",Font.BOLD,30));
Course: Advanced Java, Info Planet Programming Classes Prepared By: Atul Kabra, 9579460114
Course: Java Programming, Prepared By: Atul Kabra, 9422279260
g.setFont(new Font("Arial",Font.BOLD+Font.ITALIC,30));
g.drawString("Font Demo", 50,150);
}
}
Course: Advanced Java, Info Planet Programming Classes Prepared By: Atul Kabra, 9579460114
Course: Java Programming, Prepared By: Atul Kabra, 9422279260
Java Lecture-64
Topic: Applet Life Cycle
Applet lifecycle :
Every java applet inherits a set of default behavior from the applet class. As a
result when can applet is loaded it undergoes a series of changes in its state as
shown in the following figure-
Begin
(Load Applet) init ( )
Born
start ( )
paint ( ) stop ( )
Running Idle
start ( )
destroy ( )
End
Four method in applet class gives the framework on which we can build any
applet.
(i) init ( ) {initialization state}
This method is used for whatever initialization is needed for your applet. It is
automatically called by the browser when java creates the object for the first
time. The initialization occurs only once in the applet lifecycle.
Course: Java Programming, Prepared By: Atul Kabra, 9422279260
Java Lecture-65
Topic: Passing Information to Applet
As you seen, the CODE attributes gives the name of the class file and must
include the .class extension.
The WIDTH and HEIGHT attributes size the window that will hold the applet.
The text between the <APPLET> and </APPLET> tags is displayed only if the
browser cannot show applets.
These tags are required. If any are missing, the browser cannot load your
applet.
CODEBASE
This optional attribute tells the browser that your class files are found below
the directory indicated in the CODEBASE attribute.
WIDTH, HEIGHT
These attributes are required and give the width and height of the applet,
measured in pixels.
ALIGN
This attributes specified the alignment of the applet. Possible values are as follows
Course: Java Programming, Prepared By: Atul Kabra, 9422279260
You then pick up the value of the parameter, using the getParameter ( ) method
of the Applet class, as in the following example
Example: Write an applet to accept username in the form of parameter and print
“Hello <username>” in the Applet.
Course: Java Programming, Prepared By: Atul Kabra, 9422279260
1) hello.html
<HTML>
<APPLET CODE="MyApplet.class" WIDTH=400 HEIGHT=400>
<param name="username" value="Atul">
</APPLET>
</HTML>
2) MyApplet.java
import java.applet.Applet;
import java.awt.*;
}
}
Course: Java Programming, Prepared By: Atul Kabra, 9422279260
String [] fontNames=GraphicsEnvironment.getLocalGraphicsEnvironment().
getAvailableFontFamilyNames()
import java.awt.*;
public class ListOfFonts
{
Output: