deckmagic
asked on
Graphics2D.setClip() not working?
I have the following function which sends an image and the rectangle size I would like to have clipped:
RenderedImage clippedImage = GraphicUtilities.clipImage (image, new java.awt.geom.Rectangle2D. Double(0, 0, 80, 60));
The clipImage method looks like this:
public static RenderedImage clipImage(Image image, Shape shape) {
// Create a buffered image in which to draw
BufferedImage bufferedImage = new BufferedImage(image.getWid th(null), image.getHeight(null), BufferedImage.TYPE_INT_RGB );
// Need to get image into bufferedImage
bufferedImage.getGraphics( ).drawImag e(image, 0, 0, null);
Graphics2D g2 = bufferedImage.createGraphi cs();
// Clip the image
if (bufferedImage != null) {
g2.setClip(shape);
g2.drawImage(bufferedImage , 0, 0, null);
g2.dispose();
}
return bufferedImage;
}
Any ideas as to why the returned image is the exact same size as the original image and isn't actually clipped at all? How do I correct this?
RenderedImage clippedImage = GraphicUtilities.clipImage
The clipImage method looks like this:
public static RenderedImage clipImage(Image image, Shape shape) {
// Create a buffered image in which to draw
BufferedImage bufferedImage = new BufferedImage(image.getWid
// Need to get image into bufferedImage
bufferedImage.getGraphics(
Graphics2D g2 = bufferedImage.createGraphi
// Clip the image
if (bufferedImage != null) {
g2.setClip(shape);
g2.drawImage(bufferedImage
g2.dispose();
}
return bufferedImage;
}
Any ideas as to why the returned image is the exact same size as the original image and isn't actually clipped at all? How do I correct this?
Hi
Here are some clip examples to look at and compare to what you are
doing : http://www.java2s.com/Code/Java/2D-Graphics-GUI/Clip.htm
Regards,
Tomas Helgi
Here are some clip examples to look at and compare to what you are
doing : http://www.java2s.com/Code/Java/2D-Graphics-GUI/Clip.htm
Regards,
Tomas Helgi
ASKER
Okay, so I rewrote the method so now the image appears to be clipped (an 80 x 60 rectangle in the upper-left corner), but the full size of the image is still 480x360. I thought clipping the image would also change the size of the image returned, how do I get the returned image to be 80x60?
public static RenderedImage clipImage(Image image, Shape shape) {
// Create a buffered image in which to draw
BufferedImage bufferedImage = new BufferedImage(image.getWid th(null), image.getHeight(null), BufferedImage.TYPE_INT_RGB );
Graphics2D g2 = bufferedImage.createGraphi cs();
// Clip the image
if (bufferedImage != null) {
g2.setClip(shape);
g2.drawImage(image, 0, 0, null);
g2.dispose();
}
return bufferedImage;
}
public static RenderedImage clipImage(Image image, Shape shape) {
// Create a buffered image in which to draw
BufferedImage bufferedImage = new BufferedImage(image.getWid
Graphics2D g2 = bufferedImage.createGraphi
// Clip the image
if (bufferedImage != null) {
g2.setClip(shape);
g2.drawImage(image, 0, 0, null);
g2.dispose();
}
return bufferedImage;
}
Create a new Buffered Image with the desired size (80x60) and
copy the old BufferedImage to the new BufferedImage and return that.
Regards,
Tomas Helgi
copy the old BufferedImage to the new BufferedImage and return that.
Regards,
Tomas Helgi
> I thought clipping the image would also change the size of the image returned,
No it does not affect the size
> how do I get the returned image to be 80x60?
create it that size in the first place
No it does not affect the size
> how do I get the returned image to be 80x60?
create it that size in the first place
ASKER
Okay, I'm confused. I have the following method that I iterate thru 6 times, the shape changes by 80 on each iteration:
public static RenderedImage clipImage(Image image, Shape shape) {
BufferedImage bufferedImage = new BufferedImage(shape.getBou nds().widt h, shape.getBounds().height, BufferedImage.TYPE_INT_RGB );
Graphics2D g2 = bufferedImage.createGraphi cs();
g2.setClip(shape);
g2.drawImage(image, 0, 0, null);
return bufferedImage;
}
The first time through, I get an image that is 80x60 that looks like the upper-left corner of image.
The second - sixth time through, I get a completely blank image.
From what I understood I thought this code did the following:
1. Create a new BufferedImage of the same size as the shape (80x60)
2. Assign the graphics context of this BufferedImage to g2
3. Clip the image according to the shape bounds
4. Draw this clipped portion into bufferedImage
If this is wrong, how would I rewrite it to work correctly? I basically have a sheet of 6 images that are 80x60 in size. I want to loop through this method, clip out each image, and return that image to be saved to a seperate file.
public static RenderedImage clipImage(Image image, Shape shape) {
BufferedImage bufferedImage = new BufferedImage(shape.getBou
Graphics2D g2 = bufferedImage.createGraphi
g2.setClip(shape);
g2.drawImage(image, 0, 0, null);
return bufferedImage;
}
The first time through, I get an image that is 80x60 that looks like the upper-left corner of image.
The second - sixth time through, I get a completely blank image.
From what I understood I thought this code did the following:
1. Create a new BufferedImage of the same size as the shape (80x60)
2. Assign the graphics context of this BufferedImage to g2
3. Clip the image according to the shape bounds
4. Draw this clipped portion into bufferedImage
If this is wrong, how would I rewrite it to work correctly? I basically have a sheet of 6 images that are 80x60 in size. I want to loop through this method, clip out each image, and return that image to be saved to a seperate file.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
That did it. Here is the code I used if anyone is interested:
public static RenderedImage clipImage(Image image, Shape shape) {
// Destination rectangle
int x1,y1,x2,y2;
x1 = 0;
y1 = 0;
x2 = shape.getBounds().width;
y2 = shape.getBounds().height;
// Source rectangle
int x3,y3,x4,y4;
x3 = shape.getBounds().x;
y3 = shape.getBounds().y;
x4 = x3 + shape.getBounds().width;
y4 = y3 + shape.getBounds().height;
// Create a buffered image in which to draw
BufferedImage bufferedImage = new BufferedImage(shape.getBou nds().widt h, shape.getBounds().height, BufferedImage.TYPE_INT_RGB );
Graphics2D g2 = bufferedImage.createGraphi cs();
// Clip the image
if (bufferedImage != null) {
g2.drawImage(image, x1, y1, x2, y2, x3, y3, x4, y4, null);
g2.dispose();
}
return bufferedImage;
}
public static RenderedImage clipImage(Image image, Shape shape) {
// Destination rectangle
int x1,y1,x2,y2;
x1 = 0;
y1 = 0;
x2 = shape.getBounds().width;
y2 = shape.getBounds().height;
// Source rectangle
int x3,y3,x4,y4;
x3 = shape.getBounds().x;
y3 = shape.getBounds().y;
x4 = x3 + shape.getBounds().width;
y4 = y3 + shape.getBounds().height;
// Create a buffered image in which to draw
BufferedImage bufferedImage = new BufferedImage(shape.getBou
Graphics2D g2 = bufferedImage.createGraphi
// Clip the image
if (bufferedImage != null) {
g2.drawImage(image, x1, y1, x2, y2, x3, y3, x4, y4, null);
g2.dispose();
}
return bufferedImage;
}
because the 1st thing u do is paint it all to the BI