## Sunday, April 18, 2010

### CDK-JChemPaint #4: embedding the renderer into a Swing panel

Now that we covered the utmost basics of using the CDK-JChemPaint patch (see #1, #2, #3), it is time to move on. I am happy to hear that so many people have started using the new rendering architecture, either via the EBI JChemPaint Swing applet/application branch, or via the CDK-JChemPaint patch.

A couple of issues and questions came up (scaling not working as expected; how to layout reactions; how to get charges to show up), and I will look at those shortly. But before I get into those matters, I'll first show how to use the renderer with a Swing JPanel (I'll do the SWT alternative later). First, we need to subclass the JPanel:
class JCPPanel extends JPanel {

IMolecule mol;
AtomContainerRenderer renderer;
int width;
int height;

public JCPPanel(IMolecule mol, int width, int height) {
super();
this.setSize(width, height);
this.mol = mol;
this.width = width;
this.height = height;

// generators make the image elements
List generators = new ArrayList();

// the renderer needs to have a toolkit-specific font manager
renderer = new AtomContainerRenderer(
generators, new AWTFontManager()
);
}

public Dimension getPreferredSize() {
return new Dimension(width, height);
}

public void paint(Graphics graphics) {
// the call to 'setup' only needs to be done on the first paint
renderer.setup(mol, new Rectangle(getWidth(), getHeight()));

// paint the background
graphics.setColor(Color.WHITE);
graphics.fillRect(0, 0, getWidth(), getHeight());

// the paint method also needs a toolkit-specific renderer
renderer.paint(mol, new AWTDrawVisitor(graphics));
}

}
The panel does not implement resizing, and it could consider caching the image too, to speed things up a bit. But, we'll use this as a starting point.

We can then embed this panel into a JFrame to make a small runable application:
int WIDTH = 600;
int HEIGHT = 600;

// create molecule
IMolecule triazole = MoleculeFactory.make123Triazole();
StructureDiagramGenerator sdg = new StructureDiagramGenerator();
sdg.setMolecule(triazole);
sdg.generateCoordinates();
triazole = sdg.getMolecule();

// create the frame
JFrame frame = new JFrame("Swinging CDK-JChemPaint");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

JCPPanel panel = new JCPPanel(triazole, WIDTH, HEIGHT);