Hi Scott,

On Jun 1, 2006, at 5:47 AM, [EMAIL PROTECTED] wrote:
Sorry for this off-topic question:

        in.createGraphics().getDeviceConfiguration();

The Graphics2D returned from in.createGraphics() can't be dispose
()'d.   How do we know that it's safe to do this without leaking
something?
I mention this because I see that you were careful to do it for the
output image...
     Graphics2D g2d = out.createGraphics();
     g2d.setComposite(AlphaComposite.Src);
g2d.drawImage(in, 0, 0, targetWidth,
idth, targetHeight, null);
     g2d.dispose();

Does the dispose() call potentially flush some drawing operations?
Is it simply not needed if you don't "write" to the graphics context?

Jim would probably be a better person to answer this question, but
since JDK 1.4 or so, Graphics.dispose() doesn't really do anything
heavyweight; we have other internal mechanisms for ensuring that
associated resources are disposed properly.  It doesn't (currently)
flush pending operations or anything like that, and nowadays it
doesn't really matter whether you call it or not (it won't leak if
you don't).  That said...

  Should you always dispose() every Graphics object that you create
just to be safe?

Yes, I think it is good practice to always call g.dispose() for every
Graphics object that you create.  Even though it is now effectively a
no-op, it serves as a useful marker in your code to say "I'm done
with this Graphics object now", which helps improve readability
especially when dealing with multiple Graphics objects in the same
block of code.

So tasting my own medicine, the code probably should've looked like
this:

    BufferedImage in = ImageIO.read(srcFile);
    Graphics2D gin = in.createGraphics();
    GraphicsConfiguration gc = gin.getDeviceConfiguration();
    gin.dispose();

    BufferedImage out =
        gc.createCompatibleImage(targetWidth, targetHeight, BITMASK);
    Graphics2D gout = out.createGraphics();
    gout.setComposite(AlphaComposite.Src);
    gout.drawImage(in, 0, 0, targetWidth, targetHeight, null);
    gout.dispose();
    ImageIO.write(out, "png", destFile);

Thanks,
Chris

[Message sent by forum member 'swpalmer' (swpalmer)]

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

======================================================================
=====
To unsubscribe, send email to [EMAIL PROTECTED] and include in
the body
of the message "signoff JAVA2D-INTEREST".  For general help, send
email to
[EMAIL PROTECTED] and include in the body of the message "help".

===========================================================================
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff JAVA2D-INTEREST".  For general help, send email to
[EMAIL PROTECTED] and include in the body of the message "help".

Reply via email to