📄 trafficcanvas.java
字号:
//// The contents of this file are subject to the Mozilla Public// License Version 1.1 (the "License"); you may not use this file// except in compliance with the License. You may obtain a copy// of the License at http://www.mozilla.org/MPL/// // Software distributed under the License is distributed on an// "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or// implied. See the License for the specific language governing// rights and limitations under the License.// // The Original Code is State Machine Compiler (SMC).// // The Initial Developer of the Original Code is Charles W. Rapp.// Portions created by Charles W. Rapp are// Copyright (C) 2000 - 2003 Charles W. Rapp.// All Rights Reserved.// // Contributor(s): //// Name// TrafficCanvas//// Description// Responsible for coordinating the stoplights and vehicles.//// RCS ID// $Id: TrafficCanvas.java,v 1.6 2007/02/21 13:38:38 cwrapp Exp $//// CHANGE LOG// $Log: TrafficCanvas.java,v $// Revision 1.6 2007/02/21 13:38:38 cwrapp// Moved Java code to release 1.5.0//// Revision 1.5 2005/05/28 13:51:24 cwrapp// Update Java examples 1 - 7.//// Revision 1.0 2003/12/14 20:04:00 charlesr// Initial revision//package smc_ex4;import java.awt.BasicStroke;import java.awt.Canvas;import java.awt.Color;import java.awt.Dimension;import java.awt.Graphics;import java.awt.Graphics2D;import java.awt.Point;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.geom.Line2D;import java.awt.geom.Rectangle2D;import java.util.Iterator;import java.util.LinkedList;import java.util.List;public final class TrafficCanvas extends Canvas{//---------------------------------------------------------------// Member methods.// public TrafficCanvas() { setSize(CANVAS_SIZE.width, CANVAS_SIZE.height); // Create the stop light object. _stopLight = new Stoplight(CANVAS_SIZE.width, CANVAS_SIZE.height, this); // Create an empty vehicle list. Vehicles will be added // when the "NewVehicle" timer expires. _vehicleList = new LinkedList<Vehicle>(); _vehicleSpeed = 2; // When vehicles move off the canvas, add them to this // remove list for later removal. _removeList = new LinkedList<Vehicle>(); _newVehicleTimerDuration = 4000; _lightChanged = false; _stoplightQueue = new LinkedList[4]; for (int i = 0; i < 4; ++i) { _stoplightQueue[i] = new LinkedList(); } } public int getMinX() { return(CANVAS_CORNER.x); } public int getMaxX() { return((CANVAS_CORNER.x + CANVAS_SIZE.width) - VEHICLE_SIZE.width); } public int getMinY() { return(CANVAS_CORNER.y); } public int getMaxY() { return(CANVAS_CORNER.y + CANVAS_SIZE.height - VEHICLE_SIZE.height); } // Get the stoplight's location and size. public void getLightDimensions(Point direction, Point lightLocation, Dimension lightSize) { _stopLight.getLightDimensions(direction, lightLocation, lightSize); return; } // Return the current stop light timers. public int getLightDuration(int direction) { // Convert the duration from milliseconds to seconds. return(_stopLight.getLightDuration(direction) / 1000); } public void setLightDuration(int direction, int duration) { // Convert the duration from seconds to milliseconds. _stopLight.setLightDuration(direction, (duration * 1000)); return; } public int getNewVehicleRate() { return(_newVehicleTimerDuration / 1000); } public void setNewVehicleRate(int duration) { _newVehicleTimerDuration = (duration * 1000); // Stop the timer, reset its value and start it again. if (_newVehicleTimer != null) { long currTime; _newVehicleTimer.stop(); currTime = System.currentTimeMillis(); _newVehicleTimer.setDelay(_newVehicleTimerDuration); _newVehicleTimer.start(); _nextNewVehicleTimeout = currTime + _newVehicleTimerDuration; } return; } public int getVehicleSpeed() { return(_vehicleSpeed); } public void setVehicleSpeed(int speed) { _vehicleSpeed = speed; // Tell all vehicles their new speed. for (Vehicle vehicle: _vehicleList) { vehicle.setSpeed(speed); } return; } public void paint(Graphics g) { Graphics2D g2 = (Graphics2D) g; // First, lay down a white rectangle. g2.setPaint(Color.white); g2.fill(new Rectangle2D.Double(CANVAS_CORNER.getX(), CANVAS_CORNER.getY(), CANVAS_SIZE.getWidth(), CANVAS_SIZE.getHeight())); // Draw the four green rectangles in each corner. // The rectangle's width and height will be 40% of the // canvas area. The coordinates are: // // x[0] x[1] x[2] x[3] x[4] // y[0] +--------+ +--------+ // | | | | // y[1] +--------+ +--------+ // y[2] // y[3] +--------+ +--------+ // | | | | // y[4] +--------+ +--------+ // // Draw each of the four rectangles. g2.setPaint(Color.green); g2.fill(new Rectangle2D.Double(RECT_CORNERS[0].x, RECT_CORNERS[0].y, FIELD_SIZE.width, FIELD_SIZE.height)); g2.fill(new Rectangle2D.Double(RECT_CORNERS[3].x, RECT_CORNERS[0].y, FIELD_SIZE.width, FIELD_SIZE.height)); g2.fill(new Rectangle2D.Double(RECT_CORNERS[0].x, RECT_CORNERS[3].y, FIELD_SIZE.width, FIELD_SIZE.height)); g2.fill(new Rectangle2D.Double(RECT_CORNERS[3].x, RECT_CORNERS[3].y, FIELD_SIZE.width, FIELD_SIZE.height)); // Now draw the lane markings. Start with the line on the // north side and go clockwise. g2.setPaint(Color.black); g2.setStroke(DASHED); g2.draw(new Line2D.Double(RECT_CORNERS[2].x, RECT_CORNERS[0].y, RECT_CORNERS[2].x, RECT_CORNERS[1].y)); g2.draw(new Line2D.Double(RECT_CORNERS[3].x, RECT_CORNERS[2].y, RECT_CORNERS[4].x, RECT_CORNERS[2].y)); g2.draw(new Line2D.Double(RECT_CORNERS[2].x, RECT_CORNERS[3].y, RECT_CORNERS[2].x, RECT_CORNERS[4].y)); g2.draw(new Line2D.Double(RECT_CORNERS[0].x, RECT_CORNERS[2].y, RECT_CORNERS[1].x, RECT_CORNERS[2].y)); // Paint the vehicles. for (Vehicle vehicle: _vehicleList) { vehicle.paint(g2); } // Have the stop light draw itself. _stopLight.paint(g); return; } // Start the demo running by starting the timers. public void startDemo() { // Have the stop light go to its initial settings. _stopLight.startDemo(); // Start the timers. startNewVehicleTimer(); startRepaintTimer(); return; } public synchronized void pauseDemo() { // Kill the timers for now but leave the graphic items // displayed. if (_newVehicleTimer != null && _newVehicleTimer.isRunning() == true) { long currTime = System.currentTimeMillis(); long timeLeft; _newVehicleTimer.stop(); // Figure out the number of milliseconds to the // next new vehicle timeout and set the timer's // initial delay to that. timeLeft = _nextNewVehicleTimeout - currTime; if (timeLeft < INITIAL_NEW_VEHICLE_DELAY) { timeLeft = INITIAL_NEW_VEHICLE_DELAY; } _newVehicleTimer.setInitialDelay((int) timeLeft); } if (_repaintTimer != null && _repaintTimer.isRunning() == true) { _repaintTimer.stop(); } _stopLight.pauseDemo(); return; } public synchronized void continueDemo() { // Get the timers up and running again. startNewVehicleTimer(); startRepaintTimer(); _stopLight.continueDemo(); return; } public synchronized void stopDemo() { // Stop the timers and delete all vehicles. if (_newVehicleTimer != null && _newVehicleTimer.isRunning() == true) { _newVehicleTimer.stop(); } if (_repaintTimer != null && _repaintTimer.isRunning() == true) { _repaintTimer.stop(); } for (Vehicle vehicle: _vehicleList) { vehicle.stopDemo(getGraphics()); } _vehicleList.clear(); _stopLight.stopDemo(); return; } public synchronized void lightChanged(int lightDirection) { // Tell all the vehicles stopped at the light that they // can go. switch (lightDirection) { case EW_LIGHT: for (Vehicle vehicle: _stoplightQueue[EASTLIGHT]) { vehicle.lightGreen(); } _stoplightQueue[EASTLIGHT].clear(); for (Vehicle vehicle: _stoplightQueue[WESTLIGHT]) { vehicle.lightGreen(); } _stoplightQueue[WESTLIGHT].clear(); break; case NS_LIGHT: for (Vehicle vehicle: _stoplightQueue[NORTHLIGHT]) { vehicle.lightGreen(); } _stoplightQueue[NORTHLIGHT].clear(); for (Vehicle vehicle: _stoplightQueue[SOUTHLIGHT]) { vehicle.lightGreen(); } _stoplightQueue[SOUTHLIGHT].clear(); break; case YELLOW_LIGHT: default: break; } // The stop light is informing us that it has // changed. If the repaint timer is not running, // turn it one now so that the stop light can // be repainted. _lightChanged = true; return; } public synchronized void handleNewVehicleTimeout() { Point startingPoint = new Point(); Vehicle vehicle; // Figure out the time to the next timeout. _nextNewVehicleTimeout = System.currentTimeMillis() + _newVehicleTimerDuration; // Create a new vehicle for each size and start them // on their way. Tell the vehicle where it will // begin, in what direction it is to travel and on // what canvas it appears. // Start with the east-bound vehicle starting from // the west edge. startingPoint.setLocation( 0, (RECT_CORNERS[2].y + CURB_OFFSET)); vehicle = new Vehicle(startingPoint, EAST, _vehicleSpeed, VEHICLE_SIZE, this); // Have the vehicle paint itself on the canvas. vehicle.paint((Graphics2D) getGraphics()); // Put this new vehicle on the list.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -