I have two listeners, One is MouseWheelListener which zooms into an image if wheel is scrolled.
jLabel1.addMouseWheelListener(new MouseWheelListener() {
public void mouseWheelMoved(MouseWheelEvent e) {
int notches = e.getWheelRotation();
double temp = zoom - (notches * 0.2);
// minimum zoom factor is 1.0
temp = Math.max(temp, 1.0);
if (temp != zoom) {
zoom = temp;
Map.resizeImage(jLabel1,zoom);
}
}
});
When I zoom in, I need to be able to drag around the image with a drag listener, I have created my Listener and registered it to the jScrollPanel like this:
HandScrollListener scrollListener = new HandScrollListener(jLabel1);
jScrollPane1.getViewport().addMouseMotionListener(scrollListener);
jScrollPane1.getViewport().addMouseListener(scrollListener);
The problem is, if I add the first listener which zooms in on my image, the second listener gets generated but does not performs its function of dragging. If I remove it and zoom in on the image with a button, then the second listener gets generated as well as performs it's drag function.
Here is the HandScrollListener, it works when the listener for zooming in is not added.
public class HandScrollListener extends MouseAdapter
{
Cursor defCursor = Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR);
Cursor hndCursor = Cursor.getPredefinedCursor(Cursor.HAND_CURSOR);
Point pp = new Point();
public void mouseDragged(final MouseEvent e)
{
JViewport vport = (JViewport)e.getSource();
Point cp = e.getPoint();
Point vp = vport.getViewPosition();
vp.translate(pp.x-cp.x, pp.y-cp.y);
image.scrollRectToVisible(new Rectangle(vp, vport.getSize()));
pp.setLocation(cp);
}
public void mousePressed(MouseEvent e)
{
image.setCursor(hndCursor);
pp.setLocation(e.getPoint());
}
public void mouseReleased(MouseEvent e)
{
image.setCursor(defCursor);
image.repaint();
}
}