Most read:
Popular archives:
JavaWorld's new look is here!
We've upgraded the site with a fresh look-and-feel, improved topical navigation, better search, new features, and expanded
community platform. Learn more about the changes to JavaWorld.
| Oracle Compatibility Developer's Guide |
| The Explosion in DBMS Choice |
For nearly 10 years, I've developed Java programs for the IT industry, written articles and books on Java, answered many Java-related questions, and taught Java to lots of students. Although I've worked hard learning, programming, and teaching Java, I've also had fun with this technology.
Java Fun and Games shares some of the pleasure I've experienced while playing with Java. Each installment focuses on a specific topic that I've found to be entertaining and presents one or more Java programs I created while exploring that topic. As you journey through Java Fun and Games, you'll encounter topics that range from dragging a checker around a checkerboard to creating sophisticated 3D particle systems. I hope you enjoy the journey.
Because of my fondness for computer games, I'll emphasize them in this column.
In addition to presenting my own entertainment topics, I'd like to explore topics important to you. Feel free to suggest a topic you would like to see covered and I will consider it.
Each installment will present one or more programs in the form of Java applets. (I have nothing against applications—I just want to focus on applets.) I use J2SE 1.4 to compile (via javac) the applet's source code and run (via appletviewer) the applet's executable code.
For the most part, I won't use a Web browser to run applets. If you want to run an applet in a Web browser, make certain your
browser supports J2SE 1.4 (or a more recent version). For example, if you are using Version 6.0 of Microsoft's Internet Explorer,
select the Use Java 2 v1.4.0 for <applet> (or equivalent) checkbox. That way, Internet Explorer will use Java Plug-in 1.4
(or a more recent version) instead of its internal JVM when it encounters the <applet> tag.
If you cannot access Java Plug-in from your Web browser, you will probably need to create Java 1.x-compatible classfiles (assuming
the source code doesn't contain Java features newer than Java 1.x, and assuming you are accessing built-in virtual machines
that don't recognize classfiles newer than version 1.x). Create Java 1.x-compatible classfiles by compiling their source codes
with javac's target option, as follows:
javac -target 1.1 ClassName.java
In the command line above, -target 1.1 tells javac to create a classfile compatible with Java 1.1 (which should be recognized by most built-in virtual machines).
Also, ClassName.java is just a placeholder for the applet's source file.
Some applets will need to access the filesystem (to read/save game stats, for example). The JVM's security manager forbids
this kind of activity. However, there are two techniques for getting around this: one technique for appletviewer and one technique
for a Web browser. Before I discuss these techniques, examine Listing 1's FileIO.java source code.
Listing 1. FileIO.java
// FileIO.java
import java.awt.*;
import java.awt.event.*;
import java.io.*;
public class FileIO extends java.applet.Applet
{
private static String FILENAME = "stats.dat";
private String name;
private int score;
public void init ()
{
// Create status TextArea component.
final TextArea taStatus = new TextArea (10, 40);
// Create a Panel for organizing Button components.
Panel p = new Panel ();
// Create a Load Button that loads game stats.
Button btnLoad = new Button ("Load game stats");
// Establish a file-loading action listener.
btnLoad.addActionListener (new ActionListener ()
{
public void actionPerformed (ActionEvent e)
{
DataInputStream dis = null;
try
{
FileInputStream fis;
fis = new FileInputStream
(File.separator +FILENAME);
dis = new DataInputStream (fis);
name = dis.readUTF ();
score = dis.readInt ();
taStatus.setText ("name = " + name + ", " + "hi score = " + score);
}
catch (IOException e2)
{
taStatus.setText (e2.toString ());
}
finally
{
if (dis != null)
try
{
dis.close ();
}
catch (IOException e2)
{
taStatus.setText (e2.toString ());
}
}
}
});
// Add Load Button to Panel.
p.add (btnLoad);
// Create a Save Button that saves game stats.
Button btnSave = new Button ("Save game stats");
// Establish a file-saving action listener.
btnSave.addActionListener (new ActionListener ()
{
public void actionPerformed (ActionEvent e)
{
DataOutputStream dos = null;
try
{
FileOutputStream fos;
fos = new FileOutputStream
(File.separator + FILENAME);
dos = new DataOutputStream (fos);
dos.writeUTF ("John Doe");
dos.writeInt (50000);
taStatus.setText ("Name and " + "score saved");
}
catch (IOException e2)
{
taStatus.setText (e2.toString ());
}
finally
{
if (dos != null)
try
{
dos.close ();
}
catch (IOException e2)
{
taStatus.setText (e2.toString ());
}
}
}
});
// Add Save Button to Panel.
p.add (btnSave);
// Set applet's layout manager to BorderLayout.
setLayout (new BorderLayout ());
// Add Button panel to north region of applet.
add (p, BorderLayout.NORTH);
// Add status TextArea to south region of applet.
add (taStatus, BorderLayout.SOUTH);
}
}
Listing 1 describes a simple applet that demonstrates file I/O in an applet context. When a user clicks the Save Game Stats
button, the applet saves a name and score to a data file. When the user clicks Load Game Stats, the applet loads the previously-saved
name and score from the data file. With either button click, information appears in a status text area. Compile FileIO.java by invoking the command line below:
Archived Discussions (Read only)