schecae1
September 24th, 1999, 08:44 AM
I'm trying to implement drag scrolling and am not getting what I expect. I save the initial mousePressed point then in my drag handler I update the scroll bars by the amount of the cursor shift. It appears that my shift triggers new drag events which repeatedly undershoot then overshoot the desired shift. If I drag then stop and hold the cursor still the scrollbar thumbs just oscillate back and forth until I release the mouse.
Here's the significant code:
public void mousePressed( MouseEvent me )
{
downX = me.getX();
downY = me.getY();
}
public void mouseDragged( MouseEvent me )
{
int x = me.getX();
int y = me.getY();
int dx = x - downX;
int dy = y - downY;
if ( dx == 0 && dy == 0 ) return;
int curx = sp.getHAdjustable().getValue();
int cury = sp.getVAdjustable().getValue();
int newx = curx - dx;
if ( newx < 0 ) newx = 0;
if ( newx > gtFile.getImageWidth() ) newx = gtFile.getImageWidth();
int newy = cury - dy;
if ( newy < 0 ) newy = 0;
if ( newy > gtFile.getImageHeight() ) newy = gtFile.getImageHeight();
sp.getHAdjustable().setValue( newx );
sp.getVAdjustable().setValue( newy );
downX = x;
downY = y;
}
...
imgCanvas.addMouseListener( this );
imgCanvas.addMouseMotionListener( this );
...
I'm scrolling a Canvas (imgCanvas) in my ScrollPane (sp). My Canvas is generating the events and my application is the listener and doing the updating.
How do I fix this so that I don't get this "feedback" loop or if it's not fixable, how do I do it right?
Andrew Scheck
Laurel, MD
Andrew.Scheck@jhuapl.edu
Here's the significant code:
public void mousePressed( MouseEvent me )
{
downX = me.getX();
downY = me.getY();
}
public void mouseDragged( MouseEvent me )
{
int x = me.getX();
int y = me.getY();
int dx = x - downX;
int dy = y - downY;
if ( dx == 0 && dy == 0 ) return;
int curx = sp.getHAdjustable().getValue();
int cury = sp.getVAdjustable().getValue();
int newx = curx - dx;
if ( newx < 0 ) newx = 0;
if ( newx > gtFile.getImageWidth() ) newx = gtFile.getImageWidth();
int newy = cury - dy;
if ( newy < 0 ) newy = 0;
if ( newy > gtFile.getImageHeight() ) newy = gtFile.getImageHeight();
sp.getHAdjustable().setValue( newx );
sp.getVAdjustable().setValue( newy );
downX = x;
downY = y;
}
...
imgCanvas.addMouseListener( this );
imgCanvas.addMouseMotionListener( this );
...
I'm scrolling a Canvas (imgCanvas) in my ScrollPane (sp). My Canvas is generating the events and my application is the listener and doing the updating.
How do I fix this so that I don't get this "feedback" loop or if it's not fixable, how do I do it right?
Andrew Scheck
Laurel, MD
Andrew.Scheck@jhuapl.edu