An applet is a program written in the Java programming language that can be included in an HTML page, much in the same way an image is included in a page. When you use a Java technology-enabled browser to view a page that contains an applet, the it’s code is transferred to your system and executed by the browser’s Java Virtual Machine (JVM).

Consider the SImple HelloWorld Application

import java.applet.*; //Here we need to import this package for creating an applet.

import java.awt.*; // this is needed for implementing AWT applicaion

// the start of an applet – HelloWorld will be the executable class
// Extends applet means that you will build the code on the standard Applet class
public class HelloWorld extends Applet

// The method that will be automatically called when the applet is started
public void init()
// It is required but does not need anything. you can also omitted this method.

// This method gets called when the applet is terminated
// That’s when the user goes to another page or exits the browser.
public void stop()
// no actions needed here now. this is also optional.

// The standard method that you have to use to paint things on screen
// This overrides the empty Applet method, you can’t called it “display” for example.

public void paint(Graphics g) //this method is needed to display the applet
//method to draw text on screen
// String first, then x and y coordinate.
g.drawString(“Hello World!”,20,20);



Applet will paint special shapes and use colors and fonts
Only new methods are explained

import java.awt.*;
import java.applet.*;

public class DrawExample extends Applet

// Specify variables that will be needed everywhere, anytime here
// The font variable
Font bigFont;

// The colors you will use
Color redColor;
Color weirdColor;
Color bgColor;

public void init()
// Here we will define the varibles further
// Will use Arial as type, 16 as size and bold as style
// Italic and Plain are also available
bigFont = new Font(“Arial”,Font.BOLD,16);

// Standard colors can be named like this
redColor =;

// lesser known colors can be made with R(ed)G(reen)B(lue).
weirdColor = new Color(60,60,122);

bgColor =;

// this will set the backgroundcolor of the applet


// now lets draw things on screen
public void paint(Graphics g)
// tell g to use your font
g.drawString(“Shapes and Colors”,80,20);

// Now we tell g to change the color

// This will draw a rectangle (xco,yco,xwidth,height);

// This will fill a rectangle

// change colors again


// a circle (int x, int y, int width, int height,int startAngle, int arcAngle);
// ovals are also possible this way.



// Draw a line (int x1, int y1, int x2, int y2)


// reset the color to the standard color for the next time the applets paints
// an applet is repainted when a part was’nt visible anymore
// happens most often because of browser minimizing or scrolling.




Threads are needed when you want to perfom actions over and over again.
We want to show the current time and update that every second.

import java.awt.*;
import java.applet.*;
// We need Date to get the current time.
import java.util.Date;

// Implement Runnable, this will make it possible for threads
// to call the run method when activated.
public class ThreadExample extends Applet implements Runnable
// Define your thread.
Thread clockThread;
// This textfield will show the time.
TextField clockField;
// Date will give us the current hours, minutes and seconds
Date date;
// This variable will remain true for as long
// we want the thread to run.
boolean running = true;

public void init()
// a standard layout to place just one textfield
setLayout(new BorderLayout());
clockField = new TextField();
// Create the thread.
clockThread= new Thread(this);
// and let it start running

// Very important. You do not want your thread to keep running when
// the applet is deactivated (eg. user left page)
public void destroy()
// will cause thread to stop looping
running = false;
// destroy it.
clockThread = null;

// The method that will be called when you have a thread.
// You always need this when you implement Runnable (use a thread)
public void run()
// loop until told to stop
while (running)
// Construct the current date.
date = new Date();
// Get the hours, minutes and hours
String time = date.getHours()+”:”+date.getMinutes()+”:”+date.getSeconds();
// Put that result in the textfield
//Now the reason for threads
// Wait 500milliseconds before continuing
catch (InterruptedException e)
// he has wait and will now restart his actions.


You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.