Getting subimage from Image

Hi,

How can I get the subimage from an Image?

[58 byte] By [student@sunDNa] at [2007-11-27 10:26:21]
# 1

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)

CaptainMorgan08a at 2007-7-28 17:38:58 > top of Java-index,Java Essentials,Java Programming...
# 2

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..

student@sunDNa at 2007-7-28 17:38:58 > top of Java-index,Java Essentials,Java Programming...
# 3

I wonder what BufferedImage is derived from?

BigDaddyLoveHandlesa at 2007-7-28 17:38:58 > top of Java-index,Java Essentials,Java Programming...
# 4

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=?

student@sunDNa at 2007-7-28 17:38:58 > top of Java-index,Java Essentials,Java Programming...
# 5

> //now how can I convert this to an Image object

Why do you need to? BufferedImage is a subclass of Image.

CaptainMorgan08a at 2007-7-28 17:38:58 > top of Java-index,Java Essentials,Java Programming...
# 6

> Image img=?

See reply #3.

BigDaddyLoveHandlesa at 2007-7-28 17:38:58 > top of Java-index,Java Essentials,Java Programming...
# 7

How can I convert BufferedImage to Image(not a file on drive)

student@sunDNa at 2007-7-28 17:38:58 > top of Java-index,Java Essentials,Java Programming...
# 8

> How can I convert BufferedImage to Image(not a

> file on drive)

Image yourImage = yourBufferedImage;

But why would you need to?

CaptainMorgan08a at 2007-7-28 17:38:58 > top of Java-index,Java Essentials,Java Programming...
# 9

Come on, look in the APIs, what is BufferedImage derived from?

BigDaddyLoveHandlesa at 2007-7-28 17:38:58 > top of Java-index,Java Essentials,Java Programming...
# 10

Actually I have an image("test.jpg") I want to use only some part of that image for my Button.

So I am asking.

student@sunDNa at 2007-7-28 17:38:58 > top of Java-index,Java Essentials,Java Programming...
# 11

Prob solv?

BigDaddyLoveHandlesa at 2007-7-28 17:38:58 > top of Java-index,Java Essentials,Java Programming...
# 12

> 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));

CaptainMorgan08a at 2007-7-28 17:38:58 > top of Java-index,Java Essentials,Java Programming...
# 13

Don't forget to burp him -- remember the last time!

BigDaddyLoveHandlesa at 2007-7-28 17:38:58 > top of Java-index,Java Essentials,Java Programming...
# 14

Thanx CaptainMorgan08

I got what I want..

student@sunDNa at 2007-7-28 17:38:58 > top of Java-index,Java Essentials,Java Programming...
# 15

Thanx BigDaddyLoveHandlesyou too.

student@sunDNa at 2007-7-28 17:39:02 > top of Java-index,Java Essentials,Java Programming...
# 16

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!

BigDaddyLoveHandlesa at 2007-7-28 17:39:02 > top of Java-index,Java Essentials,Java Programming...