aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes Scrolling BufferedImage Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Scrolling BufferedImage" Watch "Scrolling BufferedImage" New topic
Author

Scrolling BufferedImage

Lucas Smith
Ranch Hand

Joined: Apr 20, 2009
Posts: 804
    
    1

Hi,

I have BufferedImage [canvas object]. Its size is 300x300. I can zoom it using mouse wheel and the method:



Unfortunately, the image can enlarge too much. The scroll bars should appear. I do not know how to do it.
I use JPanel for showing the image and I wrap that JPanel with JScrollPane, but it doesn't work.

Thanks for Your help!


SCJP6, SCWCD5, OCE:EJBD6.
BLOG: http://leakfromjavaheap.blogspot.com
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40064
    
  28
How are you adding the scroll pane? And please give us more details; we need more than "it doesn't work".
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2227
    
    8
Scrollbars appear when the "preferred size" of the component is greater than the "size" of the scrollpane.

So I guess if you are scaling your image then you will also need to set the preferred size oif the panel based on the scaling factor.
Lucas Smith
Ranch Hand

Joined: Apr 20, 2009
Posts: 804
    
    1

OK, I will provide some details.

Here is my JPanel:


And here I add this panel to the frame in the constructor:



After resizing the image the scroll bars do not appear. Any ideas?
pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
Most here will be able to help you better if you create a small compilable program that compiles, runs and demonstrates your problem. That way we can experience the problem first hand, modify the code and help you find a solution quickly. This is called creating an SSCCE, and for more on this, please read the link: SSCCE

Key to this process is to create a whole new class for this that extracts all the pertinent constructs from your current classes, but has no extraneous code that is unrelated to your problem.

Til you create and post one, as a SWAG, while you seem to be trying to resize the component holding the image, I wonder if you're calling revalidate on the component holding it (I believe here it would be the JScrollPane's JViewport) so that Swing knows to relayout the newly sized component in its container.
pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
For instance, here is my take on a possible SSCCE for your problem. Run the program first, and the un-comment the line with the //!! comment to see what effect call to revalidate has:

Lucas Smith
Ranch Hand

Joined: Apr 20, 2009
Posts: 804
    
    1

pete, thanks for your help. I will burn Your clues into my mind!
revalidate() was the key!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Scrolling BufferedImage