How to Use Applet

  1. Create two text files as follows and save them into the same drive, for example drive A:

//File name MUST be ABC.java

//

import java.applet.Applet;

import java.awt.*;

public class ABC extends Applet

{

public void paint (Graphics g)

{

g.drawString ("Be Prepared", 100, 100);

g.drawLine (0,0,100,100);

g.drawLine (0,300, 100,100);

g.drawLine (400,0,100,100);

g.drawLine (400,300,100,100);

}

}

!filename can be <AnyName>.html. The suffix html is a must. Let call it test.html

<HTML>

<HEADER>

<TITLE>COMP110 Webpage</TITLE>

</HEADER>

<BODY>

<H2>Boy Scout Slogan</H2>

<APPLET CODE = "ABC.class" WIDTH=400 HEIGHT=300> </APPLET>

</BODY>

</HTML>

  1. Compile the file ABC.java in DOS

javac ABC.java

After the compilation, compiler javac will generate an object code file: ABC.class. This file is included in the html file.

  1. Execute the file test.html. To execute the file, double click it. Since the file has a suffix html, the browser Netscape will open and interpret the file line by line. Certainly, you need to know the meaning of each instruction of this language. However, I like to focus on the instruction with an APPLET CODE:

<APPLET CODE = "ABC.class" WIDTH=400 HEIGHT=300> </APPLET>

As soon as the browser executes this line, it uses its embedded java interpreter to run the line. Then the java interpreter will open ABC.class within the same directory of html file.

In summary, we do the following 3 steps:

Step 1. Create a java file ( name must be identical as its class name) and a html file with an applet instruction to include the class name generated by the java file.

Step 2. Compile the java file:

javac ABC.java

Step 3. Execute the html file: Double click the file.

NOTES:

  • If ABC.class has any error, the net browser will not echo any error message. It is likely to ignore the execution of this class.
  • In reality, the two files are saved in a server. The file with suffix html will have a net address. The file can be called via its address through a network. The file will be transmitted as a text file as you has seen it above arrived at the caller destination ( at the local PC site). The browser at the local computer will interpret the file. During the interpretation, the applet class is also transmitted to the local computer to be interpreted by the the embedded java within the browser, not the java of jdk package.
  • If you try the options reload or refresh within the browser, the html file will be updated, not the class file. As a developer, if you modify your java file and recompile it to be a new class, it will not be reloaded. To execute with this new class, you must reopen the net browser again.

An Alternate Execution

There is another way to execute the html file ( see step 3 above for the net browser execution). You will find within jdk package a file named appletviewer. It is likely it is in the bin sub-directory. In general, the three files:

javac.exe,

java.exe, and

appletviewer.exe

are located within the same directory bin.

Here is a sequence of steps:

  1. Create two files: Do exactly the same as the above step 1.
  2. Compile the java file: Do exactly the same as the above step 2.

javac ABC.java

  1. Execute the html file: (This step is different.) Enter the command in DOS:

appletviewer test.html

NOTES:

  • You must make sure that the DOS understand the directory location of the exe files javac.exe, java.exe, and appleviewer.exe. If DOS could not fing any of them, you must manually locate them and type

path <pathName>\bin;

  • If you modify the html file, you need to re issue the command:

appletviewer test.html

  • If you modify the java file, you need to recompile it:

javac ABC.java

However, you don’t need to reissue the appletviewer command. You need to reload option within the applet window left on your screen during the previous execution of the html file.

Practice problems

1. Modify the java program to have a loop to draw a multiple rays from the center of the message “Be Prepared. Here is the modified program.. You need to compile and run it.

//File name MUST be ABC.java

//

import java.applet.Applet;

import java.awt.*;

public class ABC extends Applet

{

public void paint (Graphics g)

{

g.drawString ("Be Prepared", 100, 100);

for (int counter = 0; counter < 10; counter ++)

{

g.drawLine (counter* 10,0,100,100);

}

g.drawLine (0,300, 100,100);

g.drawLine (400,0,100,100);

g.drawLine (400,300,100,100);

}

}

  1. Modify further, compile and run the above java program by replacing the last three drawLine’s ( out of loop) by the three new ones:

g.drawLine (0,300-counter*10, 100,100);

g.drawLine (400,0+counter*10,100,100);

g.drawLine (400-counter*10,300,100,100);

Then move them into the for loop.

I/O via Applet

You have worked on java application using java.io.* to get input from keyboard. In this section, you will learn another way to get data from keyboard but using javax.swing.*.

Let look at the above java program. The constant 10 in the for loop and 10 as a factor for changing the coordinates by formula counter*10 can be read via keyboard. Let call the variable iteration to hold the constant. However, we can only read a string via keyboard. So we need to convert a string integer into an integer. Hence we need the following code:

String input;

input =JOptionPane.showInputDialog ("Enter a number:");

iteration = Integer.parseInt(input);

  • Within this code, the method JOptionPane.showInputDialog ("Enter a number:") belong to a predefined class javax.swing.*. Therefore we need to import it at the beginning of the file.
  • The variable iteration must be defined in the class but outside the methods because both methods use it.
  • Here is the modified ABC.java

//File name MUST be ABC.java

//

import java.applet.Applet;

import java.awt.*;

import javax.swing.*;

public class ABC extends Applet

{

int iteration = 0;

// a new method to get string input via keyboard

public void init()

{

String input;

input =JOptionPane.showInputDialog ("Enter a number:");

iteration = Integer.parseInt(input);

}

public void paint (Graphics g)

{

g.drawString ("Be Prepared", 100, 100);

for (int counter = 0; counter < iteration; counter ++)

{

g.drawLine (counter* iteration,0,100,100);

g.drawLine (0,300-counter*iteration, 100,100);

g.drawLine (400,0+counter*iteration,100,100);

g.drawLine (400-counter*iteration,300,100,100);

}

}

}

During the execution you will have two windows: one to ask for input data via keyboard, other a graphic display.

Practice Problem:

Use the following statement:

JoptionPane.showMessageDialog(null, “Please enter a number less than 50.”);

to send a message that the input is larger than 50.