Java – Draw Rectangle in Applet Example

/*
<applet code="DrawRectanglesExample" width=200 height=200>
</applet>
*/

import java.applet.Applet;
import java.awt.Graphics;

public class DrawRectanglesExample extends Applet {

        public void paint(Graphics g) {

                //this will draw a rectangle of width 50 & height 100 at (10,10)
                g.drawRect(10,10,50,100);

                //this will draw a square
                g.drawRect(100,100,50,50);
        }
}

Java Applet – Display Message in Browser Status Bar

import java.applet.Applet;
import java.awt.Graphics;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class TimeApplet extends Applet implements Runnable {
private DateFormat formatter = null;
private Thread t = null;

public void init() {
formatter = new SimpleDateFormat("hh:mm:ss");
t = new Thread(this);
}

public void start() {
t.start();
}

public void stop() {
t = null;
}

public void paint(Graphics g) {
Date now = Calendar.getInstance().getTime();
//
// Show the current time on the browser status bar
//
this.showStatus(formatter.format(now));
}

public void run() {
int delay = 1000;
try {
while (t == Thread.currentThread()) {
//
// Repaint the applet every on second
//
repaint();
Thread.sleep(delay);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}

Java Applet – Change Background Color

import java.applet.Applet;
import java.awt.Color;
import java.awt.Graphics;

public class WhiteBackgroundApplet extends Applet {
public void init() {
//
// Here we change the default gray color background of an applet to
// white background.
//
setBackground(Color.WHITE);
}

public void paint(Graphics g) {
g.setColor(Color.BLACK);
g.drawString("Applet background example", 0, 50);
}
}

Page 1 of 212