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

Lambda calculus, Type theory, Formal semantics, Program analysis

       Previous     Next         Java Source     All lectures


The problem: now the problem is to have the Aservicer class report the mouse clicks as they happen. This will require a reference to the U class object from the Aservicer class.

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

   // 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 Aservicer2 extends JPanel implements MouseListener {
      U2 u;
      int x,y;  // last known mouse coordinates on which a click occurred

      // Changed - a U2 object is sent in to enable calling its 
      // mouseClickedInAservicerPanel method
      public Aservicer2 (U2 u) {  
         this.u = u;
      public void mouseClicked (MouseEvent evt) { 
         x = evt.getX(); 
         y = evt.getY(); 
         u.mouseClickedInAservicerPanel(x,y);              // Added 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 U2 extends JFrame implements ActionListener {
      JTextField x,y;
      JButton a;

      public U2 () {
         setLayout(new GridLayout(2,1));
         add(new Aservicer2(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(a = new JButton("Erase Coordinates"));


      public void actionPerformed (ActionEvent evt) {
         if (evt.getSource() == a) {
            x.setText("         ");  // Changed to blank textfields
            y.setText("         ");

      //  Added this method
      public void mouseClickedInAservicerPanel(int xval, int yval) {
         x.setText(String.valueOf(xval));  // Use the Aservicer methods

   public class Prog2 extends Applet {
      public void init () {  
         U2 u2 = new U2();