Internet Systems Design:
Technologies for Modern Information Systems

Example Java Applets 1

Note: If you find that Java Applets aren't working correctly (you will usually just see a gray rectangle) on your browser, download Java 2 Platform Standard Edition SDK (version 1.4.x) from www.java.sun.com. Make sure you download the SDK (J2SE v 1.4.x SDK). The latest Microsoft JVM seems to have a problem with J2 v 1.4 (funny that).

 

Example 1 Your browser does not support Java

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

/**
* The HelloWorld class implements an applet that
* simply displays "Hello World!".
*/
public class HelloWorld extends Applet {
    public void paint(Graphics g) {
        // Display "Hello World!"
        g.drawString("Hello world!", 50, 25);
        }
}

Included in HTML as follows:

<applet width="228" height="78" code="HelloWorld.class">

Your browser does not support Java. Get with the program!</applet>

Example 2 Your browser does not support Java

/* another hello again to demonstrate subclassing */

import java.awt.Graphics;
import java.awt.Font;
import java.awt.Color;

public class HelloAgainApplet extends java.applet.Applet {
    Font f = new java.awt.Font("TimesRoman",Font.BOLD,36);
    public void paint(Graphics g) {
        g.setFont(f);
        g.setColor(Color.red);
        g.drawString("Hello again!", 5, 50);
        }
}

Included in HTML as follows:

<applet width="228" height="128" code="HelloAgainApplet.class">

Your browser does not support Java. Get with the program!</applet>

Example 3 Your browser does not support Java

 

import java.awt.Font;
import java.awt.Graphics;

public class ManyFonts extends java.applet.Applet {
    public void paint(Graphics g) {
        Font f = new Font("TimesRoman", Font.PLAIN, 18);
        Font fb = new Font("TimesRoman", Font.BOLD, 18);
        Font fi = new Font("TimesRoman", Font.ITALIC, 18);
        Font fbi = new Font("TimesRoman", Font.BOLD + Font.ITALIC, 18);

        g.setFont(f);
        g.drawString("This is a plain font", 10, 25);
        g.setFont(fb);
        g.drawString("This is a bold font", 10, 50);
        g.setFont(fi);
        g.drawString("This is an italic font", 10, 75);
        g.setFont(fbi);
        g.drawString("This is a bold italic font", 10, 100);

        }

}

Included in HTML as follows: 

<applet width="228" height="168" code="ManyFonts.class">

Your browser does not support Java. Get with the program!</applet>

 

Example 4 The browser does not support java

import java.awt.Graphics;
import java.awt.Font;
import java.awt.Color;

public class MoreHelloApplet extends java.applet.Applet {
    Font f = new Font("TimesRoman",Font.BOLD,36);
    String name;
    public void init() {
        name = getParameter("name");
        if (name == null)
        name = "Laura";
    name = "Hello " + name + "!";
    }

    public void paint(Graphics g) {
    g.setFont(f);
    g.setColor(Color.red);
    g.drawString(name, 5, 50);
    }
}

Included in HTML as follows:

<applet width="328" height="78" code="MoreHelloApplet.class">

  <param name="name" value="Fred">
Your browser does not support Java. Get with the program!

</applet>

 

Your browser does not support Java

Example Java source code:

Lamp