Chapter 5
Chapter 5
Programming
• Introduction to applets :
• Applet Basics-
• Applet is a special type of program that is embedded in the webpage to
generate the dynamic content.
• It runs inside the browser and works at client side.
• applets are small java programs that are primarily used in internet
computing
• An applet is like application program which can perform arithmetic
operations, display graphics, play sounds accept user input, create
animation and play interactive games.
• To run an applet, it must be included in HTML tags for web page
• Every applet is implemented by creating sub class of Applet class
• Advantage of Applet
• 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.
Differentiate between applet and application
Applet Application
Applet does not use main() method for initiating Application use main() method for initiating
execution of code execution of code
Applet cannot run independently Application can run independently
Applet cannot read from or write to files in local Application can read from or write to files in local
computer computer
Applet cannot communicate with other Application can communicate with other
servers on network servers on network
Applet cannot run any program from local computer. Application can run any program from local
computer
Applet are restricted from using libraries from other Application are not restricted from using
language such as C or C++ libraries from other language
Chain of classes inherited by applet class in java
Java.lang.Object
Java.awt.Component
Java.awt.Container
Java.awt.Panel
Java.applet.Applet
Applet Life Cycle/ Applet Skeleton
• The applet life cycle can be defined as the process of how the object is created,
started, stopped, and destroyed during the entire execution of its application.
• It basically has five core methods namely init(), start(), stop(), paint() and
destroy().
1. Write a java applet code and save it with as a class name declared in
a program by extension as a .java.
• e.g. from above java code file we can save as a Welcome.java
2. Compile the java file in command prompt jdk as shown below
C:\java\jdk1.7.0\bin> javac Welcome.java
3. After successfully compiling java file, it will create the .class file, e.g
Welcome.class. then we have to write applet code to add this class into
applet.
4. Applet code
<html>
<Applet code= “ Welcome.class” width= 500 height=500>
</applet> </html>
5. Save this file with Welcome.html in ‘bin’ library folder.
6. Now write the following steps in command prompt jdk.
C:\java\jdk1.7.0\bin> appletviewer Welcome.java
C:\java\jdk1.7.0\bin> appletviewer Welcome.html
(Shows output in applet viewer)
OR
C:\java\jdk1.7.0\bin> Welcome.html
(Shows output in internet browser)
Graphics Class
• The Graphics class of java includes methods for drawing different
types of shapes, from simple lines to polygons to text in a variety of
fonts.
• drawRect( )
• The drawRect() method display an outlined rectangle
• Syntax: void drawRect(int top, int left, int width, int height)
• This method takes four arguments, the first two represents the x and y co-
ordinates of the top left corner of the rectangle and the remaining two
represent the width and height of rectangle.
• Example: g.drawRect(10,10,60,50);
• Circle and Ellipse
• drawOval( ) :
• To draw an Ellipses or circles used drawOval() method can be used.
• Syntax: void drawOval( int top, int left, int width, int height)
• The ellipse is drawn within a bounding rectangle whose upper-left
corner is specified by top and left and whose width and height are
specified by width and height to draw a circle or filled circle, specify
the same width and height the following program draws several
ellipses and circle.
• Example: g.drawOval(10,10,50,50);
• fillOval ( ) :
• Draws an oval within a bounding rectangle whose upper left corner is
specified by top, left. Width and height of the oval are specified by width
and height.
• Syntax- void fillOval(int top, int left, int width, int height);
• Example g.fillOval(10,10,50,50);
• Drawing Arcs :
• drawArc( )
• It is used to draw arc
• Syntax:
• void drawArc(int x, int y, int w, int h, int start_angle, int sweep_angle);
• where x, y starting point, w& h are width and height of arc, and start_angle
is starting angle of arc sweep_angle is degree around the arc
• Example: g.drawArc(10, 10, 30, 40, 40, 90);
• Drawing polygons :
• drawArc( ) :
• It is used to draw arc
• Syntax:
• void drawArc(int x, int y, int w, int h, int start_angle, int sweep_angle);
• where x, y starting point, w& h are width and height of arc, and
start_angle is starting angle of arc sweep_angle is degree around the
arc
• Example:
• g.drawArc(10, 10, 30, 40, 40, 90);
• Drawing polygons
• drawPolygon( ) :
• drawPolygon() method is used to draw arbitrarily shaped figures.
• Syntax- void drawPolygon(int[ ] xPoints, int[ ] yPoints, int
numPoints);
• The polygon‟s end points are specified by the co-ordinates pairs
contained within the x and y arrays. The number of points define by x
and y is specified by numPoints.
• Example-
int x[ ] = {10, 170, 80};
int y[ ] = {20, 40, 140};
int n = 3;
g.drawPolygon(x, y, n);
Setting color of an Applet
• Background and foreground color of an applet can be set by using followings methods
void setBackground(Color.newColor)
void setForeground (Color.newColor)
• where newColor specifies the new color.
• The class color defines the constant for specific color listed below.
• Color.black Color.white Color.pink Color.yellow
• Color.lightGray Color.gray Color.darkGray Color.red
• Color.green Color.magenda Color.orange Color.cyan
• Example
setBackground(Color.red);
setForeground (Color.yellow);
• The following methods are used to retrieve the current background and foreground color.
Color getBackground( )
Color getForeground( )
Font class
• A font determines look of the text when it is painted.
• Font is used while painting text on a graphics context & is a property
of AWT component.
• The Font class defines these variables:
Variable Meaning
String name Name of the font
float pointSize Size of the font in points
int size Size of the font in point
int style Font style
Use of font class
• The Font class states fonts, which are used to render text in a visible way.
• It is used to set or retrieve the screen font.
• Syntax to create an object of Font class.
• To select a new font, you must first construct a Font object that describes that font. Font
constructor has this general form:
Font(String fontName, int fontStyle, int pointSize)
• fontName specifies the name of the desired font.
• The name can be specified using either the logical or face name.
• All Java environments will support the following fonts:
• Dialog, DialogInput, Sans Serif, Serif, Monospaced, and Symbol. Dialog is the font used by once
system‟s dialog boxes.
• Dialog is also the default if you don‟t explicitly set a font. You can also use any other fonts
supported by particular environment, but be careful—these other fonts may not be universally
available.
• The style of the font is specified by fontStyle.
• It may consist of one or more of these three constants:
• Font.PLAIN, Font.BOLD, and Font.ITALIC. To combine styles, OR them together.
• For example,
• Font.BOLD | Font.ITALIC specifies a bold, italics style.
• The size, in points, of the font is specified by pointSize.
• To use a font that you have created, you must select it using
setFont( ), which is defined by Component.
• It has this general form:
void setFont(Font fontObj)
Here, fontObj is the object that contains the desired font
Methods of font class
Methods Description
static Font decode(String str) Returns a font given its name.
boolean equals(Object FontObj) : Returns true if the invoking object contains the same font as that
specified by FontObj.Otherwise, it returns false.
String toString( ) Returns the string equivalent of the invoking font.
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 Returns the font associated with the System property specified by
property,Font defaultFont) 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.
int hashCode( ) Returns the hash code associated with the invoking object.
boolean isBold( ) Returns true if the font includes the BOLD style
value. Otherwise, false is returned.