Example usage for java.awt.image BufferedImage createGraphics

List of usage examples for java.awt.image BufferedImage createGraphics

Introduction

In this page you can find the example usage for java.awt.image BufferedImage createGraphics.

Prototype

public Graphics2D createGraphics() 

Source Link

Document

Creates a Graphics2D , which can be used to draw into this BufferedImage .

Usage

From source file:org.jfree.chart.demo.ChartTiming2.java

/**
 * Runs the test./*from   w ww  . j  a va2 s .co m*/
 */
public void run() {

    this.finished = false;

    // create a dataset...
    final XYDataset data = new SampleXYDataset2(1, 1440);

    // create a scatter chart...
    final boolean withLegend = true;
    final JFreeChart chart = ChartFactory.createScatterPlot("Scatter plot timing", "X", "Y", data,
            PlotOrientation.VERTICAL, withLegend, false, false);

    final XYPlot plot = chart.getXYPlot();
    plot.setRenderer(new XYDotRenderer());

    final BufferedImage image = new BufferedImage(400, 300, BufferedImage.TYPE_INT_RGB);
    final Graphics2D g2 = image.createGraphics();
    final Rectangle2D chartArea = new Rectangle2D.Double(0, 0, 400, 300);

    // set up the timer...
    final Timer timer = new Timer(10000, this);
    timer.setRepeats(false);
    int count = 0;
    timer.start();
    while (!this.finished) {
        chart.draw(g2, chartArea, null, null);
        System.out.println("Charts drawn..." + count);
        if (!this.finished) {
            count++;
        }
    }
    System.out.println("DONE");

}

From source file:org.primefaces.examples.view.DynamicImageController.java

