20-CS-4003-001 Organization of Programming Languages Fall 2016
OOP

Lambda calculus, Type theory, Formal semantics, Program analysis

       Previous     Next         Java Source     All lectures

Develop a special Event class

We look for more generality. The problem: First, consider the possibility of expanding what is sent to mouseClickedInAservicerPanel(). Develop an Event class and send an object containing coordinates plus anything else to mouseClickedInAservicePanel().

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

   // The new event class
   class AnEvent3 {
      int x, y;        // Mouse coordinates
      Aservicer3 src;  // Source of event

      public AnEvent3 (Aservicer3 src) { this.src = src; }
      public void setX (int x) { this.x = x; }
      public void setY (int y) { this.y = y; }
      public int getX () { return x; }
      public int getY () { return y; }
      public Aservicer3 getSource () { return src; }
   }

   // A provider of a click recording service - an object of this class
   // provides a panel and will record the position of mouse clicks in 
   // the panel.  The service provided is to return the coordinates of
   // the last click in the panel.
   class Aservicer3 extends JPanel implements MouseListener {
      U3 u;
      int x,y;  // last known mouse coordinates on which a click occurred

      public Aservicer3 (U3 u) {  
         addMouseListener(this);  
         this.u = u; 
      }
   
      public void mouseClicked (MouseEvent evt) { 
         x = evt.getX(); 
         y = evt.getY(); 
         AnEvent3 event = new AnEvent3(this);           // Added these
         event.setX(x);
         event.setY(y);
         u.mouseClickedInAservicerPanel(event);         // Changed this call
         System.out.println("Aservicer: X="+x+" Y="+y);
      }
      public void mouseEntered (MouseEvent evt) {}
      public void mouseExited (MouseEvent evt) {}
      public void mousePressed (MouseEvent evt) {}
      public void mouseReleased (MouseEvent evt) {}

      public int getX () { return x; }
      public int getY () { return y; }
   }

   // Want to make use of the click service above
   class U3 extends JFrame implements ActionListener {
      JTextField x,y;
      JButton a;

      public U3 () {
         setLayout(new GridLayout(2,1));
         add(new Aservicer3(this));         // Get a servicer - callback here

         JPanel p = new JPanel();
         p.setLayout(new GridLayout(1,2));
         p.add(x = new JTextField());
         p.add(y = new JTextField());

         JPanel q = new JPanel();
         q.setLayout(new GridLayout(2,1));
         q.add(p);
         q.add(a = new JButton("Erase Coordinates"));
         add(q);

         a.addActionListener(this);
      }

      public void actionPerformed (ActionEvent evt) {
         if (evt.getSource() == a) {
            x.setText("    ");
            y.setText("    ");
         }
      }

      //  Changed the argument to an AnEvent object
      public void mouseClickedInAservicerPanel(AnEvent3 evt) {
         x.setText(String.valueOf(evt.getX()));  // Use the AnEvent object
         y.setText(String.valueOf(evt.getY()));
      }
   }

   public class Prog3 extends Applet {
      public void init () {  
         U3 u3 = new U3();
         u3.setSize(180,140);
         u3.setVisible(true);
      }
   }