Mouse Dragging followup example. Many new features added to original in-class group exercise! Study the techniques.



/* This program is a followup to the January 30th, 2003 mouse dragging 
   direction indicator program.

   It illustrates the following new concepts for the first time:
            && means logical AND  -  thus
                                          temperature < 60 && temperature > 32
            means its fairly cool, under 60 degrees, but not freezing,
                     as it is also above 32 degrees, above freezing.

   The double vertical bar symbols, or pipe symbols, mean logical OR.

            playerNumber == 5 || playerNumber == 8 

            expresses the idea that the playerNumber is either 5 OR 8
            by using 
                     the || operator for the logical OR operation.      */

import objectdraw.*;
import java.awt.*;

public class Dragging2 extends WindowController
{
   private Location previousPosition;

   private Text direction;

   private double distanceDragged,
                  previousChange;  // will be negative if it was a westward or left drag
                                   // and positive if it was an eastward or right drag.

   private double westTotal, eastTotal;

   private Text westTotalDisplay, 
                eastTotalDisplay;

   public void begin()
   {
      double w, h;

      w = canvas.getWidth();
      h = canvas.getHeight();

      FilledRect c = new FilledRect(0, 0, w, h, canvas);
      c.setColor( Color.yellow );

      direction = new Text("Direction of dragging", 50, 50, canvas);	
      direction.setBold();

      createGrandTotals();
   } 

   public void onMousePress(Location pt)
   {
      previousPosition = pt;
      previousChange = 0;
      distanceDragged = 0;
   }
	
   public void onMouseDrag(Location pt)
   {
      double xChange = previousPosition.getX() - pt.getX();

      if (xChange < 0 && previousChange > 0 || xChange > 0 && previousChange < 0)
      {
         distanceDragged = xChange;
      }
      else
      {
         distanceDragged = distanceDragged + xChange;
      }
      
      if (previousPosition.getX() < pt.getX())
      {
         direction.setText("Eastward dragged: " + distanceDragged);

         eastTotal = eastTotal + xChange;
         eastTotalDisplay.setText(eastTotal);
      }
      else
      {
         direction.setText("Westward dragged: " + distanceDragged);

         westTotal = westTotal + xChange;
         westTotalDisplay.setText(westTotal);
      }
		 
      previousChange = xChange;
      previousPosition = pt;	
   }

   private void createGrandTotals()   // helper function - its PRIVATE 
   {
      westTotal = 0;
      eastTotal = 0;

      new Text("WEST = ", 10, 300, canvas).setColor( Color.blue );
      new Text("EAST = ", 10, 330, canvas).setColor( Color.red );

      westTotalDisplay = new Text("WEST", 60, 300, canvas);
      eastTotalDisplay = new Text("EAST", 55, 330, canvas);
 
      westTotalDisplay.setBold();
      westTotalDisplay.setColor( Color.blue );
      
      eastTotalDisplay.setBold();
      eastTotalDisplay.setColor( Color.red );
   }
}