/*

 * Title: japlot.jaxodraw
 * Description: Graphical user interface for drawing Feynman diagrams
 * @author Daniele Binosi 
 * @author Lukas Theussl  

 Copyright (C) 2003-2006,  Daniele Binosi and Lukas Theussl

 See the file LICENSE in the source distribution home directory
 for a full copy of the GPL (GNU General Public License).

    This file is part of japlot.jaxodraw.

    japlot.jaxodraw is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    japlot.jaxodraw is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA


 */
package japlot.jaxodraw;

import org.freehep.graphics2d.VectorGraphics;
import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.image.BufferedImage;
import java.awt.image.RenderedImage;

import java.io.File;
import java.io.IOException;

import java.util.ResourceBundle;

import javax.imageio.ImageIO;

import javax.swing.JOptionPane;

import jhplot.jadraw.JaObject;
import japlot.Plot;


/** The class that is responsible for exporting to image formats: jpg,
 * or png are currently possible options. Asks for an export file name.
 */
public class JaxoExportImg extends JaxoExport {
    private ResourceBundle language =
        ResourceBundle.getBundle(JaxoPrefs.getPref(JaxoPrefs.PREF_LANGUAGE));
    private Dimension canvasD;
    private String format;

    /** Sets the dimension of the canvas.
     * @param newD The dimension of the canvas.
     */
    public final void setCanvasD(Dimension newD) {
        this.canvasD = newD;
    }

    /** Exports the JaxoGraph graph into an img file:
     * asks for an export file name.
     * @param newGraph The graph to be exported.
     */
    public final void export(JaxoGraph newGraph) {
        setGraph(newGraph);
        if (getMode() == JaxoExportPanel.JPG) {
            format = "jpg";
        } else {
            format = "png";
        }
        checkforLatexText();
        String exportFileName = getExportFileName();
        if (exportFileName.length() != 0) {
            exportImg(exportFileName);
        }
    }

    private void exportImg(String exportFileName) {
        RenderedImage rendImage = createImage();
        try {
            File file = new File(exportFileName);
            ImageIO.write(rendImage, format, file);
        } catch (IOException e) {
            JOptionPane.showMessageDialog(null,
                language.getString("IOException:_Cannot_export_file_")
                + exportFileName + "!");
            if (JaxoPrefs.verbose()) {
                System.err.println(e);
            }
        }
    }

    private RenderedImage createImage() {
        int width = canvasD.width;
        int height = canvasD.height;

        BufferedImage bufferedImage;
        if (format.equals("png")) {
            bufferedImage = getARGBImage(width, height);
        } else {
            bufferedImage = getRGBImage(width, height);
        }

        VectorGraphics g2d = (VectorGraphics)bufferedImage.createGraphics();
        if (format.equals("png")) {
            Color transparent = new Color(0, 0, 0, 0);
            g2d.setColor(transparent);
            g2d.setComposite(AlphaComposite.Src);
        } else {
            g2d.setColor(JaxoCanvas.BCKG_COLOR);
        }
        g2d.fillRect(0, 0, width, height);

        JaxoGraph graph = getGraph();
        int length = graph.listSize();


        // this is first
            for (int i = 0; i < length; i++) {
                JaObject ob = (JaObject) graph.listElementAt(i);
                if (Plot.drawFirst(ob)) ob.jaxoDraw(g2d, false);
            }

            // this is second
            for (int i = 0; i < length; i++) {
                JaObject ob = (JaObject) graph.listElementAt(i);
                Plot.drawRest(ob,g2d,false);
            }

        g2d.dispose();

        double[] bb = graph.getBoundingBox();
        int x = (int) Math.round(bb[JaObject.BB_MIN_X]);
        int y = (int) Math.round(bb[JaObject.BB_MIN_Y]);
        int w = (int) Math.round(bb[JaObject.BB_MAX_X]
            - bb[JaObject.BB_MIN_X]) + 1;
        int h = (int) Math.round(bb[JaObject.BB_MAX_Y]
            - bb[JaObject.BB_MIN_Y]) + 1;

        return bufferedImage.getSubimage(x, y, w, h);
    }

    private BufferedImage getRGBImage(int width, int height) {
        return new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
    }

    private BufferedImage getARGBImage(int width, int height) {
        return new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
    }

}

 

Ads help maintain this website.