wood burning stoves 2.0*
The moose likes Swing / AWT / SWT and the fly likes JLabel pan image Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "JLabel pan image" Watch "JLabel pan image" New topic
Author

JLabel pan image

Anand Parthasarathy
Greenhorn

Joined: May 23, 2011
Posts: 13
Hi,

I have a JLabel with an ImageIcon embedded within it. I want to add the pan functionality on the image so that I would be able to pan the image (The image is larger than the size of the label). I was able to implement the pan functionality with AffineTransform. But the functionality does allow me to drag the image even beyond the visible portion of the label. For example, please find attached screen shots of the intial position and dragged position of the image.

I would want to prevent the user from dragging the image the edges of the image start coming into the boundary of the label. Please suggest how it can be done.

Regards,
-Anand



[Thumbnail for initial.JPG]


[Thumbnail for dragged.JPG]

Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4571
    
    5

I would use a JScrollPane. If you want to scroll by dragging you can set the scrollbar policies to _NEVER and use my Drag Scroll Handler
http://forums.oracle.com/forums/thread.jspa?threadID=1348363


luck, db
There are no new questions, but there may be new answers.
Anand Parthasarathy
Greenhorn

Joined: May 23, 2011
Posts: 13
Hi Darryl,

Thanks for your response. I cannot make changes to the UI. Is there any other way of achieving this?

Regards,
-Anand
Anand Parthasarathy
Greenhorn

Joined: May 23, 2011
Posts: 13
Hi, Darryl,

I am not sure if my requirements were clear. I actually wanted to allow the ImageIcon within the JLabel to be dragged within the Label. Having said that, my image is substantially larger than the Label itself. So I need to check if any of the corners of the image (coordinates) comes within the JLabel Rectangle. I can get the Label Rectangle using Component.getVisibleRect(). But I do not have any way of obtaining the coordinates of the image dynamically (as the image is bound to be moving during each drag). If I am able to obtain the coordinates, I can repaint the canvas only if the corners of the image are outside or on the rectangle.

Regards,
-Anand
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4571
    
    5

What's wrong with having a JLabel the size of the ImageIcon (which it is, by default) and dragging the JLabel within the viewport of a JScrollPane, as I suggested?

edit For example:

Edit 13 September 2011: commented out unneeded code that was a legacy of a more advanced version of the DragScrollHandler class. Thanks to Randall Fairman for pointing this out in Jittery Pan/Scrolling.

Edit2: scratch that. The code is needed.
Anand Parthasarathy
Greenhorn

Joined: May 23, 2011
Posts: 13
Thanks a lot Darryl. It's perfect.

Regards,
-Anand
Anand Parthasarathy
Greenhorn

Joined: May 23, 2011
Posts: 13
Darryl,

I have got some scenarios in which it is not working. Whenever I add a setPreferredSize to the label, the drag does not work. Also, we have an applet running on a browser and we have three such images in different labels in the same page. So we need to resize the labels as we want and would want to scroll through the images within the labels. I tried to mimic this situation by creating panels and adding the labels to them. Can you please suggest me what necessary changes I need to make to your code to make it work?

Regards,
-Anand
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4571
    
    5

1. Resize the images, not the labels. See the API of BufferedImage for what method to use for that.
2. Put the labels in a JPanel with a suitable layout and create the drag scroll handler for the panel.

If that doesn't work for you, post a SSCCE <--link that clearly shows the problem. Provide URLs for images accessible on the net.
Anand Parthasarathy
Greenhorn

Joined: May 23, 2011
Posts: 13
Hi Darryl,

Thanks for your response. Unfortunately, our application needs the images in its original size and that was the reason why I needed to pan the image within the smaller labels. I used the Affine Transform as suggested in this link and it worked after some tweaking:

http://www.javalobby.org/java/forums/t19387.html

Thanks anyway for your solutions too. Have a nice day.

Regards,
-Anand
 
 
subject: JLabel pan image