import uwcse.graphics.*; import java.awt.Color; import java.awt.Font; /** * Display in a graphics window a traffic light and a menu. */ public class TrafficLightView{ // Menu // Upper left corner public static final int XM = 10; public static final int YM = 350; // size public static final int MENU_WIDTH=65; public static final int MENU_HEIGHT=20; // Graphics elements private GWindow window; private Oval[] lights; /** * Create the graphics window and the menu */ public TrafficLightView(TrafficLightController c) { // Create the display window = new GWindow("Traffic light",500,500); window.setExitOnClose(); // Send all the window events to the controller window.addEventHandler(c); // Background window.add(new Rectangle(0,0,500,500,Color.blue,true)); // Create the menu drawMenu(); // Create the lights drawLights(); // Show it window.doRepaint(); } /** * Update the display of the lights according to the model */ public void updateDisplay(TrafficLightModel model) { // Turn off all of the lights // Get the color of the model and update the colors // of the array lights accordingly // Show the new light window.doRepaint(); } /** * Translate a mouse click location (x,y) into a MouseClickLocation object * (see the definition of the MouseClickLocation class) */ public MouseClickLocation findClickLocation(int x, int y) { // Inactive area if not on the menu // The click is on one of the button items } /** * Start the animation (If we followed the MVC framework to the letter, * we should have put this piece of code * in the controller. But that way we can make use of the timer features * of the GWindow class) * * Start firing an event every dt milliseconds */ public void startTimerEvents(int dt) { window.startTimerEvents(dt); } /** * Stop firing an event at regular intervals */ public void stopTimerEvents() { window.stopTimerEvents(); } /** * Draw the lights in the window */ private void drawLights() { // Create the lights (red,yellow,green) // box of the traffic light window.add(new Rectangle(200,50,100,300,Color.black,true)); // lights } /** * Draw the menu */ private void drawMenu() { // Create the buttons // Upper left corner of each button int x = XM; int y = YM; String[] labels = {"Red", "Yellow", "Green", "Animate"}; Font font = new Font("Times",Font.BOLD,15); for(int i=0; i<4; i++) { window.add(new Rectangle(x,y,MENU_WIDTH,MENU_HEIGHT,Color.black,false)); // label window.add(new TextShape(labels[i],x+5,y,Color.black,font)); // next menu item y+=MENU_HEIGHT; } } }