public DynamicImageController() {
    try {//from w w  w. j  a  v a 2 s .com
        //Graphic Text
        BufferedImage bufferedImg = new BufferedImage(100, 25, BufferedImage.TYPE_INT_RGB);
        Graphics2D g2 = bufferedImg.createGraphics();
        g2.drawString("This is a text", 0, 10);
        ByteArrayOutputStream os = new ByteArrayOutputStream();
        ImageIO.write(bufferedImg, "png", os);
        graphicText = new DefaultStreamedContent(new ByteArrayInputStream(os.toByteArray()), "image/png");

        //Chart
        JFreeChart jfreechart = ChartFactory.createPieChart("Turkish Cities", createDataset(), true, true,
                false);
        File chartFile = new File("dynamichart");
        ChartUtilities.saveChartAsPNG(chartFile, jfreechart, 375, 300);
        chart = new DefaultStreamedContent(new FileInputStream(chartFile), "image/png");

        //Barcode
        File barcodeFile = new File("dynamicbarcode");
        BarcodeImageHandler.saveJPEG(BarcodeFactory.createCode128("PRIMEFACES"), barcodeFile);
        barcode = new DefaultStreamedContent(new FileInputStream(barcodeFile), "image/jpeg");
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:peakml.util.swt.widget.IntensityTrendGraph.java

public BufferedImage getGraphImage(int width, int height) {
    BufferedImage img = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
    Graphics2D g = img.createGraphics();
    g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

    linechart.draw(g, new Rectangle(0, 0, width, height));

    return img;//  w ww.jav a2s.c o  m
}

From source file:com.wet.wired.jsr.recorder.DesktopScreenRecorder.java

public BufferedImage captureScreen(Rectangle recordArea) {
    Point mousePosition = MouseInfo.getPointerInfo().getLocation();
    BufferedImage image = robot.createScreenCapture(recordArea);

    Graphics2D grfx = image.createGraphics();

    grfx.drawImage(mouseCursor, mousePosition.x - 8, mousePosition.y - 5, null);

    grfx.dispose();/*w  w  w.j a v a  2 s  .c  om*/

    return image;
}

From source file:org.jfree.chart.demo.ChartTiming1.java

/**
 * Runs the timing./*  www .j a  va 2s.  c  om*/
 */
public void run() {
    this.finished = false;

    // create a dataset...
    final DefaultPieDataset data = new DefaultPieDataset();
    data.setValue("One", new Double(10.3));
    data.setValue("Two", new Double(8.5));
    data.setValue("Three", new Double(3.9));
    data.setValue("Four", new Double(3.9));
    data.setValue("Five", new Double(3.9));
    data.setValue("Six", new Double(3.9));

    // create a pie chart...
    final boolean withLegend = true;
    final JFreeChart chart = ChartFactory.createPieChart("Testing", data, withLegend, true, false);

    final BufferedImage image = new BufferedImage(400, 300, BufferedImage.TYPE_INT_RGB);
    final Graphics2D g2 = image.createGraphics();
    final Rectangle2D chartArea = new Rectangle2D.Double(0, 0, 400, 300);

    // set up the timer...
    final Timer timer = new Timer(10000, this);
    timer.setRepeats(false);
    int count = 0;
    timer.start();
    while (!this.finished) {
        chart.draw(g2, chartArea, null, null);
        System.out.println("Charts drawn..." + count);
        if (!this.finished) {
            count++;
        }
    }
    System.out.println("DONE");

}

From source file:ImageUtil.java

/**
 * Posted by alpha02 at http://www.dreamincode.net/code/snippet1076.htm
 *///w w w .  j a  v  a  2  s.  com
public static BufferedImage toBufferedImage(Image image) {
    if (image instanceof BufferedImage)
        return (BufferedImage) image;

    // This code ensures that all the pixels in the image are loaded
    image = new ImageIcon(image).getImage();

    // Determine if the image has transparent pixels
    boolean hasAlpha = hasAlpha(image);

    // Create a buffered image with a format that's compatible with the screen
    BufferedImage bimage = null;

    GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();

    try {
        // Determine the type of transparency of the new buffered image
        int transparency = Transparency.OPAQUE;

        if (hasAlpha == true)
            transparency = Transparency.BITMASK;

        // Create the buffered image
        GraphicsDevice gs = ge.getDefaultScreenDevice();
        GraphicsConfiguration gc = gs.getDefaultConfiguration();

        bimage = gc.createCompatibleImage(image.getWidth(null), image.getHeight(null), transparency);
    } catch (HeadlessException e) {
    } //No screen

    if (bimage == null) {
        // Create a buffered image using the default color model
        int type = BufferedImage.TYPE_INT_RGB;

        if (hasAlpha == true) {
            type = BufferedImage.TYPE_INT_ARGB;
        }
        bimage = new BufferedImage(image.getWidth(null), image.getHeight(null), type);
    }

    // Copy image to buffered image
    Graphics g = bimage.createGraphics();

    // Paint the image onto the buffered image
    g.drawImage(image, 0, 0, null);
    g.dispose();

    return bimage;
}

From source file:ImageBorderHack.java

public BufferedImage createBufferedImage(Image img) {
    BufferedImage buff = new BufferedImage(img.getWidth(null), img.getHeight(null),
            BufferedImage.TYPE_INT_ARGB);
    Graphics gfx = buff.createGraphics();
    gfx.drawImage(img, 0, 0, null);//from  w w  w  . j  av  a 2  s. c  o m
    gfx.dispose();
    return buff;
}

From source file:org.jfree.chart.demo.ChartTiming4.java

/**
 * Runs the test.//from  www .  j a v a2 s . c  o  m
 */
public void run() {

    this.finished = false;

    // create a dataset...
    populateData();

    // create a fast scatter chart...
    final Plot plot = new FastScatterPlot(this.data, new NumberAxis("X"), new NumberAxis("Y"));
    final JFreeChart chart = new JFreeChart("Fast Scatter Plot Timing", JFreeChart.DEFAULT_TITLE_FONT, plot,
            true);

    final BufferedImage image = new BufferedImage(400, 300, BufferedImage.TYPE_INT_RGB);
    final Graphics2D g2 = image.createGraphics();
    final Rectangle2D chartArea = new Rectangle2D.Double(0, 0, 400, 300);

    // set up the timer...
    final Timer timer = new Timer(10000, this);
    timer.setRepeats(false);
    int count = 0;
    timer.start();
    while (!this.finished) {
        chart.draw(g2, chartArea, null, null);
        System.out.println("Charts drawn..." + count);
        if (!this.finished) {
            count++;
        }
    }
    System.out.println("DONE");

}

From source file:maltcms.ui.fileHandles.serialized.SeriesPaintComboBoxRenderer.java

private BufferedImage createColorImage(Paint p) {
    BufferedImage bi = new BufferedImage(10, 10, BufferedImage.TYPE_INT_RGB);
    Graphics2D g2 = bi.createGraphics();
    g2.setPaint(p);//from  www.  j a  v a2 s.  c om
    g2.fillRect(0, 0, bi.getWidth(), bi.getHeight());
    return bi;
}