Applet
A Java applet is a special kind of Java program
that a browser enabled with Java technology can
download from the internet and run.
An applet is typically embedded inside a web
page and runs in the context of a browser. An applet
must be a subclass of the java.applet.Applet class.
A Java Applet can be only executed within the
applet framework of Java.
An Applet class does not have any main()
method. It is viewed using JVM. The JVM can use
either a plug-in of the Web browser or a separate
runtime environment to run an applet application.
JVM creates an instance of the applet class and
invokes init() method to initialize an Applet
Life cycle of applet
Applet class
Applet class provides all necessary support for applet
execution, such as initializing and destroying of applet.
It also provide methods that load and display images and
methods that load and play audio clips.
An applet can be embedded in an HTML document using the
tag <APPLET> and can be invoked by any web browser.
Continue…
An Applet Skeleton
Most applets override these four methods.
init() : init() is the first method to be called. This is
where variable are initialized. This method is called only
once during the runtime of applet.
start() : start() method is called after init(). This
method is called to restart an applet after it has been
stopped.
stop() : stop() method is called to suspend thread
that does not need to run when applet is not visible.
destroy() : destroy() method is called when your
applet needs to be removed completely from memory.
Note: The stop() method is always called before destroy()
method.
Program
import java.awt.*;
import java.applet.*;
/*
<applet code = "AppletTest.class" width = "320" height = "120">
If your browser was Java-enabled, a "Hello, World"
message would appear here.
</applet>
*/
public class AppletTest extends Applet
{
public void init()
{
//initialization
}
public void start()
{
//start or resume execution
}
public void stop()
{
//suspend execution
}
public void destroy()
{
//perform shutdown activity
}
public void paint(Graphics g)
{
//display the content of window
g.drawString ("Hello World", 25, 50);
}
}
Output
HTML Coding
<html>
<body>
<applet code = "AppletTest.class" width = "320"
height = "120">
If your browser was Java-enabled, a "Hello,
World"
message would appear here.
</applet>
</body>
</html>
Browser example
Java Programming
Graphics is one of the most important
features of Java. Java applets can be written to
draw lines, arcs, figures, images and text in
different fonts and styles. Different colors can
also be incorporated in display.
Graphics Class:-
The graphics class defines a number of
drawing functions, Each shape can be drawn
edge-only or filled.
To draw shapes on the screen, we may call
one of the methods available in the graphics class
Continue…
Graphics class method:-
Continue…
drawLine:- Lines are drawn by means of the
drawLine() method.
Syntax
void drawLine(int startX, int startY, int
endX, int endY)
Explanation:-
drawLine() displays a line in the current
drawing color that begins at (start X, start Y) and
ends at (endX, end Y).
Program
//Drawing Lines
import java.awt.*;
import java.applet.*;
/*
<applet code="Lines" width=300 Height=250>
</applet>
*/
public class Lines extends Applet
{
public void paint(Graphics g)
{
g.setColor(Color.blue);
g.drawLine(0,0,100,100);
g.setColor(Color.yellow);
g.drawLine(0,100,100,0);
g.setColor(Color.red);
g.drawLine(40,25,250,180);
g.setColor(Color.pink);
g.drawLine(5,290,80,19);
}
}
Output
Program
Rectangle
The drawRect() and fillRect() methods display an outlined and filled
rectangle, respectively.
Syntax
void drawRect(int top, int left, int width, int height)
void fillRect(int top, int left, int width, int height)
The upper-left corner of the rectangle is at(top,left). The dimensions of
the rectangle are specified by width and height.
Use drawRoundRect() or fillRoundRect() to draw a rounded rectangle. A
rounded rectangle has rounded corners.
Syntax
void drawRoundRect(int top, int left, int width, int height int Xdiam, int
YDiam)
void fillRoundRect(int top, int left, int width, int height int Xdiam, int
YDiam)
The upper-left corner of the rounded rectangle is at (top,left). The
Program
import java.awt.*;
import java.applet.*;
/*
<applet code="Rectangle" width=300 Height=300>
</applet>
*/
public class Rectangle extends Applet
{
public void paint(Graphics g)
{
g.drawRect(10,10,60,50);
g.fillRect(100,100,100,0);
g.drawRoundRect(190,10,60,50,15,15);
g.fillRoundRect(70,90,140,100,30,40);
}
}
Output
Circle & Ellipse
To draw an ellipse - drawOval().
To fill an ellipse - fillOval().
Syntax
void drawOval(int top, int left, int width, int
height)
void fillOval(int top, int left, int width, int height)
The ellipse is drawn within a bounding
rectangle whose upper-left corner is specified by
(top,left) and whose width and height are specified
by width and height. To draw a circle, specify a
square as the bounding rectangle i.e get height =
width.
Program
import java.awt.*;
import java.applet.*;
/*
<applet code="Ellipses.class" width=300 Height=300>
</applet>
*/
public class Ellipses extends Applet
{
public void paint(Graphics g)
{
//set color to red
setForeground(Color.red);
g.drawOval(10,10,60,50);
g.fillOval(100,10,75,50);
setForeground(Color.blue);
g.drawOval(190,10,90,30);
g.fillOval(70,90,140,100);
}
}
Draw Arc
An arc is a part of oval. Arcs can be drawn with
draw Arc() and fillArc() methods.
Syntax
void drawArc(int top, int left, int width, int height, int
startAngle, int sweetAngle)
void fillArc(int top, int left, int width, int height, int startAngle,
int sweetAngle)
The arc is bounded by the rectangle whose upper-left
corner is specified by (top,left) and whose width and height are
specified by width and height.
The arc is drawn from startAngle through the angular
distance specified by sweepAngle. Angles are specified in
degree. '0o' is on the horzontal, at the 30' clock's position. The
arc is drawn conterclockwise if sweepAngle is positive, and
clockwise if sweetAngle is negative.
Program
import java.applet.*;
import java.awt.*;
/*<applet code="DrawArcExample.class" width=500 height=500>
</applet>
*/
public class DrawArcExample extends Applet
{
public void paint(Graphics g)
{
setForeground(Color.red);
g.drawArc(10,10,50,100,10,45);
Color c=new Color(200,120,130);
g.setColor(c);
g.fillArc(100,10,100,100,0,90);
}
}
Output
Working with Frame
Frame:-
A Frame is a top-level window with a title and a
border.
Difference between Panel and Frame:-
The main difference
between Panel and Frame in Java is that
the Panel is an internal region to a frame or
another panel that helps to group multiple
components together while a Frame is a
resizable, movable independent window with a
title bar which contains all other components.
Syntax:-
Frame( )
Frame(String title)
Program
import java.awt.*;
import java.awt.event.*;
class FrameJavaExample
{
public static void main (String args[])
{
Frame frame = new Frame("Frame Java Example");
//set the size of the frame
frame.setSize(300,250);
frame.addWindowListener(new WindowAdapter() )
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
frame.setVisible(true);
}
Output
Working with font
The Font class provides a method of specifying
and using fonts. The Font class constructor
constructs font objects using the font’s name,
style (PLAIN, BOLD, ITALIC, or BOLD + ITALIC),
and point size.
Program
import java.applet.*;
import java.awt.*;
/* <APPLET CODE ="FontClass.class" WIDTH=300 HEIGHT=200>
</APPLET> */
public class FontClass extends Applet
{
Font f;
String m;
public void init()
{
f=new Font("Arial",Font.ITALIC,20);
m="Welcome to Java";
setFont(f);
}
public void paint(Graphics g)
{
Color c=new Color(0,255,0);
g.setColor(c);
g.drawString(m,4,20);
}
Output