Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Scrolling BufferedImage

 
Lucas Smith
Ranch Hand
Posts: 808
1
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
Campbell Ritchie
Sheriff
Posts: 48363
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2533
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 808
1
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1561
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1561
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 808
1
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
pete, thanks for your help. I will burn Your clues into my mind!
revalidate() was the key!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic