/*===============================================================*/ /* Dragster.java */ /*===============================================================*/ /** *
* Dragster.java: An event handling class for use with light- * weight components. * * This class actually moves the components about. * * Revisions: 1.1 Fixed background problems, minimum sizing * problems with lightweights * * 1.0 July 15, 1998, * Created class Dragster.java * ** * @author David Dagon * @version Version 1.0, August 17, 1998. */ /*===============================================================*/ import java.awt.*; import java.awt.event.*; class Dragster extends MouseAdapter implements MouseMotionListener { /*===============================================================*/ /* INSTANCE VARIABLES */ /*===============================================================*/ /* */ /* scope datatype variable name */ /*---------------------------------------------------------------*/ private LightComponent comp; private Point press; private boolean dragging = false; /*===============================================================*/ /* CONSTRUCTORS */ /*===============================================================*/ /** *
* * Constructor -- This constructor takes in a reference to the * componet that created it, for purposes of cross-class * communication. * * Precondition: An a lightweight component has been created, * and requires an event handler * * Postcondition: A class to handle events for the lightweight * is instantiated. * ** */ public Dragster(LightComponent comp) { this.comp = comp; } /*===============================================================*/ /* EVENT METHODS */ /*===============================================================*/ /** *
*
* mousePressed -- a mouse has been depressed
*
* Precondition -- a mouse event has been delivered to this
* component
*
* Postcondition -- if this component is draggable, the
* 'dragging' boolean is set to true.
* Also, the point at which the mouse
* is depressed is saved, for later
* updating from the buffers.
*
* @param -- MouseEvent -- the mouse event delivered to this
* method
*/
public void mousePressed (MouseEvent e)
{
/* save these locations for later dragging */
press = new Point (e.getX(), e.getY());
/* set dragging boolean */
if (comp.isDraggable()) dragging = true;
else dragging = false;
}
/*===============================================================*/
/**
*
*
* mouseReleased -- a mouse release event occurs
*
* Precondition -- a mouse event has occured: the release
*
* Postcondition -- the dragging boolean is set to false. Also,
* specific to the shuffle-board game, the
* lightweight component is moved from coordinates
* at StartH, StartW, to EndW, EndH. The movement
* from start to end coordinates occurs in a loop
* with constant update, producing a smooth
* animated movement--a 'sliding' square.
*/
public void mouseReleased (MouseEvent e)
{
/* set dragging to false, if it's not already */
dragging = false;
/* check if move was valid; this boolean check is
specific to the suffle board game. */
if (comp.validMove())
{
/* get a handle on the container */
LightContainer c;
c = (LightContainer) comp.getParent();
Point loc = comp.getLocation();
Point pt = new Point();
/* these calculations are specific to the shuffle-
board game, but can be generalized to
control the x, y location of any object */
int StartW = comp.getLocW() * MyPanel.w;
int StartH = comp.getLocH() * MyPanel.h;
int EndW = comp.getBlankLocW() * MyPanel.w;
int EndH = comp.getBlankLocH() * MyPanel.h;
int deltaW = (StartW>EndW)? -1: (StartW==EndW)?0:1;
int deltaH = (StartH>EndH)? -1: (StartH==EndH)?0:1;
while (StartH!=EndH || StartW != EndW)
{
StartW+=deltaW;
StartH+=deltaH;
/* set the new x, y coords in the Point */
pt.x = StartH;
pt.y = StartW;
/* move the component--it's that easy */
c.moveComponent(comp, pt);
}
/* these steps are specific to shuffleboard--updating
the new black square's location, etc. */
comp.setBlankLocH(comp.getLocH());
comp.setBlankLocW(comp.getLocW());
comp.setLocH(EndH/MyPanel.h);
comp.setLocW(EndW/MyPanel.w);
/* a call is made, but this method is not written */
comp.getPanel().checkCompletion();
} // if valid move
else
{
if (!comp.isDraggable())
{
/* here, do something if the component
was not draggable. Or not. */
} // if not draggable
} // if not valid move
} // mouse released
/*===============================================================*/
/**
*
*
* mouseEntered -- a mouse release event occurs
*
* Precondition -- a mouse event has occured: mouse entering
*
* Postcondition -- No change;
*/
public void mouseEntered (MouseEvent e)
{
e.consume();
}
/*===============================================================*/
/**
*
*
* mouseEntered -- a mouse entered event occurs
*
* Precondition -- a mouse event has occured: mouse entering
*
* Postcondition -- No change;
*/
public void mouseClicked (MouseEvent e)
{
e.consume();
}
/*===============================================================*/
/**
*
*
* mouseEntered -- a mouse move event occurs
*
* Precondition -- a mouse event has occured: mouse movement
*
* Postcondition -- No change;
*/
public void mouseMoved (MouseEvent e)
{
e.consume();
}
/*===============================================================*/
/**
*
*
* mouseEntered -- a mouse drag event occurs
*
* Precondition -- a mouse event has occured: mouse dragging
*
* Postcondition -- The x, y location of the point is updated
*/
public void mouseDragged (MouseEvent e)
{
/* check if dragging a component or not */
if (dragging)
{
LightContainer c;
Point loc = comp.getLocation();
Point pt = new Point();
pt.x = e.getX() + loc.x - press.x;
pt.y = e.getY() + loc.y - press.y;
c = (LightContainer) comp.getParent();
c.moveComponent (comp, pt);
}
} // mouseDragged
}//class