Java Programming-Unit V
Java Programming-Unit V
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.
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.
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( )
{
….
}
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).
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.
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);
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 3:
int n = x.length;
Polygon poly = new Polygon (x1, y1, n);
g.drawPolygon(poly);