Pages

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();
model.set(
  ExternalHighlightGenerator.ExternalHighlightDistance.class,
  (double)12
);
model.set(
  RendererModel.ExternalHighlightColor.class,
  Color.red
);


Finally, we set the selection we like to color:

IAtomContainer selection = new AtomContainer();
for (int i=0; i<2; i++) {
  bond = triazole.getBond(i);
  selection.addAtom(bond.getAtom(0));
  selection.addAtom(bond.getAtom(1));
  selection.addBond(bond);
}
model.setExternalSelectedPart(selection);

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 jchempaint.github.com.


1 comment:

  1. "A downside of this script is that the background of the symbol is not in the same color as the selection highlight"

    This would be a great feature but pretty difficult. This would allow you to use a single render on multiple backgrounds without having to change the background in the generator. When I looked into it the square background is painted over the bonds. I thought it would be possible to just paint a clear rectangle to 'clear' where the bonds were drawn but of course they still get rendered underneath. I think the only way it would have to be done is to stop the bonds short of connecting.

    I was also thinking it might look nicer to have the padding around the text as a circle. The double bonds connecting to the nitrogen on the left don't look as nice as the ones connecting to the bottom right carbon (imagine the red circle wasn't there and replaced with white.

    Anyways, just some musings on aesthetics :-)

    J

    ReplyDelete