0% found this document useful (0 votes)
6 views

Java Programming-Unit V

Programming in java

Uploaded by

kesavanram07
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Java Programming-Unit V

Programming in java

Uploaded by

kesavanram07
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

UNIT – 5

APPLETS
Applet are small java programs used in internet computing. They are run using Applet
Viewer or any other browser.
HOW APPLETS DIFFER FROM APPLICATIONS
 Applet do not use main method to initiate execution of code.
 Applets automatically call methods of Applet class to start and execute Applet code.
 Applets run from web page using HTML tag.
 Applets cannot read from or write to the files in local computer.
 Applets cannot communicate with other servers on network.
 Applets cannot run any program from local computer.

BUILDING APPLET CODE


GENERAL FORM
Import java.awt.*;
Import java.applet.*;
Public class appletclassname extends Applet
{
public void paint (Graphics g)
{

}
}
 The Applet class contained in java.applet package provides life and behavior to Applets
through init (), Start () and Paint ().
 The paint () method of Applet class displays the result of the Applet code.
 The paint() method requires a Graphics object g as argument
public void paint (Graphics g)
 The applet code imports the java.awt package that contains the Graphics class.
 The output may be text, graphics or sound.
Example:
import java.awt.*;
import java.applet.*;
public class sample extends Applet
{
public void paint (Graphics g)
{
g.drawString(“Hello Java”,10,100);
}
}

java.lang.Object

java.awt.Component

java.awt.Container

java.awt.Panel

java.applet.Applet

The Applet class is a subclass of panel class, which is again a subclass of container class
& so on. The main Applet inherits the properties of all these classes.

BEGIN BORN INITIALIZATION


APPLET LIFE CYCLE
The applet states
START( ) STOP( )
 Born /Initialization State
 Running State IDLE
RUNNING
 Idle State
 Dead /Destroyed State DESTROY( )
DISPLAY START( )
DEAD

END
Initialization State
 Applet enters into initialization state when it is first loaded. This is achieved by init( )
method.
 The applet is born and only once in Applets life cycle.
 At this stage
o Create objects
o Set up initial values
o Load image & fonts
o Set up colors
 General form:
public void init( )
{
….
}

Running State
 Applet enters into running state when start( ) method called.
 Starting can occur if the applet is in idle state.
 The start( ) method may be called more than once.
 General form:
public void start( )
{
….
}

Idle or Stopped State


 Applet becomes idle when stopped from running.
 Stopping occurs automatically when the currently running applet page is left.
 Stopping done explicitly by using stop( ) method.
 General form:
public void stop( )
{
….
}

Dead State
 Applet is in dead state when it is removed from memory.
 This occurs automatically by invoking destroy( ) method.
 General form:
public void destroy()
{
….
}

Display State
 Applet moves to display state when paint( ) method is called.
 General form:
public void paint (Graphics g)
{
….
}

APPLET TAG
<APPLET…> and </APPLET>
The <APPLET…> tag specifies
1. Name of the Applet
2. Width of the Applet (in pixels)
3. Height of the Applet (in pixels)
Example:
<APPLET
CODE = program,class WIDTH = 400 HEIGHT = 200 >
</APPLET>
ADDING APPLETS TO HTML FILE
<HTML>
<HEAD>
<TITLE> APPLETS</TITLE>
</HEAD>
<BODY>
<APPLET CODE = Graph.Class WIDTH= 400 HEIGHT= 200 >
</APPLET>
</BODY>
</HTML>
RUNNING THE APPLET
To run Applet we require anyone of the tools
1. Java – enabled web browser
2. Java Applet Viewer – Run the Applet as
AppletViewer graph.java (java file)
AppletViewer graph.html (html file).

THE GRAPHICS CLASS


The Java’s Graphics includes methods for drawing different types of shapes, from lines to
polygons to text in a variety of fonts.

DRAWING METHODS OF GRAPHICS CLASS


S. No. Methods Description

1 clear Rect( ) Erases rectangle area of canvas.


2 copy Area( ) Copies a rectangle area to another area.
3 draw Arc( ) Draw a hollow arc.
4 draw Line( ) Draw a straight line.
5 draw Oval( ) Draw a hollow oval.
6 draw Polygon( ) Draw a hollow polygon.
7 draw Rect( ) Draw a hollow rectangle.
8 draw RoundRect( ) Draws a hollow rectangle with rounded corners.
9 drawString( ) Displays a text string.
10 Fill Arc( ) Draws a filled arc.
11 Fill Oval( ) Draws a filled Oval.
12 Fill Polygon( ) Draws a filled Polygon.
13 Fill Rect( ) Draws a filled Rectangle.
14 Fill RoundRect( ) Draws a filled Rectangle with rounded corners.
15 get Color() Retrieves the current drawing color.
16 set Font() Retrieves the current used font.
17 set Color() Sets the drawing color.
18 set Font() Sets the Font.
LINES
The drawLine( ) method takes two pair of coordinates (x1, y1) and (x2, y2) as arguments and
draws a line between them.
Example:
g.drawLine(10,10,50,50);
g – Graphics object to paint( ) method.

RECTANGLES
The drawRect() method takes four arguments. The first two represent x & y co-ordinates of
the top left corner of rectangle, and the remaining two represent the width and height of
rectangle.
Example 1 (10, 60) g.drawRect(10, 60, 40, 30);
g.drawRect(10, 60, 40, 30); Height (Pixels)

Width (Pixels)

Example 2:
g.fillRect(10, 60, 40, 30);
Draws a solid rectangle starting at (10, 60) with width 40 pixels and height 30 pixels.

Example 3:
g.drawRoundRect(10, 60, 40, 30, 10, 10);

Example 4:
g.fillRoundRect(10, 60, 40, 30, 10, 10);

Example 3 & 4 takes extra two arguments representing the width and height of the angle of
corners.

CIRCLES AND ELLIPSES


 The drawOval( ) method used to draw a circle or ellipse.
 The method takes four arguments .The first two represent the top left corner and the
other two represent the width and height of oval.
Example 1: (70,30)
g.drawOval(20,20,200,120); //ellipse
- draws outline of an oval.
Example 2: 100
g.setColor(Color.green);
g.fillOral(70,30,100,100); // circle. 100
- draws a circle filled with green color.

DRAWING ARCS
 The drawArc( ) method takes six arguments. The first two represent the top left corner,
the next two width and height, the last two starting angle of the arc and the sweep angle
in degrees.
Example 1:g.drawArc(60, 125, 80, 40, 180, 180);

Example 2:g.fillArc(60, 125, 80, 40, 180, 180);

DRAWING POLYGONS
 The drawPolygon() method takes three arguments:
1. An array of integers containing x co-ordinates.
2. An array of integers containing y co-ordinates.
3. An integer for total number of points.
Example:
int x1 [ ] ={20, 120, 220, 20};
int y1 [ ] ={20, 120, 20, 20};
int n = 4; // x points.length;

Example 1: g.drawPolygon(x1, y1, n);

Example 2: g.fillPolygon(x1, y1, n);

Example 3:
int n = x.length;
Polygon poly = new Polygon (x1, y1, n);
g.drawPolygon(poly);

You might also like