0% found this document useful (0 votes)
4 views22 pages

Chp.5 Java Applets and Graphics Programming-converted

Uploaded by

Om Dighule
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
4 views22 pages

Chp.5 Java Applets and Graphics Programming-converted

Uploaded by

Om Dighule
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 22

Chapter 5.

Java Applets And Graphics Programming


5.1 Introduction to applets: Applet, Applet life cycle(skeleton),Applet tag, Adding Applet to
HTML file, passing parameter to applet, embedding <applet> tags in java code , adding controls
to applets.
5.2 Graphics Programming: Graphics classes, lines, rectangles,ellipse, circle,arcs, polygons,
color and fonts, setColor(), getColor(), setForeGround(), setBackGround(), font class, variable
defined by font class:name, pointSize, size,style,font methods: getFamily(), getFont(),
getFontname(), getSize(), getStyle(), getAllFonts() and get available font family name() of the
graphics environment class.
5.1 Introduction to applets
Applet basics:
An applet is a java program that runs on the web page. The java application is a program that
runs from command line prompt using java interpreter.
Applet is a special type of a program that is embedded in the webpage to generate dynamic
content.

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.

<applet codebase="http://www.apoorvacollege.com" code="MyApp.class" width=200


height=200> </applet>
5.1.2 How applets differ from applications?
Basis for comparison Applet Application
It is small program uses another An application is the programs
Basic application program for its executed on the computer
execution. independently.
main() method Do not use the main method Uses the main method for execution
Cannot run independently
Execution Can run alone but require JRE.
require API's (Ex. Web API).
Requires prior explicit installation on
Installation Prior installation is not needed
the local computer.
The files cannot be read and Applications are capable of performing
Read and write
write on the local computer those operations to the files on the
operation
through applet. local computer.
Communication with Cannot communicate with Communication with other servers is
other servers other servers. probably possible.
Applets cannot access files Can access any data or file available on
Restrictions
residing on the local computer. the system.
Requires security for the system
Security No security concerns are there.
as they are untrusted.

5.1.3 Creating an Executable Applet(Hello World: Example)

Create a Java Source File


Create a file named HelloWorld.java with the Java code shown here:
import java.applet.Applet;
import java.awt.Graphics;

public class HelloWorld extends Applet {


public void paint(Graphics g) {
g.drawString("Hello world!", 50, 25);
}
}
Step 1: Import applet package and awt package
• To create an applet, you must import Applet class. This class is in java.applet package.
• We also need to import java.awt package. The ‘awt’ stands for “Absract Window
Toolkit”.
• The java.awt package includes classes for:
➢ Drawing lines and shapes
➢ Drawing letters
➢ Setting colors
➢ Choosing fonts etc.
Step 2: Extend the Applet class
• A class must be defined that inherits from class java.applet.Applet.
• The inherited class must be declared public.
• It contains the methods to paint to the screen and window.
Step 3: Override the paint method if you want to draw
• The paint method needs the Graphics object as its parameter.
• The syntax is:
public void paint(Graphics g) {……}
• public says that anyone can use this method. Void says that it does not return any
result. A Graphics is an object that holds information about a painting.
Step 4: Compiling the program
• Once you have saved your program, you need to compile it using Java compiler. At
command line, enter the command” javac HelloWorld.java”. This command will compile
your code so that you now have HelloWorld.class file. If you receive any error messages,
look back at the code and make necessary actions.
Step 5: Creating HTML document.
• Using a text editor, create a file named Hello.html in the same directory that contains
HelloWorld.class. This HTML file should contain the following text:
<HTML>
<HEAD>
<TITLE> A Simple Program </TITLE>
</HEAD>
<BODY>
Here is the output of my program:
<APPLET CODE="HelloWorld.class" WIDTH=150 HEIGHT=25>
</APPLET>
</BODY>
</HTML>
Step 6:Running an applet
• The applet can run in 2 ways
1. By using appletviewer(To run the appletviewer utility type appletviewer
filename.html)
2. By running in web browser(In the web browser bar give the full address of your
html file.)

• 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()

The method execution sequence when an applet is closed is:


