Search in sources :

Example 1 with AWTDrawVisitor

use of org.openscience.cdk.renderer.visitor.AWTDrawVisitor in project Smiles2Monomers by yoann-dufresne.

the class AbstractPictureGenerator method createPNG.

/**
 * Create a PNG picture of molecule with width and height sizes
 * @param molecule Molecule to be print
 * @param outfile Out file
 */
public void createPNG(IMolecule molecule, File outfile) {
    /* Warning : This part of code change IAtoms coordinates
		StructureDiagramGenerator sdg = new StructureDiagramGenerator();
		sdg.setMolecule(molecule);
		try {
			sdg.generateCoordinates();
			molecule = sdg.getMolecule();
		} catch (CDKException e1) {
			e1.printStackTrace();
		}*/
    // the draw area and the image should be the same size
    Rectangle drawArea = new Rectangle(0, 0, SIZE, SIZE);
    this.renderer.setup(molecule, drawArea);
    Rectangle diagramBounds = this.renderer.calculateDiagramBounds(molecule);
    int width = diagramBounds.width;
    int height = diagramBounds.height;
    int diff = Math.abs(width - height);
    int max = width > height ? width : height;
    int xshift = width > height ? 0 : diff / 2;
    int yshift = width > height ? diff / 2 : 0;
    // Recenter image
    this.renderer.shiftDrawCenter(xshift - diagramBounds.x, yshift - diagramBounds.y);
    Image image = new BufferedImage(max, max, BufferedImage.TYPE_INT_RGB);
    // Drawing options
    this.model.set(ColoredAtomGenerator.KekuleStructure.class, true);
    this.model.set(ColoredAtomGenerator.ColorByType.class, true);
    // paint the background
    Graphics2D g2 = (Graphics2D) image.getGraphics();
    g2.setColor(Color.WHITE);
    g2.fillRect(0, 0, max, max);
    // the paint method also needs a toolkit-specific renderer
    this.renderer.paint(molecule, new AWTDrawVisitor(g2));
    try {
        ImageIO.write((RenderedImage) image, "PNG", outfile);
    } catch (IOException e) {
        e.printStackTrace();
    }
}
Also used : ColoredAtomGenerator(io.imgs.coloration.ColoredAtomGenerator) AWTDrawVisitor(org.openscience.cdk.renderer.visitor.AWTDrawVisitor) Rectangle(java.awt.Rectangle) IOException(java.io.IOException) Image(java.awt.Image) BufferedImage(java.awt.image.BufferedImage) RenderedImage(java.awt.image.RenderedImage) BufferedImage(java.awt.image.BufferedImage) Graphics2D(java.awt.Graphics2D)

Example 2 with AWTDrawVisitor

use of org.openscience.cdk.renderer.visitor.AWTDrawVisitor in project Smiles2Monomers by yoann-dufresne.

the class PictureGenerator method createPNG.

/**
 * Create a PNG picture of molecule with width and height sizes
 * @param smiles SMILES of the molecule to be print
 * @param outfile Out file
 * @throws InvalidSmilesException
 */
public void createPNG(String smiles, File outfile) throws InvalidSmilesException {
    IMolecule molecule = this.transform(smiles);
    // the draw area and the image should be the same size
    Rectangle drawArea = new Rectangle(0, 0, SIZE, SIZE);
    this.renderer.setup(molecule, drawArea);
    Rectangle diagramBounds = this.renderer.calculateDiagramBounds(molecule);
    int width = diagramBounds.width;
    int height = diagramBounds.height;
    int diff = Math.abs(width - height);
    int max = width > height ? width : height;
    int xshift = width > height ? 0 : diff / 2;
    int yshift = width > height ? diff / 2 : 0;
    // Recenter image
    this.renderer.shiftDrawCenter(xshift - diagramBounds.x, yshift - diagramBounds.y);
    Image image = new BufferedImage(max, max, BufferedImage.TYPE_INT_RGB);
    // Drawing options
    this.model.set(BasicAtomGenerator.KekuleStructure.class, true);
    this.model.set(BasicAtomGenerator.ColorByType.class, true);
    // paint the background
    Graphics2D g2 = (Graphics2D) image.getGraphics();
    g2.setColor(Color.WHITE);
    g2.fillRect(0, 0, max, max);
    // the paint method also needs a toolkit-specific renderer
    this.renderer.paint(molecule, new AWTDrawVisitor(g2));
    try {
        ImageIO.write((RenderedImage) image, "PNG", outfile);
    } catch (IOException e) {
        e.printStackTrace();
    }
}
Also used : IMolecule(org.openscience.cdk.interfaces.IMolecule) BasicAtomGenerator(org.openscience.cdk.renderer.generators.BasicAtomGenerator) AWTDrawVisitor(org.openscience.cdk.renderer.visitor.AWTDrawVisitor) Rectangle(java.awt.Rectangle) IOException(java.io.IOException) Image(java.awt.Image) BufferedImage(java.awt.image.BufferedImage) RenderedImage(java.awt.image.RenderedImage) BufferedImage(java.awt.image.BufferedImage) Graphics2D(java.awt.Graphics2D)

Aggregations

Graphics2D (java.awt.Graphics2D)2 Image (java.awt.Image)2 Rectangle (java.awt.Rectangle)2 BufferedImage (java.awt.image.BufferedImage)2 RenderedImage (java.awt.image.RenderedImage)2 IOException (java.io.IOException)2 AWTDrawVisitor (org.openscience.cdk.renderer.visitor.AWTDrawVisitor)2 ColoredAtomGenerator (io.imgs.coloration.ColoredAtomGenerator)1 IMolecule (org.openscience.cdk.interfaces.IMolecule)1 BasicAtomGenerator (org.openscience.cdk.renderer.generators.BasicAtomGenerator)1