Friday, July 06, 2012

CDK-JChemPaint #11: coloring selections

This is something I have been asked about many times. I had to find out myself, as I had no experience with this corner of the CDK rendering stack. In fact, I think there will be a second, follow-up post on that later, where I will explain I did it all wrong :)

Anyway, here is example code for how to mark a substructure. It a variations of the triazole examples I have given earlier. First thing is to add the proper generator:

// generators make the image elements
List<IGenerator> generators = new ArrayList<IGenerator>();
generators.add(new BasicSceneGenerator());
generators.add(new ExternalHighlightGenerator());
generators.add(new BasicBondGenerator());
generators.add(new BasicAtomGenerator());

And we then also configure things to, for example, make the selection halos a bit larger and make them red:

model = renderer.getRenderer2DModel();

Finally, we set the selection we like to color:

IAtomContainer selection = new AtomContainer();
for (int i=0; i<2; i++) {
  bond = triazole.getBond(i);

And the result then looks like this:

The full script can be downloaded here. A downside of this script is that the background of the symbol is not in the same color as the selection highlight. Also I do not think you can color multiple selection at the same time. But, I guess it is a start of an answer.

BTW, the new JChemPaint applet/application can be downloaded its new hangout at