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:Composite.java

public void paintComponent(Graphics g) {
    super.paintComponent(g);
    Graphics2D g2 = (Graphics2D) g;

    Dimension d = getSize();//  w w  w .  java2s.c  om
    int w = d.width;
    int h = d.height;

    BufferedImage buffImg = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
    Graphics2D gbi = buffImg.createGraphics();

    g2.setColor(Color.white);
    g2.fillRect(0, 0, d.width, d.height);

    int rectx = w / 4;
    int recty = h / 4;

    gbi.setColor(new Color(0.0f, 0.0f, 1.0f, 1.0f));
    gbi.fill(new Rectangle2D.Double(rectx, recty, 150, 100));
    gbi.setColor(new Color(1.0f, 0.0f, 0.0f, 1.0f));
    gbi.setComposite(ac);
    gbi.fill(new Ellipse2D.Double(rectx + rectx / 2, recty + recty / 2, 150, 100));

    g2.drawImage(buffImg, null, 0, 0);
}

From source file:com.webpagebytes.cms.DefaultImageProcessor.java

public boolean resizeImage(WPBFileStorage cloudStorage, WPBFilePath cloudFile, int desiredSize,
        String outputFormat, OutputStream os) throws WPBException {
    InputStream is = null;//from www.  j a v  a  2 s . co  m
    try {
        //get the file content
        WPBFileInfo fileInfo = cloudStorage.getFileInfo(cloudFile);
        String type = fileInfo.getContentType().toLowerCase();
        if (!type.startsWith("image")) {
            return false;
        }
        is = cloudStorage.getFileContent(cloudFile);
        BufferedImage bufImg = ImageIO.read(is);
        Dimension<Integer> newSize = getResizeSize(bufImg.getWidth(), bufImg.getHeight(), desiredSize);
        BufferedImage bdest = new BufferedImage(newSize.getX(), newSize.getY(), BufferedImage.TYPE_INT_RGB);
        Graphics2D g = bdest.createGraphics();
        Dimension<Double> scale = getResizeScale(bufImg.getHeight(), bufImg.getWidth(), desiredSize);
        AffineTransform at = AffineTransform.getScaleInstance(scale.getX(), scale.getY());
        g.drawRenderedImage(bufImg, at);
        ImageIO.write(bdest, outputFormat, os);
        return true;
    } catch (Exception e) {
        throw new WPBException("Cannot resize image ", e);
    } finally {
        IOUtils.closeQuietly(is);
    }

}

From source file:Main.java

