Skip to content

Commit 6165543

Browse files
committed
corejava第10版
1 parent 2021c3c commit 6165543

File tree

1,088 files changed

+94411
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

1,088 files changed

+94411
-0
lines changed
File renamed without changes.
File renamed without changes.
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
import java.awt.*;
2+
import java.io.*;
3+
import javax.swing.*;
4+
5+
/**
6+
* A program for viewing images.
7+
* @version 1.30 2014-02-27
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);
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+
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<html xmlns="http://www.w3.org/1999/xhtml">
2+
<head><title>A Traffic Simulator Applet</title></head>
3+
<body>
4+
<h1>Traffic Simulator Applet</h1>
5+
6+
<p>I wrote this traffic simulation, following the article &quot;Und nun die
7+
Stauvorhersage&quot; of the German Magazine <i>Die Zeit</i>, June 7,
8+
1996. The article describes the work of Professor Michael Schreckenberger
9+
of the University of Duisburg and unnamed collaborators at the University
10+
of Cologne and Los Alamos National Laboratory. These researchers model
11+
traffic flow according to simple rules, such as the following: </p>
12+
<ul>
13+
<li>A freeway is modeled as a sequence of grid points. </li>
14+
<li>Every car occupies one grid point. Each grid point occupies at most
15+
one car. </li>
16+
<li>A car can have a speed of 0 - 5 grid points per time interval. </li>
17+
<li>A car with speed of less than 5 increases its speed by one unit in
18+
each time interval, until it reaches the maximum speed. </li>
19+
<li>If a car's distance to the car in front is <i>d</i> grid points, its
20+
speed is reduced to <i>d</i>-1 if necessary to avoid crashing into it.
21+
</li>
22+
<li>With a certain probability, in each time interval some cars slow down
23+
one unit for no good reason whatsoever. </li>
24+
</ul>
25+
26+
<p>This applet models these rules. Each line shows an image of the same
27+
stretch of road. Each square denotes one car. The first scrollbar lets you
28+
adjust the probability that some cars slow down. If the slider is all the
29+
way to the left, no car slows down. If it is all the way to the right,
30+
every car slows down one unit. A typical setting is that 10% - 20% of the
31+
cars slow down. The second slider controls the arrival rate of the cars.
32+
When it is all the way to the left, no new cars enter the freeway. If it
33+
is all the way to the right, a new car enters the freeway every time
34+
interval, provided the freeway entrance is not blocked. </p>
35+
36+
<p>Try out the following experiments. Decrease the probability of slowdown
37+
to 0. Crank up the arrival rate to 1. That means, every time unit, a new
38+
car enters the road. Note how the road can carry this load. </p>
39+
40+
<p>Now increase the probability that some cars slow down. Note how traffic
41+
jams occur almost immediately. </p>
42+
43+
<p>The moral is: If it wasn't for the rubberneckers, the cellular phone
44+
users, and the makeup-appliers who can't keep up a constant speed, we'd all
45+
get to work more quickly. </p>
46+
47+
<p>Notice how the traffic jam is stationary or even moves backwards, even
48+
though the individual cars are still moving. In fact, the first car
49+
causing the jam has long left the scene by the time the jam gets bad.
50+
(To make it easier to track cars, every tenth vehicle is colored red.) </p>
51+
52+
<p><applet code="RoadApplet.class" archive="RoadApplet.jar"
53+
width="400" height="400" alt="Traffic jam visualization">
54+
</applet></p>
55+
56+
<p>For more information about applets, graphics programming and
57+
multithreading in Java, see
58+
<a href="http://horstmann.com/corejava">Core Java</a>. </p>
59+
</body>
60+
</html>
3.68 KB
Binary file not shown.
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import java.awt.*;
2+
import java.applet.*;
3+
import javax.swing.*;
4+
5+
public class RoadApplet extends JApplet
6+
{
7+
private RoadComponent roadComponent;
8+
private JSlider slowdown;
9+
private JSlider arrival;
10+
11+
public void init()
12+
{
13+
EventQueue.invokeLater(() ->
14+
{
15+
roadComponent = new RoadComponent();
16+
slowdown = new JSlider(0, 100, 10);
17+
arrival = new JSlider(0, 100, 50);
18+
19+
JPanel p = new JPanel();
20+
p.setLayout(new GridLayout(1, 6));
21+
p.add(new JLabel("Slowdown"));
22+
p.add(slowdown);
23+
p.add(new JLabel(""));
24+
p.add(new JLabel("Arrival"));
25+
p.add(arrival);
26+
p.add(new JLabel(""));
27+
setLayout(new BorderLayout());
28+
add(p, BorderLayout.NORTH);
29+
add(roadComponent, BorderLayout.CENTER);
30+
});
31+
}
32+
33+
public void start()
34+
{
35+
new Thread(() ->
36+
{
37+
for (;;)
38+
{
39+
roadComponent.update(
40+
0.01 * slowdown.getValue(),
41+
0.01 * arrival.getValue());
42+
try { Thread.sleep(50); } catch(InterruptedException e) {}
43+
}
44+
}).start();
45+
}
46+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Manifest-Version: 1.0
2+
Permissions: sandbox

0 commit comments

Comments
 (0)