script.imglib.analysis.ChartUtils.java Source code

Java tutorial

Introduction

Here is the source code for script.imglib.analysis.ChartUtils.java

Source

/*
 * #%L
 * ImgLib: a general-purpose, multidimensional image processing library.
 * %%
 * Copyright (C) 2009 - 2013 Stephan Preibisch, Tobias Pietzsch, Barry DeZonia,
 * Stephan Saalfeld, Albert Cardona, Curtis Rueden, Christian Dietz, Jean-Yves
 * Tinevez, Johannes Schindelin, Lee Kamentsky, Larry Lindsey, Grant Harris,
 * Mark Hiner, Aivar Grislis, Martin Horn, Nick Perry, Michael Zinsmaier,
 * Steffen Jaensch, Jan Funke, Mark Longair, and Dimiter Prodanov.
 * %%
 * This program 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.
 * 
 * This program 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, see
 * <http://www.gnu.org/licenses/gpl-2.0.html>.
 * #L%
 */

package script.imglib.analysis;

import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.awt.image.PixelGrabber;

import mpicbg.imglib.container.array.Array;
import mpicbg.imglib.container.array.ArrayContainerFactory;
import mpicbg.imglib.container.basictypecontainer.IntAccess;
import mpicbg.imglib.container.basictypecontainer.array.IntArray;
import mpicbg.imglib.image.Image;
import mpicbg.imglib.type.numeric.RGBALegacyType;

import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;

/**
 * TODO
 *
 */
public class ChartUtils {

    public static final Image<RGBALegacyType> asImage(final JFreeChart chart) {
        return asImage(chart, -1, -1);
    }

    public static final Image<RGBALegacyType> asImage(final JFreeChart chart, int width, int height) {
        ChartPanel panel = new ChartPanel(chart);
        Dimension d = panel.getPreferredSize();
        if (-1 == width && -1 == height) {
            width = d.width;
            height = d.height;
            panel.setSize(d);
        } else {
            panel.setSize(width, height);
        }
        layoutComponent(panel);
        BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
        Graphics2D g = bi.createGraphics();
        if (!panel.isOpaque()) {
            g.setColor(panel.getBackground());
            g.fillRect(0, 0, width, height);
        }
        panel.paint(g);
        int[] pixels = new int[width * height];
        PixelGrabber pg = new PixelGrabber(bi, 0, 0, width, height, pixels, 0, width);
        try {
            pg.grabPixels();
        } catch (InterruptedException e) {
        }
        g.dispose();

        Array<RGBALegacyType, IntAccess> a = new Array<RGBALegacyType, IntAccess>(new ArrayContainerFactory(),
                new IntArray(pixels), new int[] { width, height }, 1);
        // create a Type that is linked to the container
        final RGBALegacyType linkedType = new RGBALegacyType(a);
        // pass it to the DirectAccessContainer
        a.setLinkedType(linkedType);

        return new Image<RGBALegacyType>(a, new RGBALegacyType());
    }

    private static final void layoutComponent(final Component c) {
        synchronized (c.getTreeLock()) {
            c.doLayout();
            if (c instanceof Container) {
                for (Component child : ((Container) c).getComponents()) {
                    layoutComponent(child);
                }
            }
        }
        c.validate();
    }
}