• stop()
• destroy()
Example program that demonstrates the life cycle of an applet is as follows:
import java.awt.*;
import java.applet.*;
public class MyApplet extends Applet
{
public void init()
{
System.out.println("Applet initialized");
}
public void start()
{
System.out.println("Applet execution started");
}
public void stop()
{
System.out.println("Applet execution stopped");
}
public void paint(Graphics g)
{
System.out.println("Painting...");
}
public void destroy()
{
System.out.println("Applet destroyed");
}
}
Output of the above applet program when run using appletviewer tool is:
Applet initialized
Applet execution started
Painting…
Painting…
Applet execution stopped
Applet destroyed

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

alt text Specifies an alternate text for an applet


archive URL Specifies the location of an archive file
border pixels Specifies the border around the applet panel
Specifies a relative base URL for applets specified
codebase URL
in the code attribute
height pixels Specifies the height of an applet
hspace pixels Defines the horizontal spacing around an applet
Any arbitrary value Indicates whether the Java applet is allowed to
mayscript
(conventionally “mayscript”) access the scripting objects of the web page
name name Defines the name for an applet (to use in scripts)
vspace pixels Defines the vertical spacing around an applet
width pixels Specifies the width of an applet

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

5.1.6 Passing parameter to Applet


Parameters specify extra information that can be passed to an applet from the HTML page.
Parameters are specified using the HTML’s param tag.

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:

5.2 Graphics Programming


• One of the important feature of java is its ability to draw graphics. We can write java
applets that draw line, different shapes and text in different styles.
• Every applet has its own area of the screen known as canvas, where it creates its
display.
• The size of applet’s space is decided by the attribute of <applet> tag. Java’s coordinate
system has the origin(0,0) in upper left corner. Positive X values are to the right and
positive Y values are to the bottom. The value of X and Y are in pixel.
5.2.1 Graphics Class
• The Graphics class is the class used to allow a component to draw onto itself. The
Graphics class is located in java. awt package.
• Java’s Graphics class include methods for drawing many different shapes from lines to
polygons.
• To draw a shape on the screen we may call one of the methods available in Graphics
class.
Commonly used methods of Graphics class:
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 boolean drawImage(Image img, int x, int y, ImageObserver observer):
is used draw the specified image.
8. public abstract void drawArc(int x, int y, int width, int height, int startAngle, int
arcAngle): is used draw a circular or elliptical arc.
9. public abstract void fillArc(int x, int y, int width, int height, int startAngle, int
arcAngle): is used to fill a circular or elliptical arc.
10. public abstract void setColor(Color c): is used to set the graphics current color to the
specified color.
11. public abstract void setFont(Font font): is used to set the graphics current font to the
specified font.
Example of Graphics in applet:
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);

}
}

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.

Using Color Objects


