PageRenderTime 48ms CodeModel.GetById 39ms app.highlight 7ms RepoModel.GetById 1ms app.codeStats 0ms

/rcdkjar/src/org/guha/rcdk/view/panels/MoleculeCell.java

http://github.com/rajarshi/cdkr
Java | 68 lines | 53 code | 9 blank | 6 comment | 1 complexity | bf25dc96a60080e25f35f8ea4ae4fbcf MD5 | raw file
 1package org.guha.rcdk.view.panels;
 2
 3import org.guha.rcdk.view.RcdkDepictor;
 4import org.openscience.cdk.DefaultChemObjectBuilder;
 5import org.openscience.cdk.exception.CDKException;
 6import org.openscience.cdk.interfaces.IAtomContainer;
 7import org.openscience.cdk.smiles.SmilesParser;
 8
 9import javax.swing.*;
10import java.awt.*;
11import java.awt.image.BufferedImage;
12import java.io.IOException;
13
14/**
15 * A <code>JPanel</code> to display 2D chemical structure depictions.
16 *
17 * @author Rajarshi Guha
18 */
19public class MoleculeCell extends JPanel {
20
21    private int preferredWidth;
22    private int preferredHeight;
23    private boolean isNew;
24    IAtomContainer atomContainer;
25    RcdkDepictor depictor;
26    BufferedImage bufferedImage;
27
28    public MoleculeCell(IAtomContainer atomContainer, RcdkDepictor depictor) throws IOException, CDKException {
29        preferredHeight = depictor.getHeight();
30        preferredWidth = depictor.getWidth();
31        this.depictor = depictor;
32        this.atomContainer = atomContainer;
33        this.setPreferredSize(new Dimension(preferredWidth, preferredHeight));
34        this.setBackground(Color.WHITE);
35        this.setBorder(BorderFactory.createEtchedBorder());
36        bufferedImage = depictor.getImage(atomContainer);
37        isNew = true;
38    }
39
40    public void paint(Graphics g) {
41        super.paint(g);
42        if (isNew) {
43            g.drawImage(bufferedImage, 0, 0, this);
44            isNew = false;
45        } else
46            try {
47                depictor.setWidth(getWidth());
48                depictor.setHeight(getHeight());
49                g.drawImage(depictor.getImage(atomContainer), 0, 0, this);
50            } catch (CDKException e) {
51                e.printStackTrace();
52            }
53    }
54
55    public static void main(String[] args) throws CDKException, IOException {
56        SmilesParser sp = new SmilesParser(DefaultChemObjectBuilder.getInstance());
57//        IAtomContainer container = sp.parseSmiles("C1CN2CCN(CCCN(CCN(C1)Cc1ccccn1)CC2)C");
58        IAtomContainer container = sp.parseSmiles("[Na+].[Na+].[Na+].[Na+].[Na+].[Na+].[Na+].[Na+].[Na+].[Na+].[Na+].[Na+].[Na+].CCCCc1ccc(CO[C@H]2O[C@H](COS(=O)(=O)[O-])[C@@H](OS(=O)(=O)[O-])[C@H](OS(=O)(=O)[O-])[C@@H]2O[C@H]3O[C@H](COS(=O)(=O)[O-])[C@@H](OS(=O)(=O)[O-])[C@H](O[C@H]4O[C@H](COS(=O)(=O)[O-])[C@@H](OS(=O)(=O)[O-])[C@H](O[C@H]5O[C@H](COS(=O)(=O)[O-])[C@@H](OS(=O)(=O)[O-])[C@H](OS(=O)(=O)[O-])[C@@H]5OS(=O)(=O)[O-])[C@@H]4OS(=O)(=O)[O-])[C@@H]3OS(=O)(=O)[O-])cc1 ");
59        RcdkDepictor depictor = new RcdkDepictor(300, 300, 1.3, "cow", "off", "reagents", true, false, 100, "");
60        MoleculeCell mcell = new MoleculeCell(container, depictor);
61        JFrame frame = new JFrame("Molecule Cell");
62        frame.getContentPane().add(mcell);
63        frame.pack();
64        frame.setVisible(true);
65    }
66
67}
68