I would like to make a join.me like in Java.
I've made the screen capture part but now I want to scroll in the image by dragging the mouse.
Here is a screen of what i've made:
First of all, I want to replace the scroll bars by mouse dragging the image. Is it possible? Then I want to remove those scroll bars.
Today, users are able to change the zoom and use their mouse wheel to zoom in/out.
Could you help me?
Thanks.
Edit: I've found the way to hide the scroll bar using:
this.jScrollPane1.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
this.jScrollPane1.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_NEVER);
Finally, I did it myself. Here is the solution if someone need it:
Create a new class named HandScrollListener
with the following code:
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.JLabel;
import javax.swing.JViewport;
public class HandScrollListener extends MouseAdapter
{
private final Cursor defCursor = Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR);
private final Cursor hndCursor = Cursor.getPredefinedCursor(Cursor.HAND_CURSOR);
private final Point pp = new Point();
private JLabel image;
public HandScrollListener(JLabel image)
{
this.image = image;
}
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();
}
}
Then in your frame put:
HandScrollListener scrollListener = new HandScrollListener(label_to_move);
jScrollPane.getViewport().addMouseMotionListener(scrollListener);
jScrollPane.getViewport().addMouseListener(scrollListener);
It should work!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With