Re: How do you crop an image?
On Mar 26, 10:18 am, "phillip.s.pow...@gmail.com"
<phillip.s.pow...@gmail.com> wrote:
On Mar 26, 2:28 am, Knute Johnson <nos...@rabbitbrush.frazmtn.com>
wrote:
OK. Let's start from scratch then. You want to get a BufferedImage
from a file or the net and create a sub image that from the original.
Got it to work! Turns out that my research wasn't complete and I
overlooked the solution someone else on the Sun Java forums provided
that incorporates perfectly (as it is supposed to) into my code:
[snip]
Even better version I think:
/**
* Perform crop
* <a href="http://forum.java.sun.com/thread.jspa?
threadID=627125&messageID=3587532">Reference</a>
*/
public void crop() {
Rectangle r = ImageCropper.this.cropPanel.getClip();
// OFFSET CLIP FROM VIEW TO RASTER MODEL
try {
int x = (ImageCropper.this.cropPanel.getWidth() -
ImageCropper.this.cropPanel.getImage().getWidth()) / 2;
int y = (ImageCropper.this.cropPanel.getHeight() -
ImageCropper.this.cropPanel.getImage().getHeight()) / 2;
BufferedImage clippedImage =
ImageCropper.this.cropPanel.getImage().getSubimage(
(int)r.getX() - x,
(int)r.getY() - y,
(int)r.getWidth(),
(int)r.getHeight());
ImageCropper.this.cropPanel.setImage(clippedImage);
ImageCropper.this.cropPanel.setup();
ImageCropper.this.validate();
} catch (RasterFormatException e) /* CLIP IS OUT OF BOUNDS
*/ {
JOptionPane.showMessageDialog(
ImageCropper.this, "Your selection is out of bounds of
the image",
"Invalid Selection",
JOptionPane.ERROR_MESSAGE);
ImageCropper.this.cropPanel.resetClip();
}
}