A suggestion I can make is to first resize the image onto a separate BufferedImage
. The reason being, a Graphics2D
object of the BufferedImage
can be obtained in order to produce a better quality scaled image.
Graphics2D
can accept "rendering hints" which instruct the way image processing should be performed by the Graphics2D
object. The setRenderingHint
method is one of the methods which can be used to set those rendering hints. The rendering hints from the RenderingHints
class can be used.
Then, using that Graphics2D
object, an image can be drawn to the BufferedImage
using the rendering hints specified earlier.
A rough (untested) code would work as the following:
BufferedImage scaledImage = new BufferedImage(
scaledWidth,
scaledHeight,
BufferedImage.TYPE_INT_RGB
);
Graphics2D g = scaledImage.createGraphics();
g.setRenderingHints(
RenderingHints.Key.KEY_INTERPOLATION,
RenderingHints.VALUE_INTERPOLATION_BICUBIC
);
g.drawImage(panelImage, 0, 0, scaledWidth, scaledHeight, null);
g.dispose();
Other rendering hints of interest may include:
The Controlling Rendering Quality section of The Java Tutorials also has more information on how to control the rendering quality of Graphics2D
objects.
And for a very good source of information on dealing with graphical interfaces in general, Filthy Rich Clients by Chet Haase and Romain Guy is highly recommended. There is one section of the book that deals with the issue of scaling images, which seems quite relevant.