Skip to main content

Re: [JAVA2D] How to get the height of the painted zone of a BufferedImage without....

1 reply [Last post]
Anonymous

hey --

It depends on what type of BufferedImage it is. If it supports transparency, you can compute the tighest box around all the non-transparent pixels. Else, you'd have to do the same thing around all the non-background-color pixels. Of course this may not give you what you want, in which case you might just want to store the information you need ...

Brien

Brien Colwell wrote: hey --

It depends on what type of BufferedImage it is. If it supports transparency, you can compute the tighest box around all the non-transparent pixels. Else, you'd have to do the same thing around all the non-background-color pixels. Of course this may not give you what you want, in which case you might just want to store the information you need ...

Brien

java2d@JAVADESKTOP.ORG wrote: How to get the height of the painted zone of a BufferedImage without keeping track of the height of the single items drawn in the painting procedure that paints on the image.

Suppose you create a 400 x 400 buffered Image.
You paint on it for 2/3 of the height but you don't keep any track of this.
How do you know after drawing what is the really drawn height of the Image ?

Estades.
[Message sent by forum member 'estades' (estades)]

http://forums.java.net/jive/thread.jspa?messageID=216329

===========================================================================
To unsubscribe, send email to listserv@java.sun.com and include in the body
of the message "signoff JAVA2D-INTEREST". For general help, send email to
listserv@java.sun.com and include in the body of the message "help".

---------------------------------
We won't tell. Get more on shows you hate to love
(and love to hate): Yahoo! TV's Guilty Pleasures list.

---------------------------------
Ahhh...imagining that irresistible "new car" smell?
Check outnew cars at Yahoo! Autos.

===========================================================================
To unsubscribe, send email to listserv@java.sun.com and include in the body
of the message "signoff JAVA2D-INTEREST". For general help, send email to
listserv@java.sun.com and include in the body of the message "help".
[att1.html]

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
estades
Offline
Joined: 2007-05-03

By supporting transparency you catch an interesting problem.

Suppose you draw an image like a PNG file that has an alpha channel incorporated.
You have trasparent pixels and half transparent pixels.

I suppose computing the tightest box would consider half trasparent pixels as opaque and put them inside the box instead perfectly transparent pixels would be outside the box, right.

Anyway what method would make this calculation, can you give me any insight ?

And just another question what if you don't want to get rid of those completely transparent pixels, suppose you use them for centering pursposes.

Anyway I reply to the to question about which BufferedImage.

Should it be a BufferedImage.TYPE_INT_RGB or TYPE_INT_ARGB ?

I suppose the latest.

But I had strange behaviours with the last type...

Example I was working with photoshop layers and Blending Modes

I used an implementation of Composite for Screen Blending mode from a previous version of SwingX.

I got an Image from an ImageIcon from a JLabel.
And using composites I had uncorrect behaviour from using it directly.
I lost all the trasparency from the original PNG file used in the JLabel, when
applying the composite.

The same has happened when I tried to create a bufferedImage TYPE_INT_ARGB
and draw the image over it and then use this for the composite.

Instead redrawing the original image on a bufferedImage TYPE_INT_RGB and then using this for the composite works perfectly.

Here's the sample code:

/// CODE SNIPPET START

Graphics2D g2D = (Graphics2D) g;

Composite myComposite = BlendComposite.Screen;
Composite originalComposite = g2D.getComposite();

// Using this Image directly would give loss of all transparency and uncorrect result on Composite.
Image glowImage = group.getArrowGlowImageIcon().getImage();

// Procedure to make Images with alpha work with Blending Composites

// Declaring the image type as BufferedImage.TYPE_INT_ARGB would give same results as not using any buffered Image at all

BufferedImage bufferedGlowImage = new BufferedImage(glowImage.getWidth(null),glowImage.getHeight(null),BufferedImage.TYPE_INT_RGB);

Graphics2D imgG2D = bufferedGlowImage.createGraphics();
imgG2D.drawImage(glowImage, 0, 0, null);
imgG2D.dispose();

g2D.setComposite(myComposite);

g2D.drawImage(bufferedGlowImage, x, y, null);

// Restore of the old Composite to paint other stuff correctly

g2D.setComposite(originalComposite);

/// CODE SNIPPET END

As a proof of that problem I tracked the object and the image returned from the imageIcon contains a BufferedImage imageType == 2 in his imagerep.