/* 
 *  ===================================================================
 *  DemoGUI.java : Simple GUI with canvas connected to a mouse listener
 *  and buttons connected to button listeners.
 * 
 *  Written By : Mark Austin                                  May, 2005
 *  ===================================================================
 */ 

import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;

public class DemoGUI extends JApplet {
    Container container = null;
    DemoGraphicsScreen gs;

    public void init() {

      // 1. Get a handle on the applet's content pane.
   
      container = this.getContentPane();

      // 2. Create a graphics screen and mouse listener,
      //    and add content pane to center of applet.

      DemoGraphicsScreen gs = new DemoGraphicsScreen();
      gs.addMouseListener( new GraphicsListener( gs ));

      // 3. Create buttons and button listeners.

      JButton buttons[] = new JButton [3]; 

      buttons[0] = new JButton ("Clear");
      buttons[0].addActionListener( new ButtonAction( buttons[0], gs ));
      buttons[1] = new JButton ("Squares");
      buttons[1].addActionListener( new ButtonAction( buttons[1], gs ));
      buttons[2] = new JButton ("Circles");
      buttons[2].addActionListener( new ButtonAction( buttons[2], gs ));

      // 4. Create panel. Add buttons to panel.

      Panel p1 = new Panel();
      for(int ii = 1; ii <= 3; ii++ )
          p1.add( buttons[ii-1] );

      // 5. Position canvas and panel within the frame.

      container.add( "South", p1 );
      container.add("Center", gs );
   }
}

/*
 *  =================================================================
 *  This class listens for action events associated with the buttons.
 *  =================================================================
 */

class ButtonAction implements ActionListener {
      private JButton b;
      private DemoGraphicsScreen gs;

      public ButtonAction ( JButton b, DemoGraphicsScreen gs ) {
         this.b  = b;
         this.gs = gs;
      }

      public void actionPerformed ( ActionEvent e ) {

         String s = new String(e.getActionCommand());
         System.out.println(s);

         if( s.compareTo("Clear") == 0 ) {
             gs.clear();
         }
         if( s.compareTo("Circles") == 0 ||
             s.compareTo("Squares") == 0 ) {
             gs.gsDraw();
         }
      }
}

/*
 *  ==============================================================
 *  This class listens for mouse events associated with the canvas
 *  ==============================================================
 */

class GraphicsListener implements MouseListener {
      private DemoGraphicsScreen gs;

      public GraphicsListener( DemoGraphicsScreen gs ) {
         this.gs = gs;
      }

      public void mouseReleased ( MouseEvent e ) {

          // Get and save new coordinates...

          gs.iXcoord = e.getX();
          gs.iYcoord = e.getY();

          // Clear screen and redraw text...

          gs.clear();
          gs.drawText();
      }

      public void mouseClicked ( MouseEvent e ) {};
      public void mouseEntered ( MouseEvent e ) {};
      public void mouseExited ( MouseEvent e ) {};
      public void mousePressed ( MouseEvent e ) {};
}

/*
 *  =======================
 *  Create graphics screen.
 *  =======================
 */

class DemoGraphicsScreen extends Canvas {
    int iXcoord, iYcoord;

    private Dimension size;
    private Graphics    gs;

    //  Use the paint() method to display the (x,y) coordinates of the
    //  mouse when it is clicked...

    public void paint(Graphics g) {
       size = getSize();
       System.out.println("In DemoGraphicsScreen : paint()");
       drawText();
    }

    // Draw cursor coordinates on the canvas ....

    public void drawText() {
       gs = getGraphics();
       Graphics2D g2D = (Graphics2D) gs;

       g2D.setColor(Color.blue);
       g2D.drawString("Click the mouse ...", 5, 20);
       g2D.drawString("The cursor coordinates: " + iXcoord + ", " + iYcoord , 5, 95);

       System.out.println("In DemoGraphicsScreen : drawText()");
    }
    
    // Get graphics and fill in background ....

    public void clear () {
        gs = getGraphics();
        Graphics2D g2D = (Graphics2D) gs;

        g2D.setColor( Color.white );
        g2D.fillRect( 0, 0, size.width-1, size.height-1 );
    }

    // Method to draw boxes and circles ...

    public void gsDraw () {

       // Details for drawing boxes and circles removed ...
                      
    }
}
