0% found this document useful (0 votes)
6 views11 pages

Java Applets12

An applet is a Java program that runs in a web browser and has access to the entire Java API. It differs from standalone Java applications in that it does not have a main() method, is embedded in HTML, and has strict security rules. The document also outlines the applet lifecycle, methods for creating applets, and examples of graphics and event handling in applets.

Uploaded by

buushman151
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views11 pages

Java Applets12

An applet is a Java program that runs in a web browser and has access to the entire Java API. It differs from standalone Java applications in that it does not have a main() method, is embedded in HTML, and has strict security rules. The document also outlines the applet lifecycle, methods for creating applets, and examples of graphics and event handling in applets.

Uploaded by

buushman151
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

APPLETS IN JAVA

An applet is a Java program that runs in a Web browser. An applet can be a fully functional Java
application because it has the entire Java API at its disposal.

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.

Life Cycle of an Applet

Four methods in the Applet class gives you the framework on which you build any serious applet −

• init − This method is intended for whatever initialization is needed for your applet. It is
called after the param tags inside the applet tag have been processed.

• start − This method is automatically called after the browser calls the init method. It is also
called whenever the user returns to the page containing the applet after having gone off to
other pages.

• stop − This method is automatically called when the user moves off the page on which the
applet sits. It can, therefore, be called repeatedly in the same applet.

• destroy − This method is only called when the browser shuts down normally. Because
applets are meant to live on an HTML page, you should not normally leave resources behind
after a user leaves the page that contains the applet.

GEORGE WAINAINA 0718313173/0731919231 [email protected] Page 1


• paint − Invoked immediately after the start() method, and also any time the applet needs to
repaint itself in the browser. The paint() method is actually inherited from the java.awt.

How to create an applet

Create a new project and give a name. don’t select the main class

Right click on the source package under your class, create new java applet under others

GEORGE WAINAINA 0718313173/0731919231 [email protected] Page 2


Give your applet a name and click finish

Applets do not have public static void statement. Instead they have Public void paint (Graphic g)
that enable the statement to displayed on window

GEORGE WAINAINA 0718313173/0731919231 [email protected] Page 3


Run your program after writing your applet

GEORGE WAINAINA 0718313173/0731919231 [email protected] Page 4


EXAMPLE 1: Java Code to print Zetech University using Applet:
import java.applet.*;
import java.awt.*;
public class ONE extends Applet
{
public void paint (Graphics g)
{
setBackground(Color.cyan);
setForeground(Color.red);
g.drawString ("Zetech University", 25, 50);
}
}

OUTPUT:

GEORGE WAINAINA 0718313173/0731919231 [email protected] Page 5


Graphics in Applet
In Applet, java.awt.Graphicsclass provides methods for using graphics.
Below are the Methods of the Graphics class.
Sr
Methods Description
No.
1 public abstract void drawString(String str, int x, int y) Used to draw specified string.
Used to draw a rectangle of specified width
2 public void drawRect(int x, int y, int width, int height)
and height.
public abstract void fillRect(int x, int y, int width, int Used to draw a rectangle with a default
3
height) colourof specified width and height.
public abstract void drawOval(int x, int y, int width, int Used to draw oval of specified width and
4
height) height.
public abstract void fillOval(int x, int y, int width, int Used to draw oval with a default colour of
5
height) specified width and height.
Used for drawing lines between the point (x1,
6 public abstract void drawLine(int x1, int y1, int x2, int y2)
x1) and (x2, y2).
public abstract booleandrawImage(Image img, int x, int y,
7 Used for drawing a specified image.
ImageObserver observer)
public abstract void drawArc(int x, int y, int width, int
8 Used for drawing a circular arc.
height, intstartAngle, intarcAngle)
public abstract void fillArc(int x, int y, int width, int height,
9 Used for filling circular arc.
intstartAngle, intarcAngle)
10 public abstract void setColor(Color c) Used to set a colour to the object.
11 public abstract void setFont(Font font) Used to set font.

EXAMPLE 2: Java Code to come up with shapes:


import java.applet.Applet;
import java.awt.*;
public class WAINAINA extends Applet
{
public void paint(Graphics g)
{
setBackground(Color.yellow);
g.drawString("Welcome to shapes",50, 50);

GEORGE WAINAINA 0718313173/0731919231 [email protected] Page 6


g.setColor(Color.red);
g.fillOval(170,200,30,30);
g.drawLine(21,31,20,300);
g.drawRect(70,100,30,30);
g.fillRect(170,100,30,30);
g.drawOval(70,200,30,30);
}
}

OUTPUT

EventHandling in Applet

In Applet we can also perform event handling.

GEORGE WAINAINA 0718313173/0731919231 [email protected] Page 7


EXAMPLE 3: Event handling which prints a message when clicked on the button.:
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class UNIVERSITY extends Applet implements ActionListener
{
Button ZETECH;
TextField student;
public void init()
{
setBackground(Color.magenta);
student=new TextField();
student.setBounds(30,40,200,20);
ZETECH=new Button("Click");
ZETECH.setBounds(80,150,60,50);
add(ZETECH);
add(student);
ZETECH.addActionListener(this);
setLayout(null);
}
public void actionPerformed(ActionEvent e)
{
student.setText("Pursuing Bsc. at Zetech University");
}
}
OUTPUT:

GEORGE WAINAINA 0718313173/0731919231 [email protected] Page 8


Painting in Applet

Below is an example of Painting using mouseDragged() method of MouseMotionListener in Applet.

Example 4: A Java Code to generate a platform for painting.

import java.awt.*;
import java.awt.event.*;
import java.applet.*;
public class PAINTING extends Applet implements MouseMotionListener
{
public void init()
{
addMouseMotionListener(this);
setBackground(Color.white);
}
public void mouseDragged(MouseEvent me)
{
Graphics g=getGraphics();
g.setColor(Color.black);
g.fillOval(me.getX(),me.getY(),5,5);
}
public void mouseMoved(MouseEvent me)
{}
}
OUTPUT

GEORGE WAINAINA 0718313173/0731919231 [email protected] Page 9


Digital Clock in Applet
In java, Applet can be used for creating a Digital Clock. For creating a program for the digital
clock, java.apple, java.awt, java.util, and java.text package are imported. Date and Time functions are
also used. below is a program for creating a Digital Clock.

Example 4: A Java Code to generate a Digital Clock.


import java.applet.*;
import java.awt.*;
import java.util.*;
import java.text.*;
public class DigitalClock extends Applet implements Runnable
{
Thread t = null;
int h=0, m=0, s=0;
String timeString = "";
public void init()
{
setBackground( Color.black);
}
public void start()
{
t = new Thread( this );
t.start();
}
public void run()
{
try
{
while (true)
{
Calendar cal = Calendar.getInstance();
h = cal.get( Calendar.HOUR_OF_DAY );
if ( h> 12 ) h -= 12;
m = cal.get( Calendar.MINUTE );
s = cal.get( Calendar.SECOND );
SimpleDateFormat f = new SimpleDateFormat("hh:mm:ss");

GEORGE WAINAINA 0718313173/0731919231 [email protected] Page 10


Date date = cal.getTime();
timeString = f.format( date );
repaint();
t.sleep( 1000 );
}
}
catch (Exception e) { }
}
public void paint( Graphics g )
{
g.setColor( Color.white );
g.drawString( timeString, 50, 50 );
}
}

OUTPUT:

GEORGE WAINAINA 0718313173/0731919231 [email protected] Page 11

You might also like