Getting subimage from Image
Hi,
How can I get the subimage from an Image?
Hi,
How can I get the subimage from an Image?
Here's an easy way to do it with BufferedImages:
http://java.sun.com/j2se/1.5.0/docs/api/java/awt/image/BufferedImage.html#getSubimage(int,%20int,%20int,%20int)
Can we convert BufferedImage to an Image?
I want to take some part of image for a Button.
So I cant use BufferedImage over there..
look what I have is
[code]
BufferedImage bufImg=ImageIO.read(getClass().getResource("myimage.jpg"));
//This gives me BufferedImage object
//now how can I convert this to an Image object
Image img=?
> //now how can I convert this to an Image object
Why do you need to? BufferedImage is a subclass of Image.
> How can I convert BufferedImage to Image(not a
> file on drive)
Image yourImage = yourBufferedImage;
But why would you need to?
Actually I have an image("test.jpg") I want to use only some part of that image for my Button.
So I am asking.
> Actually I have an image("test.jpg") I want to use
> only some part of that image for my Button.
> So I am asking.
Here is your spoon.
BufferedImage bufImg = ImageIO.read(getClass().getResource("myimage.jpg"));
BufferedImage section = bufImg.getSubimage(5, 5, 20, 20); //adjust to your liking
JButton myButton = new JButton(new ImageIcon(section));
Suggestion: mookmark the APIs:
http://java.sun.com/javase/6/docs/api/
For example, one of the first things you'd notice about BufferedImage:
http://java.sun.com/javase/6/docs/api/java/awt/image/BufferedImage.html
... is that it extends java.awt.Image -- prob solv!