📄 stoplight.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// Stoplight.java//// Description// This class paints the stoplights on the canvas and changes// the lights' color.//// RCS ID// $Id: Stoplight.java,v 1.5 2005/05/28 13:51:24 cwrapp Exp $//// CHANGE LOG// $Log: Stoplight.java,v $// Revision 1.5 2005/05/28 13:51:24 cwrapp// Update Java examples 1 - 7.//// Revision 1.0 2003/12/14 20:02:09 charlesr// Initial revision//package smc_ex4;import java.awt.*;import java.awt.event.*;import java.awt.geom.*;import javax.swing.*;public final class Stoplight{// Member methods. public Stoplight(int canvasWidth, int canvasHeight, TrafficCanvas owner) { // Remember on which canvas this stop light is being // drawn. _owner = owner; // Calculate the stop light's width and height. // The width is equal to the individual lamp's diameter // plus space on either side. // The height is equal to three lamps plus four spaces. _size = new Dimension((LAMP_DIAMETER + (LAMP_SPACE * 2)), ((LAMP_DIAMETER * 3) + (LAMP_SPACE * 4))); // Now calculate the stop light's coordinates based on // the following diagram: // // y[0] +---+ // | o | g // | o | y // | o | r y g // y[1] +-------+---+-------+ // | o o o | c | o o o | // y[2] +-------+---+-------+ // g y r | o | // y | o | // g | o | // y[3] +---+ // x[0] x[1] x[2] x[3] // _positions = new Point[4]; _positions[0] = new Point((((int) (canvasWidth / 2)) - ((int) (_size.width / 2)) - _size.height), (((int) (canvasHeight / 2)) - ((int) (_size.width / 2)) - _size.height)); _positions[1] = new Point((_positions[0].x + _size.height), (_positions[0].y + _size.height)); _positions[2] = new Point((_positions[1].x + _size.width), (_positions[1].y + _size.width)); _positions[3] = new Point((_positions[2].x + _size.height), (_positions[2].y + _size.height)); // There are four lights: north, east, south and west. // Each light has four bulbs: green, yellow and red. _lights = new Color[4][3]; InitLights(); // Set the initial light durations. _nsLightDuration = 8000; _ewLightDuration = 10000; _yellowDuration = 4000; // Create the stop light's state machine. _fsm = new StoplightContext(this); // Uncomment to see debug output. // _fsm.setDebugFlag(true); } public Color getLightsColor(int direction) { boolean found; int i; Color retval = Color.white; // Go through the light's bulbs looking for // one that is *not* black. for (i = 0, found = false; found == false && i < 3; ++i) { if (_lights[direction][i].getRGB() != Color.black.getRGB()) { retval = _lights[direction][i]; found = true; } } return(retval); } public void getLightDimensions(Point direction, Point lightLocation, Dimension lightSize) { // If the vehicle is heading north, it will pass under // the east light. if (direction.x == _owner.NORTH.x && direction.y == _owner.NORTH.y) { lightLocation.x = _positions[2].x; lightLocation.y = _positions[1].y; lightSize.width = _size.height; lightSize.height = _size.width; } // If the vehicle is heading east, it will pass under the // south light. else if (direction.x == _owner.EAST.x && direction.y == _owner.EAST.y) { lightLocation.x = _positions[1].x; lightLocation.y = _positions[2].y; lightSize.width = _size.width; lightSize.height = _size.height; } // If the vehicle is heading south, it will pass under // the west light. else if (direction.x == _owner.SOUTH.x && direction.y == _owner.SOUTH.y) { lightLocation.x = _positions[0].x; lightLocation.y = _positions[1].y; lightSize.width = _size.height; lightSize.height = _size.width; } // If the vehicle is heading west, it will pass under the // north light. else if (direction.x == _owner.WEST.x && direction.y == _owner.WEST.y) { lightLocation.x = _positions[1].x; lightLocation.y = _positions[0].y; lightSize.width = _size.width; lightSize.height = _size.height; } return; } public int getLightDuration(int light) { int duration; switch (light) { case TrafficCanvas.NS_LIGHT: duration = _nsLightDuration; break; case TrafficCanvas.EW_LIGHT: duration = _ewLightDuration; break; case TrafficCanvas.YELLOW_LIGHT: default: duration = _yellowDuration; break; } return(duration); } public void setLightDuration(int light, int duration) { switch (light) { case TrafficCanvas.NS_LIGHT: _nsLightDuration = duration; break; case TrafficCanvas.EW_LIGHT: _ewLightDuration = duration; break; case TrafficCanvas.YELLOW_LIGHT: _yellowDuration = duration; break; } return; } public void startDemo() { _fsm.Start(); return; } public void pauseDemo() { _fsm.Pause(); return; } public void continueDemo() { _fsm.Continue(); return; } public void stopDemo() { _fsm.Stop(); return; } public void paint(Graphics g) { Graphics2D g2 = (Graphics2D) g; // Draw the four stop light boxes. Start with the // northern light and go clockwise. g2.setPaint(Color.black); g2.fill(new Rectangle2D.Double(_positions[1].x, _positions[0].y, _size.width, _size.height)); g2.fill(new Rectangle2D.Double(_positions[2].x, _positions[1].y, _size.height, _size.width)); g2.fill(new Rectangle2D.Double(_positions[1].x, _positions[2].y, _size.width, _size.height)); g2.fill(new Rectangle2D.Double(_positions[0].x, _positions[1].y, _size.height, _size.width)); // Now draw the lamps within the stop lights. // Again, start with the northern light. g2.setPaint(_lights[NORTH][GREEN]); g2.fill(new Ellipse2D.Double((_positions[1].x + LAMP_SPACE), (_positions[0].y + LAMP_SPACE), LAMP_DIAMETER, LAMP_DIAMETER)); g2.setPaint(_lights[NORTH][YELLOW]); g2.fill(new Ellipse2D.Double((_positions[1].x + LAMP_SPACE), (_positions[0].y + (LAMP_SPACE * 2) + LAMP_DIAMETER), LAMP_DIAMETER, LAMP_DIAMETER)); g2.setPaint(_lights[NORTH][RED]); g2.fill(new Ellipse2D.Double((_positions[1].x + LAMP_SPACE), (_positions[0].y + (LAMP_SPACE * 3) + (LAMP_DIAMETER * 2)), LAMP_DIAMETER, LAMP_DIAMETER)); // Eastern light. g2.setPaint(_lights[EAST][RED]); g2.fill(new Ellipse2D.Double((_positions[2].x + LAMP_SPACE), (_positions[1].y + LAMP_SPACE), LAMP_DIAMETER, LAMP_DIAMETER)); g2.setPaint(_lights[EAST][YELLOW]); g2.fill(new Ellipse2D.Double((_positions[2].x + (LAMP_SPACE * 2) + LAMP_DIAMETER), (_positions[1].y + LAMP_SPACE), LAMP_DIAMETER, LAMP_DIAMETER)); g2.setPaint(_lights[EAST][GREEN]); g2.fill(new Ellipse2D.Double((_positions[2].x + (LAMP_SPACE * 3) + (LAMP_DIAMETER * 2)), (_positions[1].y + LAMP_SPACE), LAMP_DIAMETER, LAMP_DIAMETER)); // Southern light.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -