Skip to main content

Proposal for compound Composites

1 reply [Last post]
jansan
Offline
Joined: 2005-02-24

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:

<br />
Composite myComposite = createMyComposite();<br />
Composite originalComposite = g2D.getComposite();<br />
g2D.setComposite(myComposite);<br />
paintALot(g2D);<br />
g2D.setComposite(originalComposite);<br />

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:

<br />
Composite myComposite = createMyComposite();<br />
Composite originalComposite = g2D.getComposite();<br />
if (originalComposite != null) {<br />
 CompoundComposite compoundComposite = new CompoundComposite(originalComposite, myComposite);<br />
 g2D.setComposite(compoundComposite);<br />
} else {<br />
 g2D.setComposite(myComposite);<br />
}<br />
paintALot(g2D);<br />
g2D.setComposite(originalComposite);<br />

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 JAVA2D-INTEREST@JAVA.SUN.COM 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?

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
tompalmer
Offline
Joined: 2006-08-26

This looks similar to what I was trying to accomplish when I encountered a problem. See here for my question:

http://forums.java.net/jive/thread.jspa?threadID=19117&tstart=0

I didn't read back to notice your post until now.