Last active
November 30, 2018 22:15
-
-
Save rynecheow/5537535 to your computer and use it in GitHub Desktop.
Java snippet that does panning around a JScrollPane by add this as the MouseListener and MouseMotionListener to the viewport of the scroll pane.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//~--- JDK imports ------------------------------------------------------------ | |
import java.awt.Cursor; | |
import java.awt.Point; | |
import java.awt.Rectangle; | |
import java.awt.event.MouseAdapter; | |
import java.awt.event.MouseEvent; | |
import javax.swing.JComponent; | |
import javax.swing.JViewport; | |
/** | |
* | |
* Handles event while user try to drag on the canvas, which results in a | |
* image panning effect (hand scrolling). | |
* <p> | |
* Event handling starts while user presses on the canvas. | |
* | |
* @author: Cheow Yeong Chi | |
* | |
* @version 1.4 | |
* | |
*/ | |
public class RCPanMouseAdapter extends MouseAdapter { | |
/** | |
* Panning pivot point | |
*/ | |
private final Point panPoint = new Point(); | |
/** | |
* {@inheritDoc}<p> | |
* Set pan starting point to be the point where the mouse currently located, and change the | |
* current cursor to <code>HAND_CURSOR</code>. | |
*/ | |
@Override | |
public void mousePressed(MouseEvent event) { | |
JViewport viewport = (JViewport) event.getSource(); // viewport from the JScrollPane | |
JComponent component = (JComponent) viewport.getView(); | |
panPoint.setLocation(event.getPoint()); | |
component.setCursor(new Cursor(Cursor.HAND_CURSOR)); | |
} | |
/** | |
* {@inheritDoc}<p> | |
* Reset cursor to <code>DEFAULT_CURSOR</code>. | |
*/ | |
@Override | |
public void mouseReleased(MouseEvent event) { | |
JViewport viewport = (JViewport) event.getSource(); | |
JComponent component = (JComponent) viewport.getView(); | |
component.setCursor(new Cursor(Cursor.DEFAULT_CURSOR)); | |
} | |
/** | |
* {@inheritDoc}<p> | |
* Get the current viewport of the canvas and move the viewable area respective to the | |
* event cursor location. | |
*/ | |
@Override | |
public void mouseDragged(MouseEvent event) { | |
JViewport viewport = (JViewport) event.getSource(); | |
JComponent component = (JComponent) viewport.getView(); | |
Point currentPoint = event.getPoint(); | |
Point viewPoint = viewport.getViewPosition(); | |
viewPoint.translate(panPoint.x - currentPoint.x, panPoint.y - currentPoint.y); | |
component.scrollRectToVisible(new Rectangle(viewPoint, viewport.getSize())); | |
panPoint.setLocation(currentPoint); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I took the general idea from what you had, and put this component together that seems to work. Presently its only set up to pan on a right click drag, but this could be easily changed
And here is an example of how to use it