Setting the color of a clipped area
Hello, I currently have a problem with clipping an image. The clipping itself is actually doing fine, however the clipped area is always black.
I tried to set the color to something else that black by setting the g2d.setBackground (I would like to have that area transparent or a least white) but that does not change anything.
Please find the code below, I would appreciate any feedback.
Many thanks
Martin
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
public class Clipping {
public static void main(String[] args) {
int width = 1024;
int height = 744;
ImageIcon image = null;
try {
image = new ImageIcon(
new URL(
"http://java.sun.com/products/jfc/tsc/sightings/S20/smartcvs/annotate.png"));
} catch (MalformedURLException e) {
e.printStackTrace();
}
BufferedImage bufferedImage = new BufferedImage(width, height,
BufferedImage.TYPE_INT_RGB);
Graphics2D g2d = (Graphics2D) bufferedImage.getGraphics();
g2d.setBackground(Color.white);
g2d.setClip(0, 0, width - 200, height);
g2d.drawImage(image.getImage(), 0, 0, null);
// clear resources
g2d.dispose();
try {
ImageIO.write(bufferedImage, "png", new File("clipped.png"));
} catch (IOException e) {
e.printStackTrace();
}
}
}

