This example demonstrates how to create images from the contents of the MarvinSketch canvas using two different approaches.
The image below shows the sample GUI component of this example. There
are two buttons below the MSketchPane component, Get Sketch Image and
Get Molecule Image, that will demonstrate the difference between the two methods.
To make it easy, the images that are created will be shown by placing them
on a javax.swing.JLabel
component below the two buttons.
By pressing the Get Sketch Image button, the image of the canvas using the actual magnification is generated and set as the icon image of the label.
The Get Molecule Image button creates a 200 x 200 pixel size image, automatically computing the appropriate scale factor. In this example we modify the default behavior with a parameter. If this scale factor is greater than 100%, the scale factor is explicitly set to 100%, so that small molecules will not be overscaled. Note, that the molecule is automatically centered in the image.
The very brief code samples below are taken
from SketchImages.java
.
// getting the actual scale factor of the canvas double currentScale = sketchPane.getScale(); // getting the image of the canvas with the current scale factor Image canvasImage = sketchPane.getImage(currentScale);
The image format along with additional options should be set
as a String. The options are separated from the format identifier with a colon, and from each other
with commas.
More details of specifying image formats are available in the Image Generation documentation.
With the use of the maxscale
parameter the scale factor is not allowed to be grater than that of 100%.
String format = "jpeg:w200,h200,maxscale28"; byte[] imageData = sketchPane.getMol().toBinFormat(format);As of Marvin 5.2.0 images can be created with transparent background in PNG, SVG, PDF and EMF formats. To set the transparent background, the
transbg
parameter should be set along with a
background color containing alpha values in RGBA form.
String format = "png:w200,h200,maxscale28,transbg,#ffffffff";
There is an alternative to the image generation method which does not require to set the image
format explicitely, and retrieves a java.awt.Image
object instead of the image data.
This conversion is more appropriate in case the image is not written to file.
Image image = sketchPane.getMol().toObject("image:w200,h200");
The advantages of converting the molecule to an image format are straightforward:
However there is one disadvantage as well, namely that the various functionalities offered by the JavaBean component affecting the display are not directly available on the molecule itself. This means that all display options being different from the default has to be set explicitely as the parameter of the conversion String.
For example, to convert a molecule to an image where the CHIRAL flags are visible is as follows:
String format = "jpeg:w200,h200,chiral_all"; byte[] imageData = sketchPane.getMol().toBinFormat(format);The available options are enumerated as Export options of the Image Generation documentation.