Applet Example

 


This routine prints out Hello World to a browser window:

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

public class HelloWorld extends Applet {
  public void paint(Graphics g) {
    g.drawString("Helloworld!"
                        ,50,25);

  }
}

  Put this code into a file called HelloWorld.java

Then compile the application with

This creates the class file

Create an html file to hold the applet. Put the following code into a file called HelloWorld.html in the SAME directory as the class file.

 

Creating a Simple Java Applet WITH JBUILDER

The following exercise will help you become familiar with Borland’s JBuilder 3 Development Environment. This is an “at-home” exercise for your personal computer and is recommended following the installation of JBuilder3.

 

Start Borland's JBuilder3

Click the "Start" button
Select: Programs | Borland JBuilder3 University | JBuilder3

From the Menu Bar,

Select: File | Close to close any open projects or files

 

Create the Project

From the Menu Bar

Select: File | Open

When the Open dialog box appears:

If you want this project to be saved to floppy:

In the "Look in:" Drop-Down box, Left Click the Down Arrow
Left Click "3½ Floppy (A:)"

Left Click on the "Create New Folder" Button

Type Exercise2 as the folder name
Double-click on the "Exercise2" folder
Left Click in the "File name:" box

In the "Filename:" box, remove any existing text and type:

Exercise2.jpr
Click the "Open" button

A message dialog box will appear stating the file does not exist.

Click the "Yes" button to the question, "Create a new file?"

From the Menu Bar

Select: Project | Properties
Modify the Root Directories as follows:

Source:
    C:\Java\Exercise2
        -or-
    A:\Exercise2

Output:
    C:\Java\Exercise2
        -or-
    A:\Exercise2

 

Click the "OK" button

 

Create the Java Source

From the Menu Bar

Select: Project | Add to Project

Enter Filename: "WelcomeLines.java"
Check "Add to Project" box
Click the "Open" button

Type in lines 1 - 13 exactly as shown in Fig 3.10 on page 93 of the text
(do not type the line numbers)

From the Menu Bar

Select: File | Save

 

Create the HTML Source

From the Menu Bar

Select: Project | Add to Project

Enter Filename: "WelcomeLines.html"
Check "Add to Project" box
Click the "Open" button

Type in lines 1 - 4 exactly as shown in Fig 3.11 on page 93 of the text
(do not type the line numbers)

From the Menu Bar

Select: File | Save

 

Run the Program

From the Menu Bar,

Select: Run | Run "WelcomeLines.java"

If the program does NOT work and there are errors in the message window, correct the errors and try to "Run" the program again. Repeat until the program works.