/**
 *  The GUIDisplay turtle displays the same basic user interface as GUI, but it does
 *	it within the Display.  This shows how to get the display panel and add graphical 
 *  elements to it.
 *
 *  Note the use of waitForStop() at the end of the runTurtle() method.  This causes
 *  the turtle to not finish until the Stop button has been clicked or the stopTurtle() 
 *  method has been called.
 */

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import com.otherwise.jurtle.*;

public class GUIDisplay extends Turtle
{

    JButton redBtn;
    JButton greenBtn;
    JButton blueBtn;
    Container displayPanel;

    /**
     *  Main entry point to the code.  Creates the GUI within the Display
     *  area calls waitForStop to wait until the Stop button is clicked.
     */
    public void runTurtle()
    {
        displayPanel = getDisplay();
        displayPanel.setLayout( new FlowLayout() );

        redBtn = new JButton( "Red" );
        redBtn.addActionListener( new BtnHandler() );
        displayPanel.add( redBtn );

        greenBtn = new JButton( "Green" );
        greenBtn.addActionListener( new BtnHandler() );
        displayPanel.add( greenBtn );

        blueBtn = new JButton( "Blue" );
        blueBtn.addActionListener( new BtnHandler() );
        displayPanel.add( blueBtn );

        displayPanel.validate();
        waitForStop();
    }

    /**
     *  The BtnHandler class is responsible for responding to mouse
     *  clicks on the buttons.
     */
    private class BtnHandler implements ActionListener
    {
        public void actionPerformed( ActionEvent evt )
        {
            Object src = evt.getSource();
            if ( src == redBtn )
            {
                displayPanel.setBackground( Color.red );
                Console.println( "Red button clicked" );
            }
            else if ( src == greenBtn )
            {
                setDisplayColor( Color.green );
                Console.println( "Green button clicked" );
            }
            else if ( src == blueBtn )
            {
                setDisplayColor( Color.blue );
                Console.println( "Blue button clicked" );
            }

        }
    }


}
