Hello,
I would like to draw multiple partially overlapping figures in xor mode (applying the even-odd rule) and then place the drawing on another image.
I've tried this on a JPanel:
public void paintComponent(Graphics g) {
super.paintComponent( g );
Graphics2D g2 = (Graphics2D) g;
Dimension d = getSize();
int w = d.width;
int h = d.height;
BufferedImage buffImg = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
Graphics2D gbi = buffImg.createGraphics();
gbi.setColor(new Color(1f,1f,1f,0f)); // I except that it clears the Graphics with "white" transparent color
gbi.fillRect(0, 0, d.width, d.height);
gbi.setXORMode(new Color(0f,0f,1f, 1f)); // it would be tell Graphics to alternate opaque blue and transparent white
int rectx = w/4; // draw the two partially overlapping figure
int recty = h/4;
gbi.fill(new Rectangle2D.Double(rectx, recty, 150, 100));
gbi.fill(new Ellipse2D.Double(rectx+rectx/2,recty+recty/2,150,100));
g2.setColor(Color.green);
g2.fillRect(30,30, 100, 140); // it shows what happens with the previously drawed things
g2.drawImage(buffImg, null, 0, 0);
}
It draws only the green rectangle, ie. the BufferedImage contains tansparency only. :(
If I change the clearing transparent white to this way
gbi.setColor(new Color(1f,1f,1f,1f)); // opaque white
then the blue figures shows correctly, but the green rectangle goes away because of no transparency.
What did I wrong?
Any working solution/idea would be greatly appreciated.
Thanks,
Feri
Andrey Kuznetsov - 24 Aug 2006 21:05 GMT
> I would like to draw multiple partially overlapping figures in xor mode
> (applying the even-odd rule) and then place the drawing on another image.
[quoted text clipped - 43 lines]
>
> Any working solution/idea would be greatly appreciated.
fill these 2 rectangles with opaque white first.
Andrey

Signature
http://uio.imagero.com Unified I/O for Java
http://reader.imagero.com Java image reader
http://jgui.imagero.com Java GUI components and utilities
Zaka Ferenc - 24 Aug 2006 22:12 GMT
Uhm... I think I dont't understand you.
Which two rectangle? Did you mean the blue Ellipse2D and Rectangle2D? I've tried this:
// make sure the entire image is transparent
gbi.setColor(new Color(1f,1f,1f,0f));
gbi.fillRect(0, 0, d.width, d.height);
int rectx = w/4;
int recty = h/4;
Rectangle2D r2d=new Rectangle2D.Double(rectx, recty, 150, 100);
Ellipse2D e2d=new Ellipse2D.Double(rectx+rectx/2,recty+recty/2,150,100);
// fill with opaque white as you mentioned
gbi.setColor(new Color(1f,1f,1f,1f));
gbi.fill(r2d);
gbi.fill(e2d);
//set to xor to blue
gbi.setXORMode(new Color(0f,0f,1f,1f));
// fill again
gbi.fill(r2d);
gbi.fill(e2d);
Unfortunately it doesn't solve the problem. Parts of the BufferedImage where never was drawn any objects are transparent.
But the intersection of the Rectangle2D and Ellipse2D is not transparent but white, so it covers the underlaying surface (the green one in the example below).
Or did you mean anything else I didn't understand?
Thanks,
Feri
Andrey Kuznetsov írta:
>>I would like to draw multiple partially overlapping figures in xor mode
>>(applying the even-odd rule) and then place the drawing on another image.
[quoted text clipped - 47 lines]
>
> Andrey
Andrey Kuznetsov - 25 Aug 2006 10:36 GMT
> Which two rectangle? Did you mean the blue Ellipse2D and Rectangle2D?
yup
since it didn't worked I suggest another (a bit tricky) way -
use two BufferedImages:
first (TYPE_INT_ARGB) for image data and second (TYPE_BYTE_GRAY) for alpha.
1) first fill first image with opaque white and second with black.
2) draw your figures to both images
3) copy pixel data from second image to alpha channel of first image (you
will need access to DataBuffer)
Andrey

Signature
http://uio.imagero.com Unified I/O for Java
http://reader.imagero.com Java image reader
http://jgui.imagero.com Java GUI components and utilities