• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JLabel pan image

 
Anand Parthasarathy
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
initial.JPG
image position when applet starts
[Thumbnail for initial.JPG]
dragged.JPG
image position after dragged
[Thumbnail for dragged.JPG]
 
Darryl Burke
Bartender
Posts: 5125
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Anand Parthasarathy
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5125
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot Darryl. It's perfect.

Regards,
-Anand
 
Anand Parthasarathy
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5125
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic