//[skeleton0
import java.awt.event.*;
import java.util.*;
import javax.swing.*;

/**
 * A simple example of a GUI JApplet that responds
 * to events (in this case, events generated by a button)
 *
 * Note: This example contains a subtle mistake.  Specifically, it
 * manipulates GUI elements in the init() method which is
 * NOT called in the event dispatch thread.
 *
 * @author  Prof. David Bernstein, James Madison University
 * @version 1.0
 */
public class      BadInteractiveRandomMessageJApplet 
       extends    JApplet
       implements ActionListener
{
    // Attributes
    private JLabel                label;       

    // The pseudo-random number generator
    private static Random         rng = new Random();    

    // String "constants"
    private static final String   CHANGE = "Change";    

    // The messages
    private static final String[] MESSAGES = 
    {
       "What a great example!",
       "This class is great.",
       "I can't wait to do the programming assignments.",
       "I wish lectures lasted for 5 hours.",
       "I've never had a better Professor."
    };
//]skeleton0

//[constructor
    /**
     * Default Constructor
     */
    public BadInteractiveRandomMessageJApplet()
    {
       super();
    }
//]constructor

//[actionPerformed
    /**
     * Handle actionPerformed messages
     * (required by ActionListener)
     *
     * @param event   The ActionEvent that generated the message
     */
    public void actionPerformed(ActionEvent event)
    {
       String      actionCommand;
       
       actionCommand = event.getActionCommand();
       if (actionCommand.equals(CHANGE))
       {
          label.setText(createRandomMessage());          
       }
    }
//]actionPerformed
    
//[skeleton1
    /**
     * "Create" a message at random
     *
     * @return   The message
     */
    private static String createRandomMessage()
    {
       return  MESSAGES[rng.nextInt(MESSAGES.length)];
    }
//]skeleton1
    
//[init
    /**
     * Called to indicate that this JApplet has been loaded
     */
    public void init()
    {
       JButton                   button;       
       JPanel                    contentPane;
       String                    s;
       
       // Select a message at random
       s = createRandomMessage();

       // Get the container for all content
       contentPane = (JPanel)getContentPane();
       contentPane.setLayout(null);     
  
       // Add a component to the container
       label = new JLabel(s, SwingConstants.CENTER);
       label.setBounds(50,50,500,100);       
       contentPane.add(label);    

       // Add the button to the container
       button = new JButton(CHANGE);
       button.setBounds(450,300,100,50);
       button.addActionListener(this);       
       contentPane.add(button);
    }
//]init
//[skeleton2
}
//]skeleton2
