- Preface
- Markup Languages – A Definition and Some History
- Beginning HTML
- HTML Lists
- HTML Tables
- HTML - Color, Fonts and Special Characters
- HTML Links
- HTML Images
- HTML Frames
- Cascading Style Sheets
- MicroSoft PhotoDraw
- JavaScript
- HTML Forms and Form Handling
- VBScript
- MicroSoft FrontPage
- Active Server Pages
- Java Applets
- XML Meaning and More
- Macromedia Flash 5.0
- References
|
Although Java and JavaScript have similar names and a couple of similar types of statements, they are very
different programming languages. Java is a much more powerful language that was first developed for
implementing programs on appliances like Cable TV boxes, microwaves, etc. They wanted the language
to be independent of the processor that the programs would be run on, since the processors used in
consumer applicances might be vary. They wanted the language to be tight and efficient (not requiring
a lot of memory or processing power).
Java programs are somewhat unique in that they are machine independent. The Java compiler
translates Java code into byte-code, rather than machine code. Byte-code is an intermediate translation
that does not include machine-specific details. To run the byte-code, we can either use a Java interpreter
or run the byte-code on a Java-enabled browser. If we use a browser, the program is referred to as an
applet, rather than an application. The Java compiler can be obtained free from Sun .
Java is a strongly typed language. That means that you must declare variables to be a certain data type and
they cannot hold data of a different type. Java is object-oriented and, in fact, all functions (or methods) must
belong to some class in a Java program. There are no stand-alone functions in Java.
17.1 A Simple Java Example
There are three basic steps to create and run a Java applet.
- Create the Java Program file using a text editor such as Notepad.
For an example, see the file “welcome.java” below.
- Create the byte-code for the program using a Java compiler. This should create a
file with an extension of .class. The javac command can be accessed on the SVC CIS
computers at
c:\Program Files\jdk1.3.1\bin . Go to that directory and type in:
javac c:\cs105\java\filename.java (or whatever your path and filename are).
If there are no errors, a file called filename.class will be created.
It is this filename that is used in the applet tag.
- Include the class as the value of the code attribute of the
<applet > … </applet> tags. See the example file “java.htm” below.
//File - welcome.java
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
public class welcome extends Applet implements ActionListener
{
//create a textbox for user first name
private Label fnamePrompt = new Label("What is your first name?");
private TextField fnameField = new TextField(12);
private String message = "";
public void init()
{
//initializes the webpage
add(fnamePrompt);
add(fnameField);
fnameField.addActionListener(this);
}
public void actionPerformed(ActionEvent e)
{//respond to user entering name
String fname = fnameField.getText();
message = "Welcome to my first java applet, " + fname;
fnameField.setText(" ");
repaint();
}
public void paint(Graphics g)
{//put message on the screen
g.drawString(message, 1, 80);
}
}
<!--HTML File with Java Applet-->
<html>
<!-- java.htm -->
<head>
<title> Java Applet Example </title>
</head>
<body>
<h2>This page implements a short Java applet. </h2>
<applet alt = "Java applet should be here."
align = "CENTER"
code = "welcome.class"
width = 200
height = 300 >
</applet>
</body>
</html>
References
- Dietel, H. M., Dietel, P. J. & Neito, T. R. (2001) Internet & World Wide Web: How to Program. 2nd Edition. Prentice Hall, NJ.
|