Docsity

Introduction to Java Applets and the Math Class, Papers of Cryptography and System Security

Toggle sidebar
bg1
1
Pic 20
Lecture 4
The HelloWorld Applet
An applet is a java program that can be viewed by a browser, such
as Netscape. We start with an example that demonstrates the elementary
syntax rules used in writing and displaying an applet.
The output of our example is
The program for producing this screen follows. It should be saved
in a file called HelloWorld.java
import javax.swing.JApplet;
import java.awt.Graphics;
public class HelloWorld extends JApplet
{
public void paint(Graphics g)
{
g.drawString("Hello World", 20, 30);
} // end paint(Graphics g)
}// end class HelloWorld
There are several new syntax points here. To begin, the first two
lines of the program import two java classes: JApplet, which is in the
javax.swing package, and Graphics, which is in the java.awt package.
The abbreviation awt stands for abstract windowing toolkit.
Next, the first line after the import statements
public class HelloWorld extends Japplet
says that this is going to be an applet; the phrase "extends Japplet"
accomplishes this.
Finally, paint(Graphics g) is the one method in this program.
Its' one line of code
g.drawString("Hello World", 20, 30);
uses the method drawString(string, x_coordinate, y_coordinate) of the
class Graphics class to place the string on the screen, starting at a
specific location.
pf3
pf4
pf5
pf8
pf9

Partial preview of the text

Download Introduction to Java Applets and the Math Class and more Papers Cryptography and System Security in PDF only on Docsity!

Pic 20 Lecture 4

The HelloWorld Applet

An applet is a java program that can be viewed by a browser, such as Netscape. We start with an example that demonstrates the elementary syntax rules used in writing and displaying an applet.

The output of our example is

The program for producing this screen follows. It should be saved in a file called HelloWorld.java

import javax.swing.JApplet; import java.awt.Graphics;

public class HelloWorld extends JApplet { public void paint(Graphics g) { g.drawString("Hello World", 20, 30);

} // end paint(Graphics g)

}// end class HelloWorld

There are several new syntax points here. To begin, the first two lines of the program import two java classes: JApplet, which is in the javax.swing package, and Graphics, which is in the java.awt package. The abbreviation awt stands for abstract windowing toolkit.

Next, the first line after the import statements

public class HelloWorld extends Japplet

says that this is going to be an applet; the phrase "extends Japplet" accomplishes this.

Finally, paint(Graphics g) is the one method in this program. Its' one line of code

g.drawString("Hello World", 20, 30);

uses the method drawString(string, x_coordinate, y_coordinate) of the class Graphics class to place the string on the screen, starting at a specific location.

The output screen produced by the applet will have x_coordinates starting from 0 at the left and running to the right. The y_coordinates will start at the top and run downward. Thus (0,0) is at the top, left of the screen. The line g.drawString("Hello World", 20, 30); places the string starting at x = 20 and y = 30; i.e., 20 lines down and 30 columns to the right.

As mentioned, this program should be saved in a file called HelloWorld.java, and the compiled.

Once compiled, it can be displayed on the screen. To do so first, write an .html file

Save this under the name HelloWorld.html; place it in the same directory that contains the HelloWorld.java and HelloWorld.class files:

The applet is displayed by invoking the command:

>appletviewer HelloWorld.html

The log of all the DOS commands used in producing this applet, starting from switching to the appropriate directory, to compiling, and then to using appletviewer is:

To run the applet in your browser open it and go to the address where the file HelloWorld.html is located. The white space to the right of "Location" below shows the syntax for displaying it from my system. (That is a straight line, to the right of the Z, in the address file:///Z|/..)

To run other applets in the browser, simply replace the two occurrences of HelloWorld.class in the above .html file by the appropriate class name.

Java's Math Class

Java has a class, Math, that permits one to use the standard mathematical functions with little effort. These functions are "built- in".

Our next applet will show how to display text without using the paint() method as well as the syntax of the Math class. Its output is:

The framework for the program is

import javax.swing.*; import java.awt.Container;

public class mathFunctions extends JApplet { public void init() { } // end init()

}// end class mathFunctions

The package javax.swing.* is used, as in the previous example, to construct the applet. The class java.awt.Container is imported to assist with the construction of the output screen. The init() method is common to nearly all applets. Only extremely trivial applets, such as our HelloWorld, do not have an init() method.

The full program is

import javax.swing.*; import java.awt.Container;

public class mathFunctions extends JApplet { public void init() { String output = "";

JTextArea outputArea; outputArea = new JTextArea(10, 30);

Container c = getContentPane(); c.add(outputArea);

output = output + " cos(0.34) = " +

The class JTextArea is contained in the javax.swing package. The line of code JTextArea outputArea;

creates a reference, outputArea, to the class JTextArea. Roughly speaking, outputArea is a variable of type JTextArea. The next line

outputArea = new JTextArea(10, 30);

creates an instance of JTextArea; outputArea is defined to be an instance of JTextArea. It is a specific text area of 10 lines and 30 columns.

To continue, the text area we have just constructed has to be put into a pane. The Container class has a method for producing a pane:

Container c = getContentPane();

The text area is then added to the pane by means of the line:

c.add(outputArea);

Thus, we have built the infrastructure of the applet. The line

output = output + " cos(0.34) = " + Math.cos(0.34) + "\n";

demonstrates how the Math class is used: cos() is a method of the class Math. To calculate the value of cos(0.34) we write Math.cos(0.34). The "output" string is formed by repeated calls to methods of the Math class. Note that we have not had to import the Math class. It is "built-in".

Once the "output" string is constructed it is added to the outputArea by means of the setText() method:

outputArea.setText(output);

To finish, compile the program (javac mathFunctions.java). Then create a mathFunctions.html file. The following will do:

Then run it using the appletviewer (appletviewer mathFunctions.html)

The Math class contains a variety of mathematical functions:

Method Example abs(x) abs(10.1) = 10.1, abs(-5) = 5 exp(x) exp(1) = 2.718.. log(x) log to the base e; log(x) = ln(x) pow(x,y) x y max(x,y) min(x,y) floor(x) (^) largest integer ≤≤≤≤ x, floor(3.2) =. sin(x) cos(x) tan(x) sqrt(x) random(x) (^) random number, ≥≥≥≥ 0, < 1