/*
 * ChangeableTL.java    1.0 97/07/07
 * Copyright (c) 1997, B. F. Caviness
 *
 * This software may be copied/modified for any non-commercial educational use.
 */


import java.awt.*;

/**
 * The <code>class ChangeableTL</code> defines a canvas on which the
 * <code>paint( Graphics )</code> method draws a traffic light.
 * The light that is turned on a given traffic light may be changed
 * by clicking the mouse on a new light.
 * This code uses JDK 1.1 except for the method handleEvent.
 */

/************************************************/
public class ChangeableTL extends TrafficLight
{

    // Named class constants representing possible mouse events for a 
    // traffic light
    protected static final int MD_RedLight = 1; // Event mouse down on red light
    protected static final int MD_YellowLight = 2;// Mouse down on yellow light
    protected static final int MD_GreenLight = 3; // mouse down on green light
    protected static final int MD_NoLight = 4;    // mouse down on no light

    ChangeableTL()		// Constructor
    { super(); }

    ChangeableTL( Color c )	// Constructor
    { super( c ); }

    // Handle mouse events on a traffic light
    public boolean handleEvent( Event e )
    {
        System.out.println("TrafficLight:handleEvent w e = " + e );
        switch ( e.id )
        {
            case Event.MOUSE_DOWN:  int tlEvent = lightClicked( e );
                                    if ( tlEvent != MD_NoLight )
                                         {
                                             switch ( tlEvent )
                                             {
                                                 case MD_RedLight:
                                                     setOnLight( Color.red );
                                                     break;
                                                 case MD_YellowLight:
                                                     setOnLight( Color.yellow );
                                                     break;
                                                 case MD_GreenLight:
                                                     setOnLight( Color.green );
                                                     break;
                                             }
                                             repaint();
                                         }
                                    return true;
            default:  break;
        }
        return super.handleEvent(e);
    }

    // Determine which mouse event occurred
    int lightClicked( Event e )
    {
        ChangeableTL tl = (ChangeableTL)e.target;
       
        // Compute traffic light placement data
        Dimension d = tl.getSize();
        int TLWidth = d.width/4;
        int TLHeight = 3*d.height/4;
        int xOrig = (d.width -TLWidth)/2;
        int yOrig = d.height/8;

        // First check horz coordinate to see if MD possibly occurred on a light
        int rl_xOrig = xOrig + TLWidth/4;
        if ( e.x >= rl_xOrig && e.x <= rl_xOrig + TLHeight/6 )
        {
            // Check vertical coordinate for MD event on red light
            int rl_yOrig = yOrig + TLHeight/8;
            if ( e.y >= rl_yOrig && e.y <= rl_yOrig + TLHeight/6 )
            {
                return MD_RedLight;
            }

            // Check vertical coordinate for MD event on yellow light
            int yl_yOrig = yOrig + 5*TLHeight/12;
            if ( e.y >= yl_yOrig && e.y <= yl_yOrig + TLHeight/6 )
            {
                return MD_YellowLight;
            }

            // Check vertical coordinate for MD event on green light
            int gl_yOrig = yOrig + 17*TLHeight/24;
            if ( e.y >= gl_yOrig && e.y <= gl_yOrig + TLHeight/6 )
            {
                return MD_GreenLight;
            }
        }
        return MD_NoLight;	// Otherwise, MD event occurred on no light
    }
}
