Java:
-
I'm creating an image viewer, and have extended JComponent for the viewing area.
This component is placed in a JScrollPane. To make sure the scrollbars appear, I need to tell it that its contents are big...
Image i = Toolkit.getDefaultToolkit().getImage(path);
int height = i.getHeight(null);
int width = i.getWidth(null);
System.out.println(width + " " + height); //some debugging help...
previewArea.setImage(i);
previewArea.setPreferredSize(new Dimension(width,height));My problem is that getWidth and getHeight nearly always return -1 (i.e. size not yet known). What should I give them as the "ImageObserver" in order to make them wait?
-
just call setHorizontalScrollbarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS) on the scrollpane and the same for vertical scrollbar.
-
That makes it show scrollbars, but they don't work because the extended JComponent isn't making itself big enough to stretch the viewport...
-
What are you wanting? Are you wanting the component to be larger or are you wanting the scroll bars to work? If you are wanting the component to be larger I would suggest overriding the getWidth and getHeight methods of the component if they are not getting the correct width and height of the image.
BTW:
I wrote an image manager once that loaded images into memory and this problem didn't occur for me. I think I used ImageIcon and then if I needed the actual image I just called icon.getImage(). You might want to try that, but I don't remember if that's exactly how I did it and I don't have the code with me.
-
I have an extended Jcomponent in a scrollpane.
in the JComponent's paint method, I paint the current image onto the canvas, using AffineTransforms to allow rotation and zooming (rotation isn't supported by ImageIcon as far as I can see.
I need to define the component's size for the scrollpane to think there's something that needs to be scrolled.I don't think it'll matter whether I override the standard JComponent's getWidth function to hardwire it to the Image.getWidth finction, or if i set the component's size normally, because I still don't know the image's size (it returns -1 becuase it doesn't know)
see?
-
I've avoided the getWidth returning -1 problem by moving this section to inside the Paint method...
<font face="Geneva,Arial,sans-serif" size="2">So my problem is now that neither using setPreferredSize(Dimension) nor setMinimumSize(Dimension) will make the JScrollPane realise that its contents are actually quite big... </font>
-
Try calling revalidate() on the image or its container after the image has been loaded or transformed.
-
My extended JComponent now implements scrollable, and works, except that if I stretch the window so it's bigger than the current image, it refuses to paint any future images; the dimensions and other data come up in the right places, but the image area remains grey.
-
In your paint method, call super(g); (g being your Graphics object). Lemme know if that works.
-
edit: bah
-
D:\Java>javac *.java MyPaintingArea.java:13: call to super must be first statement in constructor super(g); ^ 1 error
-
I think he meant super.paint(g)
-
The object you want to pass is previewArea:
int height = i.getHeight(previewArea); int width = i.getWidth(previewArea);
However, you will never be guaranteed that getWidth or getHeight will return a nonnegative value, because the image is being loaded in another thread.
It's possible to use a MediaTracker to wait for the information to become available. However, if you're using Java 1.4 or later, there's a much easier solution:
import javax.imageio.ImageIO; ... BufferedImage i = ImageIO.read(new File(path));
This call will not return until the image is fully loaded. And it returns a BufferedImage, so you can call the getWidth() and getHeight() methods which take no arguments.