Step-by-step code examples

Create a JFrame containing a MarvinSpace canvas

Let's create a simple MarvinSpace application.

First, we have to install the Jogl native libraries. MSpaceInstaller will do this for us.

The second step is creating a JFrame.

Third, we have to put the MarvinSpace canvas on it. MSpaceEasy makes it simple, we can also add several GUI components such as Popup Menu and MenuBar.

Finally the frame is ready:

public void createSimpleMarvinSpaceFrame() throws Exception {
//parameter true tells that dynamic loading of the Jogl native libraries is necessary
final mspace = new;

JFrame frame = new JFrame();
frame.setTitle(" ";
frame.setSize(800, 750);

mspace.setSize(600, 600);


Load a molecule

To go one step further, we will need some molecules to display. The following code lines can be placed for example before the frame.pack() call.

In case of having a SMILES String:

    final String molS = "C1C2=CC=CC=C2C3=C4CC5=CC=CC=C5C4=C6CC7=CC=CC=C7C6=C13";
Molecule mol = MolImporter.importMol(molS);
mspace.addMolecule( mol );

By default MarvinSpace checks whether the molecule is defined in plane or not, and calls Clean, Hydrogenize and Aromatize functions of the Molecule .

Loading from a file or URL:


Let's suppose we have a properly initilaized Vector containing Molecule objects. We can place them in different cells each:

    for(int i=0; i<molVector.size(); i++) {

Loading a molecule without calling Clean, Hydrogenize and Aromatize:

    mspace.addMoleculeWithoutChange( mol );

In the previous examples we added the molecules to the scene, but we can also load a molecule by closing all molecules before:

    mspace.openMolecule( mol );

Loading molecule to a specific cell (indexing starts from 0, from top to bottom and left to right):

    mspace.addMoleculeTo( mol, 1 );