This is just a thought that I had yesterday when I used the AlphaComposite 
class, but I thought it may be worth sharing with you:

Setting a composite when painting on java.awt.Graphics2D object is usually done 
like this:
[code]
Composite myComposite = createMyComposite();
Composite originalComposite = g2D.getComposite();
g2D.setComposite(myComposite);
paintALot(g2D);
g2D.setComposite(originalComposite);
[/code]
Just yesterday I decided that this will not work in some cases. Imagine you 
want to create a fancy effect by making a translucent panel. To achieve this 
you set an AlphaComposite.SrcOver with 0.5f alpha parameter in the panel's 
paintComponent() method . On that panel you have a bunch of labels with html 
text. You decided that if a label is disabled, it should be translucent, using 
an AlphaComposite.SrcOver with 0.5f alpha. This works well on a normal panel, 
but with our fancy translucent panel the disabled text will suddenly look 
exactly the same as the enabled text (both will be translucent with alpha 0.5). 
The reason is simply that the composites are replaced, while they should be 
combined to get the desired effect.

If you look at Romain Guy's recent blog entries, you will see that using 
Composites will be used more frequently, and sooner or later someone will run 
into a problem similar to the one I described. The solution would be to have a 
class CompoundComposite, that simply takes two Composite objects in the 
constructor. The code from the beginning would then look like this:
[code]
Composite myComposite = createMyComposite();
Composite originalComposite = g2D.getComposite();
if (originalComposite != null) {
 CompoundComposite compoundComposite = new CompoundComposite(originalComposite, 
myComposite);
 g2D.setComposite(compoundComposite);
} else {
 g2D.setComposite(myComposite);
}
paintALot(g2D);
g2D.setComposite(originalComposite);
[/code]
I do not know too much about the inner workings of Java2D, but I would like to 
know if it is possible at all to create a class that can combine two arbitrary 
Composites. Maybe it is even very simple. What if the createContext() method of 
such a class creates a CompundCompositeContext with a CompositeContext from 
each of the simple Composites (created by calling the simple Composites' 
createContext() method). The CompundCompositeContext's compose() method would 
then call the compose() method on each of these CompositeContexts.

What do you think? Am I on the right track? And if so, should this always be 
used when working with Composites? And if so, should CompoundComposite maybe be 
added to the java.awt package?

Just for the record, a similar problem exists for the AffineTransform, but here 
the concatenate() method in the AffineTransform class does what my proposed 
CompoundComposite class would do.


Cheers,
Jan


P.S.: Sending messages to this forum through [EMAIL PROTECTED] does not seem to 
work for me anymore (no error messages, no replies, message just never 
arrives). I have to use the java.net web interface. Does anybody have an idea 
how I can fix this?
[Message sent by forum member 'jansan' (jansan)]

http://forums.java.net/jive/thread.jspa?messageID=160667

===========================================================================
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff JAVA2D-INTEREST".  For general help, send email to
[EMAIL PROTECTED] and include in the body of the message "help".

Reply via email to