• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to scroll an image ?  RSS feed

 
Smilidon Sapiens
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,
I have an applet(awt) with some elements (e.g. TextField, CheckBox,...) and want also display an image. This image has a different size, because I change it sometimes. I want to show it in a seperate area. I created an own Panel (overwrite the paint-methode) and added it to the ScrollPane. I added this ScrollPane to my applet. I see the picture in the pane, but I can't scroll it.
The scrollbars are only grey! How can I tell the pane that there is an image to scroll ???
Thanks for your attention.
Bye

[This message has been edited by Brain Tenner (edited April 23, 2001).]
 
Nathan Pruett
Bartender
Posts: 4121
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You may be able to add the image to a Panel or a Canvas and mess with their preferred size by overriding getPreferredSize() on the Panel or Canvas...
You also may be able to fix this by loading the image with a MediaTracker... sometimes I have noticed image sizes not reporting correctly unless they were loaded by MediaTracker... test your code before and after adding MediaTracker by printing out image.getWidth( this ); and image.getHeight( this )... chances are they are both currently returning 0... MediaTracker completely loads images before they are displayed, and it prevents this...

If you need help with MediaTracker...
[code]
Image i = new Image( getCodeBase(), "pic.gif" );
MediaTracker tracker = new MediaTracker( this );
tracker.addImage( i, 0 );
try
{
tracker.waitForAll();
}
// The image should be completely loaded now!
[code]
 
Smilidon Sapiens
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your answer.
Do you have some code for adding the image to the panel and how to overwrite the method getPreferdSize() ?
Thanks a lot!!!
 
Nathan Pruett
Bartender
Posts: 4121
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would just make a subclass of Panel, store the image in it, have it paint the image, and have it set it's size to the image's size... in the following example, MediaTracker is used in the constructor to ensure that images that are assigned to the ImagePanel are completely loaded... if you find that this impacts the speed at which your program runs, you should remove the MediaTracker code from the constructor and load all your images externally with MediaTracker before assigning them to the ImagePanel...



Then, in your code, you just make an ImagePanel to display your Image...


HTH,
-Nate
 
Smilidon Sapiens
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your example. This willl help me very much.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!