Before loading:  Mem Usage: 18700K -- VM Size: 16856K
After loading: Mem Usage: 28656K  -- VM Size: 34304K

All that it does
is read the image in as a ImageIcon, convert the ImageIcon's getImage
result to a BufferedImage, store a reference to the BufferedImage for
later use in manipulations, and then set a JLabel's displayed icon to
this BufferedImage.

On the input end, instead of the ImageIcon=>BufferedImage conversion, use the Image I/O package to load the image directly as a BufferedImage. Check out the javax.imageio.ImageIO class and its various static read methods for more information.

Then on the output end, instead of using a JLabel with an ImageIcon to
display the image, create your own JComponent and override its
paintComponent(Graphics g) method to render the image directly.  This
Graphics parameter that you receive can be cast to a Graphics2D object
and between the two classes you have access to several methods which
let you render the image in more dynamic ways.  For example, there is
the Graphics.drawImage(x, y, w, h) method which lets you draw the image
scaled to a new size on the fly.  There is also the
Graphics2D.drawImage(img, transform) method which lets you perform more
complex (rotation, shearing) transforms on the fly.  There is also a
Graphics2D.drawImage(img, bufimgop) which lets you apply an arbitrary
BufferedImageOp to the image as it is rendered to the screen.  At the
very least, avoiding the ImageIcon for use in displaying a
BufferedImage avoids a nasty side effect of having the ImageIcon treat
the BufferedImage as if it is an asynchronous image and having it go
through an unnecessary "load your data" cycle which could potentially
bloat memory as well.

The reason this wasn't more obvious in the first place is that you are
falling victim here to the fact that our system provides several
different mechanisms for loading, manipulating, and displaying images
that all store their data differently, but since we provide lots of
convenience APIs to transfer the data around between them then it
doesn't appear that way to the casual developer.

The ImageIcon class you are using in 2 places uses the original image
APIs that have been around since the 1.0 versions and which were
targetted at Applets.  These APIs were designed to manage modest images
that might be appropriate for web page design elements and to do so
asynchronously so that your applet is not necessarily stuck waiting for
image data to arrive over the network while it is trying to get
initialized.  The image data for these APIs is stored very opaquely and
is very difficult to manipulate and needs to be converted when moving
to or from a BufferedImage.

                               ...jim

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