Reputation: 139
I am trying to write some programs where I load images to a panel. Here are my 5 questions:
1> Is there any restriction on what kind(extension) of images can be loaded? I tried to load a .bmp file, it didn't load even after I renamed it with .jpg. However, some other file that were with extensions such as .png or .jpg loaded.
2>Is there a way I can cut an image through java to create a new image. Say, I have a 600x600 pixel image and I want to create a new image by selecting a 200x200 pixel from the middle of the original picture.
3>Is there a way I can resize an image?
4>Can I add an image to a scrollpane?
5> Can I rotate an image by an angle ,say 30 degree?
That's it. A little elaboration with examples will be nice. Thanks in advance.
Upvotes: 0
Views: 186
Reputation: 15408
1> Is there any restriction on what kind(extension) of images can be loaded? I tried to load a .bmp file, it didn't load even after I renamed it with .jpg. However, some other file that were with extensions such as .png or .jpg loaded.
Image I/O has built-in support for GIF, PNG, JPEG, BMP, and WBMP
. Image I/O is also extensible so that developers or administrators can "plug-in" support for additional formats. For example, plug-ins for TIFF and JPEG 2000 are separately available.
Check the Reading/Loading an image tutorial page
2>Is there a way I can cut an image through java to create a new image. Say, I have a 600x600 pixel image and I want to create a new image by selecting a 200x200 pixel from the middle of the original picture.
Two ways. Croping the image using Clipping with Graphics. But faster approach is to use BufferedImage.getSubimage(int x, int y, int w, int h)
method.
BufferedImage image = ImageIO.read("image file");
image = image.getSubimage(50, 50, 200, 200);
This will crop an image at location(x, y) == (50, 50)
and size 200 x 200
.
3>Is there a way I can resize an image?
The discussion about the various approach will take a size of a blog. Read through the The Perils of Image.getScaledInstance()
article for good insight.
However, a quick approach for example: with cWidth
and cHeight
BufferedImage tmpImage = new BufferedImage(cWidth, cHeight, BufferedImage.TYPE_INT_ARGB);
Graphics2D g2d = (Graphics2D)tmpImage.getGraphics();
g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
RenderingHints.VALUE_INTERPOLATION_BILINEAR);
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2d.drawImage(image, 0, 0, cWidth, cHeight, null);
There is working example done by MadProgrammer. It is also better to use external library such as this which does this works nicely.
4>Can I add an image to a scrollpane?
Direct adding is not suggested. use JLabel
instead. Or use a custom component and override the paintComponent(Graphics g)
function and draw inside it. There are some working example with these two approach. Generally working with JLabel
is easier.
See this question answers. Mine including @MadProgrammer. And The custom painting official tutorial page.
5> Can I rotate an image by an angle ,say 30 degree?
Yes using the Graphics2D.rotate(double theta)
function; There are actually much more things you can do with the Graphics2D API. Working examples are shown by MadProgrammer here and here.
Upvotes: 2