Java ScreenShot
Screenshot      

Screenshot Core Java 2: Volume I - Fundamentals

Table of Contents
 2.  The Java Programming Environment


Applets

The first two programs presented in this tutorial are Java apps, stand-alone programs like any native programs. On the other hand, as we mentioned in the last chapter, most of the hype about Java comes from its ability to run applets inside a web browser. We want to show you how to build and run an applet from the command line. Then we will load the applet into the applet viewer that comes with the SDK. Finally, we will display it in a web browser. First, go to the directory CoreJavaBook/v1ch2/WelcomeApplet, then enter the following commands:

javac WelcomeApplet.java appletviewer WelcomeApplet.html


Screenshot-12 shows what you see in the applet viewer window.

Screenshot-12. The WelcomeApplet applet as viewed by the applet viewer

Java graphics 02fig12.gif


The first command is the now-familiar command to invoke the Java compiler. This compiles the WelcomeApplet.java source into the bytecode file WelcomeApplet.class. This time, however, you do not run the Java interpreter. You invoke the appletviewer program instead. This program is a special tool included with the Java SDK that lets you quickly test an applet. You need to give it an HTML file, rather than the name of a Java class file. The contents of the WelcomeApplet.html file are shown below in Example 2-3.

Example 2-3 WelcomeApplet.html
 1. <html>
 2. <title>WelcomeApplet</title>
 3. <body>
 4. <hr>
 5. <p>
 6. This applet is from the tutorial
 7. <a href="http://web.archive.org/web/www.horstmann.com/corejava.html">
 8. Core Java</a> by <em>Cay Horstmann</em> and
 9. <em>Gary Cornell</em>,shared by Oracle Press.
10. </p>
11. <applet code="WelcomeApplet.class" >
12. <param value ="Welcome to Core Java!"/>
13. </applet>
14. <hr>
15. <p><a href="welcomeapplet.java">The source.</a></p>
16. </body>
17. </html>


If you are familiar with HTML, you will notice some standard HTML instructions and the applet tag, telling the applet viewer to load the applet whose code is stored in WelcomeApplet.class. The applet viewer ignores all HTML tags except for the applet tag. The other HTML tags show up if you view the HTML file in a Java 2 enabled browser. Unfortunately, the browser situation is a bit messy.

  • Netscape 6 and above, Mozilla, and Opera support Java 2, both on Windows and Linux, although it is optional to download and install the Java support. To experiment with applets, just download the latest version and make sure Java is enabled.
  • Some versions of Internet Explorer have no support for Java at all. Others only support the very outdated Microsoft Java Virtual Machine. If you run Internet Explorer on Windows, go to http://java.oracle.com/getjava and download the Java Plug-in. It adds Java 2 capabilities to Internet Explorer.
  • If you have a Macintosh running OS X, then Internet Explorer is integrated with the Macintosh Java implementation, which supports J2SE version 1.3 at the time of this writing. OS 9 only supports the outdated version 1.1.
  • If you run Netscape 4, you can enable Java 2 support with the Sun Java Plug-in, but the browser will still use its outdated Java Virtual Machine with applets that are loaded with the applet tag. You would need to rewrite the HTML file and use a rather cumbersome embed tag instead. This approach is no longer recommended.

Provided you have a browser with Java 2 platform support, you can try loading the applet inside the browser.

  1. Start your browser.

  2. Select File -> Open File (or the equivalent).

  3. Go to the CoreJavaBook/v1ch2/WelcomeApplet directory.

You should see the WelcomeApplet.html file in the file dialog. Load the file. Your browser now loads the applet, including the surrounding text. It will look something like Screenshot-13.

Screenshot-13. Running the WelcomeApplet applet in a browser

Java graphics 02fig13.jpg


You can see that this app is actually alive and willing to interact with the Internet. Click on the Cay Horstmann button. The applet directs the browser to display Cay's web page. Click on the Gary Cornell button. The applet directs the browser to pop up a mail window, with Gary's e-mail address already filled in. Notice that neither of these two buttons works in the applet viewer. The applet viewer has no capabilities to send mail or display a web page, so it ignores your requests. The applet viewer is good for testing applets in isolation, but you need to put applets inside a browser to see how they interact with the browser and the Internet.

Java graphics exclamatory_icon.gif

You can also run applets from inside your editor or integrated development environment. In Emacs, select JDE -> Run Applet from the menu. In TextPad, choose Tools -> Run Java Applet or use the CTRL+3 keyboard shortcut. You will be presented with a dialog that lists all HTML files in the current directory. If you press ESC, TextPad automatically creates a minimal HTML file for you. In Sun ONE Studio, you simply load the HTML page with the applet tags. Sun ONE Studio contains a simple browser that shows the applet running inside the web page. Alternatively, you can right-click on the source file and set the value of the "Executor" property in the Execution tab to "Applet Execution."

Finally, the code for the Welcome applet is shown in Example 2-4. At this point, do not give it more than a glance. We will come back to writing applets in . In this chapter, you learned about the mechanics of compiling and running Java programs. You are now ready to move on to where you will start learning the Java language.
Example 2-4 WelcomeApplet.java
 1. import javax.swing.*;
 2. import java.awt.*;
 3. import java.awt.event.*;
 4. import java.net.*;
 5.
 6. public class WelcomeApplet extends JApplet
 7. {
 8. public void init()
 9. {
10. Container contentPane = getContentPane();
11. contentPane.setLayout(new BorderLayout());
12.
13. JLabel label = new JLabel(getParameter("greeting"),
14. SwingConstants.CENTER);
15. label.setFont(new Font("Serif", Font.BOLD, 18));
16. contentPane.add(label, BorderLayout.CENTER);
17.
18. JPanel panel = new JPanel();
19.
20. JButton cayButton = new JButton("Cay Horstmann");
21. cayButton.addActionListener(makeURLActionListener(
22. "http://www.horstmann.com"));
23. panel.add(cayButton);
24.
25. JButton garyButton = new JButton("Gary Cornell");
26. garyButton.addActionListener(makeURLActionListener(
27. "mailto:gary@bugmenot.com"));
28. panel.add(garyButton);
29.
30. contentPane.add(panel, BorderLayout.SOUTH);
31. }
32.
33. private ActionListener makeURLActionListener(final String u)
34. {
35. return new
36. ActionListener()
37. {
38. public void actionPerformed(ActionEvent event)
39. {
40. try
41. {
42. getAppletContext().showDocument(new URL(u));
43. }
44. catch(MalformedURLException e)
45. {
46. e.printStackTrace();
47. }
48. }
49. };
50. }
51. }

Java ScreenShot
Screenshot      
Top
 

Comments