?? exercise14_13.java
字號:
// Exercise14_13.java: Simulate elevators
/* Simulating elevator
Write a program to simulate an elevator going up
and down as shown in Figure 12.10. The buttons on the left
indicating the floor on which the rider is now.
The rider must first click a button on the left to request
the elevator move to the floor to pick up the rider.
When the rider gets inside the elevator, the rider clicks
the buttons on the right to request the elevator to go to
the specified floor.
*/
/*testing notes 10/5/97:
Testing synchronization: Pressing a button before the previous button
action ends.
The synchronization works fine on JDK using java or appletviewer.
It also works fine with jview, but not with IE4.0 Preview II.
*/
// Modified 3/19/2000
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Exercise14_13 extends JApplet {
private JLabel jlblStatue = new JLabel("Status");
private Elevator elevator = new Elevator(this);
public void init() {
// Create left buttons and right buttons
ButtonPanel lb = new ButtonPanel(elevator, true);
ButtonPanel rb = new ButtonPanel(elevator, false);
// Place status label on the north, left buttons on the west
// right buttons on the east and the elevator on the center
// Set applet layout
getContentPane().setLayout(new BorderLayout());
getContentPane().add(jlblStatue, BorderLayout.NORTH);
getContentPane().add(lb, BorderLayout.WEST);
getContentPane().add(elevator, BorderLayout.CENTER);
getContentPane().add(rb, BorderLayout.EAST);
}
// Set status on the status label to indicate left or right buttons are at work
public void setStatus(String status, Color color) {
jlblStatue.setForeground(color);
jlblStatue.setText(status);
}
// Enable the applet to run standalone
// Main method
public static void main(String[] args) {
// Create a frame
JFrame frame = new JFrame("Exercise14_13: Elevator Simulation");
// Create an instance of the applet
Exercise14_13 applet = new Exercise14_13();
// Add the applet instance to the frame
frame.getContentPane().add(applet, BorderLayout.CENTER);
// Invoke init() and start()
applet.init();
applet.start();
// Display the frame
frame.setSize(200, 200);
frame.setVisible(true);
}
}
// The elevator class encapsulates elevator operations
class Elevator extends JPanel implements Runnable {
private Exercise14_13 app; // The applet
private boolean left; // Left or right button
private int destinationFloor = 6; // Elevator destination floor
private int width = 30; // Elevator width
private int height = 30; // Elevator height
private int x = 50; // The x coordinator of the elevator's upper left corner
private int currentY = height*6; // Elevator current location
private int dy = 4; // Moving interval
private Thread thread = null; // Run the while loop on a separate thread
public Elevator(Exercise14_13 app) {
this.app = app; // Passed from applet
setBackground(Color.yellow); // Set elevator background color
thread = new Thread(this); // Create thread
thread.start(); // Move the elevator the initial location, i.e. the first floor
}
// Set elevator color
public void setColor(Color color) {
setForeground(color);
}
// Move the elevator to destination
public void move(int toFloor, boolean left) {
destinationFloor = toFloor;
this.left = left;
thread = new Thread(this);
thread.start();
}
// Run the elevator to the destination, called from move()
public synchronized void run() {
//set status and set elevator color
if (left) {
app.setStatus("getting passengers", Color.black);
this.setColor(Color.green);
}
else {
app.setStatus("sending passengers", Color.red);
this.setColor(Color.red);
}
//newY is the destination floor's y
int newY = (8-destinationFloor)*height;
if (newY < currentY) { //moving up
while (currentY > newY) {
try {
thread.sleep(200);
}
catch (InterruptedException ex) {};
currentY = currentY - dy;
repaint();
}
}
else { // moving down
while (currentY < newY) {
try {
thread.sleep(200);
}
catch (InterruptedException ex) {};
currentY = currentY + dy;
repaint();
}
}
}
// Paint elevator
public void paintComponent(Graphics g) {
super.paintComponent(g);
height = getSize().height/8;
for (int i=0; i<9; i++) {
g.drawLine(0, i*height, this.getSize().width, i*height);
}
g.fillRect(x, currentY, width, height);
}
}
//The ButtonPanel encapsulates buttons
class ButtonPanel extends JPanel implements ActionListener {
private Elevator elevator; // The elevator
private boolean left; // Indicate left or right button
private JButton b[] = new JButton[8]; // Buttons
ButtonPanel(Elevator elevator, boolean left) {
// Pass the elevator, frame, status to the button panel
this.elevator = elevator;
this.left = left;
// Set panel layout
setLayout(new GridLayout(8, 1, 0, 0));
// Set panel background color
setBackground(Color.blue);
// Add buttons to the panel
for (int i=8; i>0; i--)
add(b[i-1] = new JButton("F"+(char)('0'+i)));
// Register listeners
for (int i=0; i<8; i++)
b[i].addActionListener(this);
}
// Handle button actions
public void actionPerformed(ActionEvent e) {
String arg = e.getActionCommand(); // A label like "F8"
if (e.getSource() instanceof JButton)
elevator.move((int)arg.charAt(1)-48, left); // Ascii '0' is 48
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -