drawing on a Grapics or BufferedImage - still unclear on the concept
I'm trying to implement a red-eye removal feature, and to do so, I need to apply a Graphics2D filter to the contents of a Shape. Here's what I'm doing
private Image image;// this is the image being draw on the JLabel via ImageIcon
...
publicvoid paintComponent( Graphics g ){
super.paintComponent( g );
Graphics2D g2 = (Graphics2D)g;
Stack<Rectangle> redeyes = Pick.peek().getRedeye();
// apply RedEye fix to this Stack of "circles"
for(int i=0;i<redeyes.size();i++){
Rectangle r = redeyes.elementAt(i);
Ellipse2D.Float circle =new Ellipse2D.Float(r.x, r.y, r.width, r.height);
applyFilter2Shape(g2, (Shape)circle );
}
}
publicvoid applyFilter2Shape(Graphics2D g2, Shape s){
// this works, I can draw a red circle just fine
//g2.setColor(Color.RED);
//g2.draw( s );
//g2.setColor(Color.WHITE); // Should be drawing TRANSPARENT on a black RGB as an alpha composite
//g2.fill( s );
LookupTable lookup_table =newshort[256];
// assume there is a filter here
LookupOp lookup_op =new LookupOp (lookup_table,null);
lookup_op =new LookupFilterOp();
g2.setClip(s);
// write g2.clip into a new buffered image and filter
BufferedImage clipBI =new BufferedImage (s.getBounds().width, s.getBounds().height, BufferedImage.TYPE_INT_RGB);
g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC );
g2.drawImage(clipBI, 0, 0, s.getBounds().width, s.getBounds().height,null);
clipBI = lookup_op.filter (clipBI,null);
// draw clipBI onto g2
Graphics2D g2clip = clipBI.createGraphics();
g2clip.setClip(s);
// I know I'll draw the filtered image in the top left corner, but that's good enough for now
g2clip.drawImage((BufferedImage)image, 0, 0, s.getBounds().width, s.getBounds().height,null);
}
But nothing happens. What am I doing wrong? Is there a better way to do this?>
[3108 byte] By [
mixersofta] at [2007-11-27 6:31:54]

// write g2.clip into a new buffered image and filter
BufferedImage clipBI = new BufferedImage (s.getBounds().width, s.getBounds().height, BufferedImage.TYPE_INT_RGB);
g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC );
g2.drawImage(clipBI, 0, 0, s.getBounds().width, s.getBounds().height, null);
clipBI = lookup_op.filter (clipBI, null);
// draw clipBI onto g2
Graphics2D g2clip = clipBI.createGraphics();
g2clip.setClip(s);
// I know I'll draw the filtered image in the top left corner, but that's good enough for now
g2clip.drawImage((BufferedImage)image, 0, 0, s.getBounds().width, s.getBounds().height, null);
It looks like you're:
1) Creating a new, empty BufferedImage
2) Drawing that empty BufferedImage to the screen
3) Drawing onto the empty BufferedImage
Maybe you should swap 2) and 3).
I tried to flip it around but it still didn't work
// image was set at initialization, is this a problem?
Image image = getBufferedImage();
// get the source BI from the component
BufferedImage srcBI = (BufferedImage)image
lookup_op = getfLookupFilterOp();
Graphics2D g2 = srcBI.createGraphics();
g2.setClip(s);
// draw clipped srcBI onto Graphics2D of new BufferedImage, clipBI
clipBI = new BufferedImage (s.getBounds().width, s.getBounds().height, BufferedImage.TYPE_INT_RGB);
Graphics2D g2clip = clipBI.createGraphics();
g2clip.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC );
g2clip.drawImage(srcBI, 0, 0, s.getBounds().width, s.getBounds().height, null);
// apply filter to clipBI
clipBI = lookup_op.filter (clipBI, null);
// draw filtered clipBI onto g2 of srcBI
g2.drawImage(clipBI, 0, 0, s.getBounds().width, s.getBounds().height, null);
BTW, the code is in a JLabel with the image in an ImageIcon set at initializaion. what I'm trying to do is something like the following:
0) draw a BufferedImage onto a JLabel via ImageIcon
1) draw 1 or more Shapes on the JLabel's graphics context for the "mask"
2) "clip" the pixels inside the Shape(s)
- do I "clip" the original BufferedImage (preferred) or the JLabel ImageIcon image?
3) apply a Graphics2D filter to the "clipped" region(s)
4) "paste" the results back on top of the BuffreredImage object,
5) draw the modified BufferedImage onto the JLabel's ImageIcon image.
But I'm really confused when I start mixing the JLabel.paintComponent( Graphics g) and the Graphics2D g2 BufferedImage.createGraphics()
TIA