Java programs come in two basic flavors--"applications" that are written to
be run as standalone programs, and "applets," which are written to be run
from within Worldwide Web (WWW) pages.
[See text, pp.14-15 ] Since almost all
of the programs
that we will be working with in these labs are of the applet variety, we
refer to them collectively as "lablets."
In order to run a lablet, you must perform four steps.
[See the picture on p.16 ]
- Produce the source code. The source code for the lablet must be
typed in and saved as a text file. This can be accomplished using
any word processing program, or by using any of the
programs that provide integrated development environments (IDEs) for Java.
The name of this source file must consist of the name of the
"
public class
" defined in the file, followed by the extension
".java". So, for our lablet below in which we define the class
Colors
, we would save our file that
contains the program text with the name "Colors.java".
- Compile the source code. Once the source file has been created,
the next step is to translate it into the Java byte code
[p.16] that can be interpreted
and run on your computer. This, too, can be accomplished in a number of ways,
ranging from invoking the Java compiler (typically called "javac") from a
command line to choosing the "compile" command in an IDE. Whichever compiler
you are using, it will translate your ".java" file ("Colors.java," in this lab)
and produce a second file called a class file (named, in our
example, "Colors.class").
- Make a Web page for the applet.Step three in the process involves
creating an HTML file that references the
class file that we want to run. Many of the project-based IDEs produce such
a file for you automatically. You may, though, have to make a new HTML
textfile (as you did to make "Colors.java") like the sample we provide in
part (f) below.
- Run the applet.It is the HTML file that can be run (or, more
precisely, interpreted and
viewed) by any applet viewer or Java-enabled browser. Again, depending on
your programming environment, this may entail running a browser and opening
your HTML file, sending the HTML file to your applet viewer, or running a
project in an IDE.
Clearly, the details of how you accomplish these steps (creating the Java
source file, compiling it to produce one or more class files, creating an
HTML file that references the class file, and viewing the HTML file) depends
entirely on the combination of tools you have available to you for creating
and running Java programs. Your instructor will provide you with the
information you need to use your tools to accomplish these steps. Once you
have this information, and have access to a computer, you can perform the
following tasks.
Hint: while doing these exercises, you
might want to keep your browser open to this page while you switch to
whatever other programs are necessary to complete the lab. If you're
running your applet in a browser, it's helpful to open a new browser
window so you can read this lab page and watch the applet at the same time.
Type the contents of the source file "Colors.java" exactly as
it appears below. Beware that the Java compiler distinguishes between
upper- and lower-case characters (and, thus, regards the name
Colors
as different from the name colors
).
Hint: in most browsers, you can click and
drag to highlight the code below, select Copy from the Edit
menu, click on the text editor document, and Paste it in. Sure
saves a lot of typing!
// My First Applet
// Watch the colors change as you click the buttons
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class Colors extends Applet implements ActionListener
{
int bgCode = 0,
txtCode = 0;
Button backButton,
textButton;
Font f = new Font("Helvetica",Font.BOLD,18);
public void init()
{
backButton = new Button("Background Color");
add(backButton);
backButton.addActionListener(this);
textButton = new Button("Text Color");
add(textButton);
textButton.addActionListener(this);
}
public void actionPerformed(ActionEvent e)
{
Object source = e.getSource(); // Who generated the event?
if (source == backButton) // if backButton did, do this...
{
bgCode = ++bgCode % 4;
switch (bgCode)
{
case 0: setBackground(Color.cyan); break;
case 1: setBackground(Color.orange); break;
case 2: setBackground(Color.red); break;
case 3: setBackground(Color.green); break;
}
}
else if (source == textButton) // if textButton did, do this...
{
txtCode = ++txtCode % 3;
}
repaint(); // Force a call to paint, so the new colors show up.
}
public void paint(Graphics g)
{
switch (txtCode)
{
case 0: g.setColor(Color.blue); break;
case 1: g.setColor(Color.magenta); break;
case 2: g.setColor(Color.black); break;
}
g.setFont(f);
g.drawString("Goodbye world! Hello Java!", 30, 120);
}
}
- Save the file as "Colors.java".
Caution: If you're using a word processor
to prepare the source file, make sure you save it as text
or ASCII, since
the compiler won't know what to do with the formatting information in,
say, an ordinary WordPerfect or Word document.
- Compile the file "Colors.java".
Notes to myself.
This is what I have to do to compile a Java source file: |
|
- Correct any syntax errors detected by the Java compiler by returning
to the editor, fixing the offending line or lines, and recompiling your
source.
Notes to myself.
Some compile error messages and how to fix them: |
|
- Repeat step (d) until your file compiles successfully,
producing the class file "Colors.class".
Note: No matter how carefully you type, you'll
probably have to perform these "compile/find errors/fix them"
steps several times before the Java code compiles without errors.
-
Run "Colors.class". This may require that you create an HTML
file that refers to your class. [p.17 ]
If so, the HTML file below will work.
Caution: make sure your "Colors.class"
file is in the same directory or folder as the HTML file,
so the browser or applet runner can find it.
<HTML>
<HEAD>
<TITLE>Colors</TITLE>
</HEAD>
<BODY>
<HR>
<APPLET code = "Colors.class" width = 400 height = 200>
</APPLET>
<HR>
<A href = "Colors.java">The source.</A>
</BODY>
</HTML>
Notes to myself.
This is what I have to do to run an applet: |
|
- Make sure that the window in which your applet is running is large
enough to see the text that it displays. This may involve editing the
values of the
width
and height
parameters
in the HTML file created by your IDE to match those above, or it
may simply involve manually resizing the window in which the
applet is being viewed.