public static BufferedImage toBufferedImage(Image image) {
    if (image instanceof BufferedImage) {
        return (BufferedImage) image;
    }//from  www.  j a  v a  2 s.c o m

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

    // Determine if the image has transparent pixels; for this method's
    // implementation, see Determining If an Image Has Transparent Pixels
    boolean hasAlpha = true;

    // 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) {
            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) {
        // The system does not have a screen
    }

    if (bimage == null) {
        // Create a buffered image using the default color model
        int type = BufferedImage.TYPE_INT_RGB;
        if (hasAlpha) {
            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:org.jfree.chart.demo.ChartTiming3.java

/**
 * Runs the test.//from  w  w  w  .  j  a  va  2  s.c om
 */
public void run() {

    this.finished = false;

    // create a dataset...
    final XYSeries series = new XYSeries("Random Data");
    for (int i = 0; i < 1440; i++) {
        final double x = Math.random();
        final double y = Math.random();
        series.add(x, y);
    }
    final XYDataset data = new XYSeriesCollection(series);

    // 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:dk.netdesign.common.osgi.config.osgi.OCD.java

public BufferedImage scaleImage(BufferedImage img, int width, int height, Color background) {
    int imgWidth = img.getWidth();
    int imgHeight = img.getHeight();
    if (imgWidth * height < imgHeight * width) {
        width = imgWidth * height / imgHeight;
    } else {/*from w  w w  .  java2s  . com*/
        height = imgHeight * width / imgWidth;
    }
    BufferedImage newImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
    Graphics2D g = newImage.createGraphics();
    try {
        g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);
        g.setBackground(background);
        g.clearRect(0, 0, width, height);
        g.drawImage(img, 0, 0, width, height, null);
    } finally {
        g.dispose();
    }
    return newImage;
}

From source file:com.tdclighthouse.prototype.servlets.JLatexServlet.java

private synchronized BufferedImage generateImage(String latex) {
    TeXFormula formula = new TeXFormula(latex);
    TeXIcon icon = formula.createTeXIcon(TeXConstants.STYLE_DISPLAY, 20);
    icon.setInsets(new Insets(5, 5, 5, 5));

    BufferedImage image = new BufferedImage(icon.getIconWidth(), icon.getIconHeight(),
            BufferedImage.TYPE_INT_ARGB);
    Graphics2D g2 = image.createGraphics();
    g2.setColor(Color.white);/*from  www  .j a v  a  2 s  .  co m*/
    g2.fillRect(0, 0, icon.getIconWidth(), icon.getIconHeight());
    JLabel jl = new JLabel();
    jl.setForeground(new Color(0, 0, 0));
    icon.paintIcon(jl, g2, 0, 0);
    return image;
}

From source file:probe.com.model.util.vaadintoimageutil.HeatmapSwingComponent.java

public String generateHeatmap(String[] rows, String[] columns, String[][] data) {

    JPanel heatmapPanelLayout = new JPanel();
    heatmapPanelLayout.setLayout(null);/* www .  jav a2  s  .  c  o  m*/
    heatmapPanelLayout.setVisible(true);
    heatmapPanelLayout.setBorder(new LineBorder(Color.BLACK));
    int width = (columns.length + 1) * 50;
    int height = (rows.length + 1) * 50;
    heatmapPanelLayout.setSize(width, height);
    JPanel cornerCell = initCell("#ffffff", 0, 0);
    int x = 50;
    int y = 0;
    heatmapPanelLayout.add(cornerCell);

    for (String headerCell : columns) {
        JPanel cell = initCell(headerCell, x, y);
        x += 50;
        heatmapPanelLayout.add(cell);

    }
    y = 50;
    for (String headerCell : rows) {
        JPanel cell = initCell(headerCell, 0, y);
        y += 50;
        heatmapPanelLayout.add(cell);

    }
    x = 50;
    y = 50;
    for (String[] row : data) {
        for (String color : row) {
            JPanel cell = initCell(color, x, y);
            heatmapPanelLayout.add(cell);
            x += 50;
        }
        x = 50;
        y += 50;
    }

    BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
    Graphics2D graphics = image.createGraphics();
    graphics.setPaint(Color.WHITE);
    heatmapPanelLayout.paint(graphics);
    //        super.paint(graphics);
    byte[] imageData = null;

    try {

        ImageEncoder in = ImageEncoderFactory.newInstance(ImageFormat.PNG, new Float(0.084666f));
        imageData = in.encode(image);
    } catch (Exception e) {
        System.out.println(e.getLocalizedMessage());
    }

    String base64 = Base64.encodeBytes(imageData);
    base64 = "data:image/png;base64," + base64;
    return base64;
    //
    //        JFrame frame = new JFrame();
    //        frame.setSize(1000, 1000);
    //        frame.add(heatmapPanelLayout);
    //        frame.setVisible(true);

    //        return "";
}

From source file:MainClass.java

private BufferedImage createImage() {
    BufferedImage bim;

    Color[] colors = { Color.red, Color.blue, Color.yellow, };

    int width = 8, height = 8;
    bim = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
    Graphics2D g2 = bim.createGraphics();
    for (int i = 0; i < width; i++) {
        g2.setPaint(colors[(i / 2) % colors.length]);
        g2.drawLine(0, i, i, 0);/*from w w w . j  ava  2  s. c  o m*/
        g2.drawLine(width - i, height, width, height - i);
    }
    return bim;
}

From source file:com.scramcode.djinn.ui.panels.GraphPanel.java

/**
 * copy the visible part of the graph to a file as a jpeg image
 * @param file// ww  w. jav  a 2  s. c  om
 */
public void writeJPEGImage(File file) {
    int width = visualizationViewer.getWidth();
    int height = visualizationViewer.getHeight();

    BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
    Graphics2D graphics = bi.createGraphics();
    visualizationViewer.paint(graphics);
    graphics.dispose();

    try {
        ImageIO.write(bi, "jpeg", file);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:jadx.core.utils.android.Res9patchStreamDecoder.java

public void decode(InputStream in, OutputStream out) throws JadxException {
    try {//  w w w  .j  a va 2s  . c  o m
        byte[] data = IOUtils.toByteArray(in);

        BufferedImage im = ImageIO.read(new ByteArrayInputStream(data));
        int w = im.getWidth(), h = im.getHeight();

        BufferedImage im2 = new BufferedImage(w + 2, h + 2, BufferedImage.TYPE_INT_ARGB);
        im2.createGraphics().drawImage(im, 1, 1, w, h, null);

        NinePatch np = getNinePatch(data);
        drawHLine(im2, h + 1, np.padLeft + 1, w - np.padRight);
        drawVLine(im2, w + 1, np.padTop + 1, h - np.padBottom);

        int[] xDivs = np.xDivs;
        for (int i = 0; i < xDivs.length; i += 2) {
            drawHLine(im2, 0, xDivs[i] + 1, xDivs[i + 1]);
        }

        int[] yDivs = np.yDivs;
        for (int i = 0; i < yDivs.length; i += 2) {
            drawVLine(im2, 0, yDivs[i] + 1, yDivs[i + 1]);
        }

        ImageIO.write(im2, "png", out);
    } catch (IOException | NullPointerException ex) {
        throw new JadxException(ex.toString());
    }
}