I have an application where I scale a thumbnail image to fit inside a preset area that displays it. The image gets stretched to fill up that area, so I would like to place it on a black image to act as a background that has the correct size. That way, the component won't stretch the image since the newly build one will fit exactly.
So, how do you superimpose one image (my thumbnail) over top of another one (the black image)?
You can override the 'getPreferredSize' method in the component that draws the scaled–to–fill image. Add this component to a container with GridBagLayout and set its background to black. The container will expand to fill its parent and show the image component centered within the black field. If you put the container inside a JScrollPane the scrollpane will set its scrollbars with the information it gets (Swing takes care of this) when it calls 'getPreferredSize.'