Add calls to Graphics.dispose() and Image.flush() whereever possible.

Rewrite protected resize to directly set image, width, and height.
This commit is contained in:
hns 2005-04-07 14:15:46 +00:00
parent a74ec62b68
commit 530afc9f88

View file

@ -85,7 +85,9 @@ public class ImageWrapper {
if (!(image instanceof BufferedImage)) { if (!(image instanceof BufferedImage)) {
BufferedImage buffered = new BufferedImage(width, height, BufferedImage buffered = new BufferedImage(width, height,
BufferedImage.TYPE_INT_ARGB); BufferedImage.TYPE_INT_ARGB);
buffered.createGraphics().drawImage(image, 0, 0, null); Graphics2D g2d = buffered.createGraphics();
g2d.drawImage(image, 0, 0, null);
g2d.dispose();
setImage(buffered); setImage(buffered);
} }
return (BufferedImage)image; return (BufferedImage)image;
@ -111,10 +113,17 @@ public class ImageWrapper {
* Any code that is changing the internal image should do it through this function * Any code that is changing the internal image should do it through this function
* to make sure getGraphcis() returns a valid graphics object the next time it is called. * to make sure getGraphcis() returns a valid graphics object the next time it is called.
*/ */
protected void setImage(Image image) { protected void setImage(Image img) {
this.image = image; // flush image and dispose graphics before updating them
if (graphics != null) {
graphics.dispose();
graphics = null; graphics = null;
} }
if (image != null) {
image.flush();
}
image = img;
}
/** /**
* Creates and returns a copy of this image. * Creates and returns a copy of this image.
@ -150,7 +159,10 @@ public class ImageWrapper {
* Dispose the Graphics context and null out the image. * Dispose the Graphics context and null out the image.
*/ */
public void dispose() { public void dispose() {
if (image != null) {
image.flush();
image = null; image = null;
}
if (graphics != null) { if (graphics != null) {
graphics.dispose(); graphics.dispose();
graphics = null; graphics = null;
@ -253,7 +265,7 @@ public class ImageWrapper {
/** /**
* Draws another image to this image. * Draws another image to this image.
* *
* @param filename ... * @param image ...
* @param x ... * @param x ...
* @param y ... * @param y ...
*/ */
@ -319,7 +331,7 @@ public class ImageWrapper {
/** /**
* resizes the image using the Graphics2D approach * resizes the image using the Graphics2D approach
*/ */
protected BufferedImage resize(int w, int h, boolean smooth) { protected void resize(int w, int h, boolean smooth) {
BufferedImage buffered = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB); BufferedImage buffered = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
Graphics2D g2d = buffered.createGraphics(); Graphics2D g2d = buffered.createGraphics();
@ -339,7 +351,10 @@ public class ImageWrapper {
); );
g2d.drawImage(image, at, null); g2d.drawImage(image, at, null);
g2d.dispose(); g2d.dispose();
return buffered; setImage(buffered);
// set new width/height
width = w;
height = h;
} }
/** /**
@ -356,8 +371,8 @@ public class ImageWrapper {
); );
// if the image is scaled, used the Graphcis2D method, otherwise use AWT: // if the image is scaled, used the Graphcis2D method, otherwise use AWT:
if (factor > 1f) { if (factor > 1f) {
// scalie it with the Graphics2D approach for supperiour quality. // scale it with the Graphics2D approach for supperiour quality.
setImage(resize(w, h, true)); resize(w, h, true);
} else { } else {
// Area averaging has the best results for shrinking of images: // Area averaging has the best results for shrinking of images:
@ -369,10 +384,10 @@ public class ImageWrapper {
// this version is up to 4 times faster than getScaledInstance: // this version is up to 4 times faster than getScaledInstance:
ImageFilterOp filter = new ImageFilterOp(new AreaAveragingScaleFilter(w, h)); ImageFilterOp filter = new ImageFilterOp(new AreaAveragingScaleFilter(w, h));
setImage(filter.filter(getBufferedImage(), null)); setImage(filter.filter(getBufferedImage(), null));
}
width = w; width = w;
height = h; height = h;
} }
}
/** /**
* Resize the image, using a fast and cheap algorithm * Resize the image, using a fast and cheap algorithm
@ -381,9 +396,7 @@ public class ImageWrapper {
* @param h ... * @param h ...
*/ */
public void resizeFast(int w, int h) { public void resizeFast(int w, int h) {
image = resize(w, h, false); resize(w, h, false);
width = w;
height = h;
} }
/** /**