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

Add the interface

Finally, we require implementing an interface to guarantee that mouseClickedInAservicePanel will be available for the service provider to use.

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

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

      public AnEvent4 (Aservicer4 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 Aservicer4 getSource () { return src; }
   }

   // The new interface
   interface AserviceListener { void mouseClickedInAservicerPanel (AnEvent4 evt); }

   // 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 Aservicer4 extends JPanel implements MouseListener {
      AserviceListener u;   // All the Us become AserviceListeners
      int x,y;  // last known mouse coordinates on which a click occurred

      public Aservicer4 (AserviceListener u) {  
         addMouseListener(this);  
         this.u = u; 
      }
   
      public void mouseClicked (MouseEvent evt) { 
         x = evt.getX(); 
         y = evt.getY(); 
         AnEvent4 event = new AnEvent4(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; }
   }

   // Class U4 objects are also AserviceListener objects - hence they
   // may be used are arguments to Aservicer4 constructors
   class U4 extends JFrame implements ActionListener, AserviceListener {
      JTextField x,y;
      JButton a;

      public U4 () {
         setLayout(new GridLayout(2,1));
         add(new Aservicer4(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(AnEvent4 evt) {
         x.setText(String.valueOf(evt.getX()));  // Use the AnEvent object
         y.setText(String.valueOf(evt.getY()));
      }
   }

   public class Prog4 extends Applet {
      public void init () {  
         U4 u4 = new U4();
	 u4.setSize(180,140); 
         u4.setVisible(true);
      }
   }