To draw an object in a particular color, you must create an instance of the Color class to
represent that color. The Color class defines a set of standard color objects, stored in class
variables, to quickly get a color object for some of the more popular colors. For example,
Color.red returns a Color object representing red (RGB values of 255, 0, and 0), Color.white
returns a white color (RGB values of 255, 255, and 255), and so on.
Color Name RGB Value
Color.white 255,255,255
Color.black 0,0,0
Color.lightGray 192,192,192
Color.gray 128,128,128
Color.darkGray 64,64,64
Color.red 255,0,0
Color.green 0,255,0
Color.blue 0,0,255
Color.yellow 255,255,0
Color.magenta 255,0,255
Color.cyan 0,255,255
Color.pink 255,175,175
Color.orange 255,200,0
Table: Standard Colors
Color c = new Color(140,140,140);
This line of Java code creates a color object representing a dark gray. You can use any
combination of red, green, and blue values to construct a color object.
Alternatively, you can create a color object using three floats from 0.0 to 1.0:
Color c = new Color(0.55,0.55,0.55);
To draw an object or text using a color object, you have to set the current color to be that color
object, just as you have to set the current font to the font in which you want to draw. Use the
setColor() method (a method for Graphics objects) to do this:
g.setColor(Color.green);
After you set the current color, all drawing operations will occur in that color.
In addition to setting the current color for the graphics context, you can also set the
background and foreground colors for the applet itself by using the setBackground() and
setForeground() methods. Both of these methods are defined in the java.awt.Component class,
which Applet-and therefore your classes-automatically inherits.
The setBackground() method sets the background color of the applet, which is usually a light
gray (to match the default background of the browser). It takes a single argument, a Color
object:
setBackground(Color.white);
The setForeground() method also takes a single color as an argument, and it affects everything
that has been drawn on the applet, regardless of the color in which it has been drawn. You can
use setForeground() to change the color of everything in the applet at once, rather than having
to redraw everything:
setForeground(Color.black);
In addition to the setColor(), setForeground(), and setBackground() methods, there are
corresponding get methods that enable you to retrieve the current graphics color, background,
or foreground. Those methods are getColor() (defined in Graphics objects), getForeground()
(defined in Applet), and getBackground() (also in Applet). You can use these methods to choose
colors based on existing colors in the applet:
setForeground(g.getColor());
Example: A simple applet that sets the foreground and background colors and outputs a
string.
Applet Code:
import java.awt.*;
import java.applet.*;
public class colorsample extends Applet
{
String msg;
public void init()
{
setBackground(Color.cyan);
setForeground(Color.red);
msg="Inside init()--";
}
public void start()
{
msg=msg+"Inside start()--";
}
public void paint(Graphics g)
{
msg=msg+"Inside paint()";
g.drawString(msg, 10, 30);
}
}
HTML Code:
<html>
<head>
<title>Color Samples</title>
</head>
<body>
<applet code="colorsample.class" width=300 height=200>
</applet>
</body>
</html>

Output:

5.2.10 Working with Fonts


The AWT supports multiple type fonts. Fonts have a family name, a logical font name, and a face
name. The family name is the general name of the font, such as Courier. The logical name
specifies a name, such as Monospaced, that is linked to an actual font at runtime. The face name
specifies a specific font, such as Courier Italic.
The Font class defines these protected variables:
String name : Name of the font
float pointSize : Size of the font in points
int size : Size of the font in points
int style : Font style
Font Methods:
String getFamily( ) : Returns the name of the font family to which the invoking font belongs.
static Font getFont(String property) : Returns the font associated with the system property
specified by property. null is returned if property does not exist.
static Font getFont(String property, Font defaultFont) : Returns the font associated with the
system property specified by property. The font specified by defaultFont is returned if property
does not exist.
String getFontName() : Returns the face name of the invoking font.
String getName( ) : Returns the logical name of the invoking font.
int getSize( ) : Returns the size, in points, of the invoking font.
int getStyle( ) : Returns the style values of the invoking font.
Determining the Available Fonts
When working with fonts, often you need to know which fonts are available on your machine. To
obtain this information, you can use the getAvailableFontFamilyNames( ) method defined by the
GraphicsEnvironment class. It is shown here:

String[ ] getAvailableFontFamilyNames( )

This method returns an array of strings that contains the names of the available font families.

In addition, the getAllFonts( ) method is defined by the GraphicsEnvironment class. It is shown


here:
Font[ ] getAllFonts( )
This method returns an array of Font objects for all of the available fonts. Since these methods
are members of GraphicsEnvironment, you need a
GraphicsEnvironment reference to call them. You can obtain this reference by using the
getLocalGraphicsEnvironment( ) static method, which is defined by
GraphicsEnvironment. It is shown here:
static GraphicsEnvironment getLocalGraphicsEnvironment( )
Example:
Applet Code:
import java.applet.*;
import java.awt.*;
public class showFonts extends Applet {
public void paint(Graphics g) {
String msg = ""; String FontList[];
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
FontList = ge.getAvailableFontFamilyNames();
for(int i = 0; i < FontList.length; i++)
msg += FontList[i] + " ";
g.drawString(msg, 4, 16);
}
}
HTML Code:
<html>
<head>
<title>Showing Fonts</title>
</head>
<body>
<applet code="showFonts.class" width=300 height=200>
</applet>
</body>
</html>
OUTPUT:

You might also like