Chp.5 Java Applets and Graphics Programming-converted
Chp.5 Java Applets and Graphics Programming-converted
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.
Advantage of Applet
There are many advantages of applet. They are as follows:
• It works at client side so less response time.
• Secured
• It can be executed by browsers running under many plateforms, including Linux,
Windows, Mac Os etc.
Drawback of Applet
• Plugin is required at client browser to execute applet.
5.1.1 Local and Remote Applets
A local applet is the one that is stored on our own computer system. When the Web-page has
to find a local applet, it doesn't need to retrieve information from the Internet. A local applet is
specified by a path name and a file name as shown below in which the codebase attribute
specifies a path name, whereas the code attribute specifies the name of the byte-code file that
contains the applet's code.
<applet codebase="MyAppPath" code="MyApp.class" width=200 height=200> </applet>
A remote applet is the one that is located on a remote computer system . This computer system
may be located in the building next door or it may be on the other side of the world. No matter
where the remote applet is located, it's downloaded onto our computer via the Internet. The
browser must be connected to the Internet at the time it needs to display the remote applet. To
reference a remote applet in Web page, we must know the applet's URL (where it's located on
the Web) and any attributes and parameters that we need to supply. A local applet is specified
by a url and a file name as shown below.
• Once you've successfully completed these steps, you should see something like this in
the browser window:
5.1.4 Applet Life Cycle(Skeleton)
As shown in the above diagram, the life cycle of an applet starts with init() method and ends
with destroy() method. Other life cycle methods are start(), stop() and paint(). The methods to
execute only once in the applet life cycle are init() and destroy(). Other methods execute
multiple times.
Below is the description of each applet life cycle method:
init(): The init() method is the first method to execute when the applet is executed. Variable
declaration and initialization operations are performed in this method.
start(): 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.
stop(): 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.
destroy(): The destroy() method executes when the applet window is closed or when the tab
containing the webpage is closed. stop() method executes just before when destroy() method is
invoked. The destroy() method removes the applet object from memory.
paint(): 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.
The method execution sequence when an applet is executed is:
• init()
• start()
• paint()
5.1.5 Applet HTML Tag(Adding Applet to HTML file/ embedding <applet> tags in java code)
(Answer will be same)
The <applet> tag in HTML was used to embed Java applets into any HTML document.
Syntax:
<applet attribute1 attribute2....>
<param parameter1>
<param parameter2>
....
</applet>
Attributes: The <applet> tag takes a number of attributes, with one of the most important
being the code attribute. This code attribute is used to link a Java applet to the concerned
HTML document. It specifies the file name of the Java applet.
<!DOCTYPE html>
<html>
<applet code="HelloWorld" width=200 height=60>
</applet>
</html>
Here, HelloWorld is the class file, which contains the applet. The width and height attributes
determine the width and height of the applet in pixels when it is opened in the browser.
Attributes available to be used in conjunction with the <applet> tag are as follows:
Attribute
Values Remarks
Name
left
right
top
align Specifies the alignment of an applet.
bottom
middle
baseline
Parameters: Parameters are quite similar to command-line arguments in the sense that they
provide a way to pass information to the applet after it has started. All the information
available to the applet before it starts is said to be hard-coded i.e. embedded within it.
Parameters make it possible to generate and use data during run-time of the applet.
Syntax:
<param name=parameter_name value=parameter_value>
The name assigned to the name attribute of the param tag is used by the applet code as a
variable to access the parameter value specified in the value attribute. In this way, the applet is
able to interact with the HTML page where it is embedded, and can work on values provided to
it by the page during run-time.
<!DOCTYPE html>
<html>
<applet code="HelloWorld" width=200 height=60>
<param name="message" value="HelloWorld">
</applet>
</html>
In this piece of code, the applet file HelloWorld can use the variable named message to access
the value stored in it, which is “HelloWorld”.
Param Tag
The <param> tag is a sub tag of the <applet> tag. The <param> tag contains two
attributes: name and value which are used to specify the name of the parameter and the value
of the parameter respectively. For example, the param tags for passing name and age
parameters looks as shown below:
<param name=”name” value=”Ramesh” />
<param name=”age” value=”25″ />
Now, these two parameters can be accessed in the applet program using the getParameter()
method of the Applet class.
getParameter() Method
The getParameter() method of the Applet class can be used to retrieve the parameters passed
from the HTML page. The syntax of getParameter() method is as follows:
Let’s look at a sample program which demonstrates the <param> HTML tag and
the getParameter() method:
import java.awt.*;
import java.applet.*;
public class MyApplet extends Applet
{
String n;
String a;
public void init()
{
n = getParameter("name");
a = getParameter("age");
}
public void paint(Graphics g)
{
g.drawString("Name is: " + n, 20, 20);
g.drawString("Age is: " + a, 20, 40);
}
}
HTML code:
<html>
<head>
<title> parameter passing in Applet</title>
</head>
<body>
<applet code="MyApplet" height="300" width="500">
<param name="name" value="Ramesh" />
<param name="age" value="25" />
</applet>
</body>
</html>
Output:
}
}
myapplet.html
<html>
<body>
<applet code="GraphicsDemo.class" width="300" height="300">
</applet>
</body> </html>
5.2.2 Drawing Lines
Lines are drawn by means of drawLine() method, shown here:
Void drawLine(int startX, int startY, int endX, int endY)
drawLine() displays a line in the current drawing color that begins at startX,startY and endX,
endY.
Applet Program:
import java.awt.*;
import java.applet.*;
public class Lines extends Applet{
public void paint(Graphics g)
{
g.drawLine(0,0,100,100);
g.drawLine(0,100,100,0);
g.drawLine(40,25,250,180);
g.drawLine(75,90,400,400);
g.drawLine(20,150,400,40);
g.drawLine(5,290,80,19);
}
}
HTML CODE:
<html>
<head>
<title>Drawing Lines</title>
</head>
<body>
<applet code="Lines.class" width=300 height=200>
</applet>
</body>
</html>
OUTPUT:
5.2.3 Drawing Rectangles
• The drawRect() and fillRect() methods display an outlined andd filled rectangle.
void drawRect(int top, int left, int width, int height)
void fillRect(int top, int left, int width, int height)
• To draw a rounded rectangle use,
void drawRoundRect(int top, int left, int width, int height, int xDim, int yDim)
void fillRoundRect(int top, int left, int width, int height, int xDim, int yDim)
Applet Code:
import java.awt.*;
import java.applet.*;
public class rectangle extends Applet
{
public void paint(Graphics g)
{
g.drawRect(10,10,60,50);
g.fillRect(100,10,60,50);
g.drawRoundRect(190,10,60,50,15,15);
g.fillRoundRect(70,90,140,100,30,40);
}
}
HTML Code:
<html>
<head>
<title>Drawing Rectangles</title>
</head>
<body>
<applet code="rectangle.class" width=600 height=400>
</applet>
</body>
</html>
OUTPUT:
5.2.4 Drawing Ellipses and Circles
• To draw an ellipse, use drawOval(). To fill an ellipse, use fillOval().These methods are
shown here:
void drawOval(int top, int left, int width, int height)
void fillOval(int top, int left, int width, int height)
• To draw a circle, width and height must be same.
• Applet code:
import java.awt.*;
import java.applet.*;
public class ellipseDraw extends Applet
{
public void paint(Graphics g)
{
g.drawOval(10,10,50,50);
g.fillOval(100,10,75,50);
g.drawOval(190,10,90,30);
g.fillOval(70,90,140,100);
}
}
HTML Code:
<html>
<head>
<title>Drawing Ellipse and circle</title>
</head>
<body>
<applet code="ellipseDraw.class" width=300 height=200>
</applet>
</body>
</html>
OUTPUT:
5.2.5 Drawing Arcs
• Arcs can be drawn with drawArc() and fillArc(), shown here:
void drawArc(int top, int left, int width, int height, int startAngle, int sweepAngle)
void fillArc(int top, int left, int width, int height, int startAngle, int sweepAngle)
• The arc is drawn from startAngle through the angular distance specified by sweepAngle.
• Angles are specified in degrees. Zero degrees is on horizontal.
• The arc is anticlockwise if sweepAngle is positive and clockwise if sweepAngle is
negative.
Applet Code:
import java.applet.*;
import java.awt.*;
public class arcDraw extends Applet
{
public void paint(Graphics g)
{
g.drawArc(10,40,70,70,0,75);
g.fillArc(100,40,70,70,0,75);
g.drawArc(10,100,70,80,0,175);
g.drawArc(100,100,70,80,0,-175);
}
}
HTML Code:
<html>
<head>
<title>Drawing arcs</title>
</head>
<body>
<applet code="arcDraw.class" width=300 height=200>
</applet>
</body>
</html>
OUTPUT:
5.2.6 Drawing Polygons:
• It is possible to draw arbitrarily shaped figures using drawPolygon() and fillPolygon(),
shown here:
void drawPolygon(int x[], int y[], int numPoints)
void fillPolygon(int x[], int y[], int numPoints)
• The polygon’s endpoints are specified by the coordinate pairs contained within x and y
arrays. The number of points defined by x and y is specified by numPoints.
Applet Code:
import java.awt.*;
import java.applet.*;
public class polygon extends Applet
{
public void paint(Graphics g)
{
int xpoints[]={30,200,30,250,30};
int ypoints[]={30,30,250,200,30};
int num=5;
g.drawPolygon(xpoints,ypoints,num);
}
}
HTML Code:
<html>
<head>
<title>Drawing polygons</title>
</head>
<body>
<applet code="polygon.class" width=300 height=200>
</applet>
</body>
</html>
OUTPUT:
5.2.7. Program to draw smiling face.
Applet Code:
import java.awt.*;
import java.applet.*;
public class smilingface extends Applet
{
public void paint(Graphics g)
{
g.drawOval(40,40,120,150);
g.drawOval(57,75,30,20);
g.drawOval(110,75,30,20);
g.fillOval(68,81,10,10);
g.fillOval(121,81,10,10);
g.drawOval(85,100,30,30);
g.fillArc(60,125,80,40,180,180);
g.drawOval(25,92,15,30);
g.drawOval(160,92,15,30);
}
}
HTML Code:
<html>
<head>
<title>Drawing smiling face</title>
</head>
<body>
<applet code="smilingface.class" width=300 height=200>
</applet>
</body>
</html>
OUTPUT:
5.2.8 Using control loops in applet
Applet Code:
import java.awt.*;
import java.applet.*;
public class printnum extends Applet
{
int x,y;
public void init()
{
x=50;
y=50;
}
public void paint(Graphics g)
{
for(int i=1; i<=10; i++)
{
g.drawString(i+" ",x, y);
x=x+10;
}
}
}
HTML Code:
<html>
<head>
<title>Printing numbers using control loop </title>
</head>
<body>
<applet code="printnum.class" width=300 height=200>
</applet>
</body>
</html>
OUTPUT:
5.2.9 Color methods: setColor(), getColor(), setForeGround(), setBackGround()
Java provides methods and behaviors for dealing with color in general through the Color class,
and also provides methods for setting the current foreground and background colors so that
you can draw with the colors you created.
Java's abstract color model uses 24-bit color, wherein a color is represented as a combination of
red, green, and blue values. Each component of the color can have a number between 0 and
255. 0,0,0 is black, 255,255,255 is white, and Java can represent millions of colors between as
well.
Output:
String[ ] getAvailableFontFamilyNames( )
This method returns an array of strings that contains the names of the available font families.