/** Tuition - Unit 1- Intro to Java Programming with Applets / Fall 2015
 * @author Larry Wrolstad 
 * @version Project due date 9/14/15 CSci 1130-51 
 * http://www.geocities.ws/eldubs/
 */
import java.awt.*;
import javax.swing.*;
import java.applet.*; //required for audio
import java.awt.event.*;
public class TrafficLight extends JApplet implements ActionListener
{
    AudioClip s1, s2, s3;                       //defining audio for buttons
    JButton redButton, ylwButton, grnButton;
    Image redImg, ylwImg, grnImg;               //defining images for buttons
    ImageIcon icon;
    JLabel imagelabel;
    JPanel butpanel;
    public void init( )
    {
        setLayout( new BorderLayout( ) );
        setupSounds( );
        setupButtons( );                                //Image selects for buttons
        redImg=getImage( getCodeBase( ), "1red.jpg" );      
        ylwImg=getImage( getCodeBase( ), "2yellow.jpg" );
        grnImg=getImage( getCodeBase( ), "3green.jpg" );
        icon = new ImageIcon( );
        imagelabel=new JLabel( icon, JLabel.RIGHT );    
        add( imagelabel, BorderLayout.CENTER );
        setupImage( redImg );
    }
    public void setupSounds( )
    {    
        s1 = getAudioClip( getCodeBase( ), "bellred.wav" );         //Audio selects for buttons
        s2 = getAudioClip( getCodeBase( ), "bellylw.wav" );
        s3 = getAudioClip( getCodeBase( ), "bellgrn.wav" );
    }
    public void setupButtons( )
    {
    redButton = new JButton( "STOP" );          //Button labels
    ylwButton = new JButton( "WAIT" ); 
    grnButton = new JButton( "GO" );
    redButton.addActionListener( this );
    ylwButton.addActionListener( this );
    grnButton.addActionListener( this );
    
    butpanel=new JPanel( new GridLayout( 3, 1 ) );  //Button position
    butpanel.add( redButton );
    butpanel.add( ylwButton );
    butpanel.add( grnButton );
    add( butpanel, BorderLayout.EAST );
}    
public void setupImage( Image img )
{
    icon.setImage( img );
    imagelabel.setIcon( icon );
    repaint( );                     //Allows action to reoccur
}    
public void actionPerformed( ActionEvent ae)
{
    Object source =ae.getSource( );     //Action statements for buttons images
    if( source == redButton )
        setupImage( redImg );
    else if( source == ylwButton )
        setupImage( ylwImg );
    else if( source == grnButton )
        setupImage( grnImg );
        
    Object src = ae.getSource( );       //Action statements for buttons audio
    if ( src == redButton )
    s1.play( );
    else if ( src == ylwButton )
    s2.play( );
    else if ( src == grnButton )
    s3.play( );    
}//new for adding sound


public void setupButton( JButton btn )
{
    btn.addActionListener( this );
    add( btn );
}
public void paint( Graphics g )
{
    super.paint( g );                                       //Street view gif- would be ideal to to place a loop of individual slides
    Image img = getImage( getCodeBase( ), "street.gif" );
    g.drawImage( img, 300, 300,  this ); 
    
    getContentPane().setBackground(Color.BLACK); //Set background color
    
}
}

                
        
            
                       