I think the question is quite self-explainitory, I want to implement a simple zoom function using a JSlider like in Windows Live Photo Gallery for instance.
I've had a quick look online but all the code I've tried to use appears to have errors in when I copy it into eclipse. I don't really want to use a third party library either as the application may be sold under a company name. Plus, I'm begginning to realise that there may be some safety precations required in order to prevent errors, but I do not know what these will be.
So, if anybody can provide me with some Java code to zoom in and out of images it would be grately appreciated.
Thanks in advance
PS I plan to use the Image as an ImageIcon inside of a JLabel which will be added to a JScrollPane
解决方案
You can easily achieve this by using scale transforms on the original image.
Assuming your the current image width newImageWidth, and the current image height newImageHeight, and the current zoom level zoomLevel, you can do the following:
int newImageWidth = imageWidth * zoomLevel;
int newImageHeight = imageHeight * zoomLevel;
BufferedImage resizedImage = new BufferedImage(newImageWidth , newImageHeight, imageType);
Graphics2D g = resizedImage.createGraphics();
g.drawImage(originalImage, 0, 0, newImageWidth , newImageHeight , null);
g.dispose();
Now, replace the original image, originalImage, in your display area by resizedImage.