

import java.awt.*;
import java.awt.event.*;     //  needed for events and listeners
import java.applet.Applet;

// THIS IS A VERY SIMPLE APPLET THAT HAS ONE BUTTON.
// IT DETECTS WHEN THE BUTTON IS PRESSED

public class SampleEventGUI extends Applet {

   private Dimension size;

   // declare the components of the gui
   private Button press_me;
   private MyCanvas pic;

   private MyListener button_listener;
   
   // Constructor is for instantiating the components
   //
   public SampleEventGUI () {
      size = new Dimension(600,400);

      // instantiate the components.
      press_me = new Button("Don't press this button.");
      pic = new MyCanvas();

      button_listener = new MyListener(this);
   }

   // init() is for adding the components to the applet
   //
   public void init() {

      // add the components to the applet

      add(press_me);
      add(pic);

      // set up the listener
      press_me.addActionListener (button_listener);
      
      resize (size);
   }

   // Change the label on the button to a new string
   //
   public void updateButton() {
      press_me.setLabel ("I told you not to press it!");
   }
   
}

// This is a class whose task is to listen for button presses, which
// are called actions.
//   It implements an interface that is defined in the Java API
// 
class MyListener implements ActionListener {

   SampleEventGUI gui;    // this is the applet
   
   public MyListener (SampleEventGUI gui) {
      this.gui = gui;
   }

   public void actionPerformed (ActionEvent e) {
      gui.updateButton();
   }
}


// This is a simple canvas.  A rectangle with an 'X' through it. 
class MyCanvas extends Canvas {

   Dimension size;
      
   public MyCanvas() {
      size = new Dimension(100,100);
      resize(size);
      
   }

   // draw the image
   //
   public void paint(Graphics page) {
      page.drawRect(0,0,size.width-1,size.height-1);
      page.drawLine(0,0,size.width-1,size.height-1);
      page.drawLine(0,size.height-1,size.width-1,0);
   }

   // the next two methods are required for the canvas to work
   public Dimension getMinimumSize() {
      return size;
   }
   public Dimension getPreferredSize() {
      return getMinimumSize();
   }
}


