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:

Composite myComposite = createMyComposite();
Composite originalComposite = g2D.getComposite();
g2D.setComposite(myComposite);
paintALot(g2D);
g2D.setComposite(originalComposite);

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:

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);

Since 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 very simple. What if its
createContext() method 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 of each of the simple 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 does what my
proposed CompoundComposite class would do.


Cheers,
Jan

===========================================================================
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