(Continues)
31
2.4 Running a Graphical Application
From the Library of Hristo Dimov Hristov
ptg18360597
Listing 2.2
(Continued)
8
* @author Cay Horstmann
9
*/
10
public class ImageViewer
11
{
12
public static void main(String[] args)
13
{
14
EventQueue.invokeLater(() -> {
15
JFrame frame = new ImageViewerFrame();
16
frame.setTitle("ImageViewer");
17
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
18
frame.setVisible(true);
19
});
20
}
21
}
22
23
/**
24
* A frame with a label to show an image.
25
*/
26
class ImageViewerFrame extends JFrame
27
{
28
private JLabel label;
29
private JFileChooser chooser;
30
private static final int DEFAULT_WIDTH = 300;
31
private static final int DEFAULT_HEIGHT = 400;
32
33
public ImageViewerFrame()
34
{
35
setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
36
37
// use a label to display the images
38
label = new JLabel();
39
add(label);
40
41
// set up the file chooser
42
chooser = new JFileChooser();
43
chooser.setCurrentDirectory(new File("."));
44
45
// set up the menu bar
46
JMenuBar menuBar = new JMenuBar();
47
setJMenuBar(menuBar);
48
49
JMenu menu = new JMenu("File");
50
menuBar.add(menu);
51
52
JMenuItem openItem = new JMenuItem("Open");
53
menu.add(openItem);
Chapter 2
The Java Programming Environment
32
From the Library of Hristo Dimov Hristov
ptg18360597
54
openItem.addActionListener(event -> {
55
// show file chooser dialog
56
int result = chooser.showOpenDialog(null);
57
58
// if file selected, set it as icon of the label
59
if (result == JFileChooser.APPROVE_OPTION)
60
{
61
String name = chooser.getSelectedFile().getPath();
62
label.setIcon(new ImageIcon(name));
63
}
64
});
65
66
JMenuItem exitItem = new JMenuItem("Exit");
67
menu.add(exitItem);
68
exitItem.addActionListener(event -> System.exit(0));
69
}
70
}
2.5 Building and Running Applets
The first two programs presented in this book are Java applications—stand-alone
programs like any native programs. On the other hand, as mentioned in the pre-
vious chapter, most of the early hype about Java came from its ability to run applets
inside a web browser.
If you are interested in experiencing a “blast from the past,” follow along to see
how to build and run an applet and how to display it in a web browser; if you
aren’t interested, by all means, skip this example and move on to Chapter 3.
Open a terminal window and go to the directory
corejava/v1ch02/RoadApplet
, then enter
the following commands:
javac RoadApplet.java
jar cvfm RoadApplet.jar RoadApplet.mf *.class
appletviewer RoadApplet.html
Figure 2.9 shows what you see in the applet viewer window. This applet visualizes
how traffic jams can be caused by drivers who randomly slow down. In 1996,
applets were a great tool for creating such visualizations.
The first command is the now-familiar command to invoke the Java compiler.
This compiles the
RoadApplet.java
source into the bytecode file
RoadApplet.class
.
This time, however, you do not run the
java
program. First, you bundle the class
files into a “JAR file,” using the
jar
utility. Then you invoke the
appletviewer
program,
a tool included with the JDK that lets you quickly test an applet. You need to give
this program an HTML file name, rather than the name of a Java class file. The
contents of the
RoadApplet.html
file are shown at the end of this section in Listing 2.3.
33
2.5 Building and Running Applets
From the Library of Hristo Dimov Hristov
ptg18360597
Figure 2.9
The RoadApplet as viewed by the applet viewer
If you are familiar with HTML, you will notice standard HTML markup and the
applet
tag, telling the applet viewer to load the applet whose code is stored in
RoadApplet.jar
. The applet viewer ignores all HTML tags except for the
applet
tag.
Of course, applets are meant to be viewed in a browser. Unfortunately, nowadays,
many browsers do not have Java support, or make it difficult to enable it. Your
best bet is to use Firefox.
If you use Windows or Mac OS X, Firefox should automatically pick up the Java
installation on your computer. Under Linux, you need to enable the plug-in with
the following commands:
mkdir -p ~/.mozilla/plugins
cd ~/.mozilla/plugins
ln -s
jdk
/jre/lib/amd64/libnpjp2.so
To double-check, type
about:plugins
into the address bar and look for the Java Plug-in.
Make sure it uses the Java SE 8 version of the plug-in—look for a MIME type of
application/x-java-applet;version=1.8
.
Next, turn your browser to
http://horstmann.com/applets/RoadApplet/RoadApplet.html
, agree to
all the scary security prompts, and make sure the applet appears.
Chapter 2
The Java Programming Environment
34
From the Library of Hristo Dimov Hristov
ptg18360597
Unfortunately, that is not enough to test the applet that you just compiled. The
applet on the
horstmann.com
server is digitally signed. I had to expend some effort,
getting a certificate issuer that is trusted by the Java virtual machine to trust me
and sell me a certificate, which I used to sign the JAR file. The browser plug-in
will no longer run untrusted applets. This is a big change from the past, when a
simple applet that draws pixels on the screen would have been confined to the
“sandbox” and would work without being signed. Sadly, not even Oracle has
faith in the security of the sandbox any more.
To overcome this problem, you can temporarily configure Java to trust applets
from the local file system. First, open the Java control panel.
•
In Windows, look inside the Programs section of the control panel.
•
On a Mac, open System Preferences.
•
On Linux, run
jcontrol
.
Then click the Security tab and the Edit Site List button. Click Add and type in
file:///
. Click OK, accept another security prompt, and click OK again (see
Figure 2.10).
Figure 2.10
Configuring Java to trust local applets
Now you should be able to load the file
corejava/v1ch02/RoadApplet/RoadApplet.html
into
your browser and have the applet appear, together with the surrounding text. It
will look something like Figure 2.11.
35
2.5 Building and Running Applets
From the Library of Hristo Dimov Hristov
ptg18360597
Figure 2.11
Running the RoadApplet in a browser
The code for the applet class is shown in Listing 2.4. At this point, do not give it
more than a glance. We will come back to writing applets in Chapter 13.
Listing 2.3
RoadApplet/RoadApplet.html
1
2
A Traffic Simulator Applet
3
4
Do'stlaringiz bilan baham: |