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:com.fusesource.forge.jmstest.persistence.rrd.RrdGraphPostProcessor.java

private void createThumbnail(BufferedImage image, String name, int thumbWidth, int thumbHeight) {

    double thumbRatio = (double) thumbWidth / (double) thumbHeight;
    int imageWidth = image.getWidth(null);
    int imageHeight = image.getHeight(null);
    double imageRatio = (double) imageWidth / (double) imageHeight;

    if (thumbRatio < imageRatio) {
        thumbHeight = (int) (thumbWidth / imageRatio);
    } else {//from   ww w .j a v  a  2  s  .c o  m
        thumbWidth = (int) (thumbHeight * imageRatio);
    }

    BufferedImage thumbImage = new BufferedImage(thumbWidth,

            thumbHeight, BufferedImage.TYPE_INT_RGB);
    Graphics2D graphics2D = thumbImage.createGraphics();
    graphics2D.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
    graphics2D.drawImage(image, 0, 0, thumbWidth, thumbHeight, null);

    File thumbFile = new File(getWorkDir().getAbsolutePath() + "/" + name + "-thumb.png");

    try {
        ImageIO.write(thumbImage, "PNG", thumbFile);
    } catch (IOException ioe) {
        log().error("Error creating thumbnail for: " + name);
    }
}

From source file:net.sf.maltcms.chromaui.project.spi.nodes.DescriptorNode.java

@Override
public Image getIcon(int type) {
    Image descrImage = DescriptorFactory.getImage(getBean());
    int w = descrImage.getWidth(null);
    int h = descrImage.getHeight(null);
    if (getBean() instanceof IColorizableDescriptor) {
        IColorizableDescriptor colorDescr = (IColorizableDescriptor) getBean();
        Color c = colorDescr.getColor();
        if (c != null) {
            BufferedImage bi = new BufferedImage(w / 10, h / 10, BufferedImage.TYPE_INT_ARGB);
            Graphics2D g2 = bi.createGraphics();

            g2.setColor(colorDescr.getColor());
            g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.9f));
            g2.fillRect(0, 0, bi.getWidth(), bi.getHeight());
            descrImage = ImageUtilities.mergeImages(bi, descrImage, w - bi.getWidth(), h - bi.getHeight());
        }//from   w w  w .ja v  a 2  s  .  co m
    }
    return descrImage;
}

From source file:org.n52.server.io.DiagramGenerator.java

/**
 * Creates a time series chart diagram and writes it to the OutputStream.
 *///from   ww  w . j  a v a  2s.co  m
public void producePresentation(Map<String, OXFFeatureCollection> entireCollMap, DesignOptions options,
        FileOutputStream out, boolean compress) throws OXFException, IOException {

    // render features:
    int width = options.getWidth();
    int height = options.getHeight();
    Calendar begin = Calendar.getInstance();
    begin.setTimeInMillis(options.getBegin());
    Calendar end = Calendar.getInstance();
    end.setTimeInMillis(options.getEnd());

    DiagramRenderer renderer = new DiagramRenderer(false);

    JFreeChart diagramChart = renderer.renderChart(entireCollMap, options, begin, end, compress);
    diagramChart.removeLegend();

    // draw chart into image:
    BufferedImage diagramImage = new BufferedImage(width, height, TYPE_INT_RGB);
    Graphics2D chartGraphics = diagramImage.createGraphics();
    chartGraphics.setColor(Color.white);
    chartGraphics.fillRect(0, 0, width, height);

    diagramChart.draw(chartGraphics, new Rectangle2D.Float(0, 0, width, height));

    JPEGImageWriteParam p = new JPEGImageWriteParam(null);
    p.setCompressionMode(JPEGImageWriteParam.MODE_DEFAULT);
    write(diagramImage, FORMAT, out);
}

From source file:com.krawler.esp.handlers.FileUploadHandler.java

private BufferedImage toBufferedImage(Image image, int type) {
    image = new ImageIcon(image).getImage();
    BufferedImage bufferedImage = new BufferedImage(image.getWidth(null), image.getHeight(null), type);
    Graphics g = bufferedImage.createGraphics();
    if (type == BufferedImage.TYPE_INT_RGB) {
        g.setColor(Color.white);// w  w  w. ja  v  a  2  s  . c  o m
        g.fillRect(0, 0, image.getWidth(null), image.getHeight(null));
    }
    g.drawImage(image, 0, 0, null);
    g.dispose();

    return bufferedImage;
}

From source file:CompositeTest.java

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

    BufferedImage image = new BufferedImage(getWidth(), getHeight(), BufferedImage.TYPE_INT_ARGB);
    Graphics2D gImage = image.createGraphics();
    gImage.setPaint(Color.red);//from w ww. j av a  2s.  com
    gImage.fill(shape1);
    AlphaComposite composite = AlphaComposite.getInstance(rule, alpha);
    gImage.setComposite(composite);
    gImage.setPaint(Color.blue);
    gImage.fill(shape2);
    g2.drawImage(image, null, 0, 0);
}

From source file:image.text.CreateTifAndAnnotate.java

public void createAnnotatedTif(String[] args, OutputStream out) throws Exception {

    byte[] byteArray = new byte[] { -1, 0 };
    ColorModel colorModel = new IndexColorModel(1, 2, byteArray, byteArray, byteArray);

    WritableRaster writeableRaster = Raster.createPackedRaster(DataBuffer.TYPE_BYTE, 1700, 2200, 1, 1, null);
    BufferedImage bufImg = new BufferedImage(colorModel, writeableRaster, false, null);

    // -------------------------------------------------------------------        
    Graphics2D g2d = bufImg.createGraphics();
    g2d.setColor(Color.black);//from   w  ww  . jav  a  2  s .co m

    Font font = new Font("Arial Bold", Font.PLAIN, 36);
    g2d.setFont(font);
    int vertPos = 200;
    for (int i = 0; i < args.length; i++) {
        g2d.drawString(args[i], 75, vertPos);
        vertPos += 48;
    }

    PlanarImage planarImage = PlanarImage.wrapRenderedImage(bufImg);

    TIFFEncodeParam encodeParam = new TIFFEncodeParam();
    encodeParam.setCompression(TIFFEncodeParam.COMPRESSION_GROUP4);
    encodeParam.setWriteTiled(false); // false means use strips.
    encodeParam.setTileSize(0, 0); // tiling will make the file size much larger. 
    encodeParam.setLittleEndian(true); // create an Intel (II) format image

    String SoftwareVersion[] = new String[] { "TIFF by Joe, " + this.getClass().getName() };
    String docName[] = new String[] { "JoesTifAnnotator" };

    // Create a new TIFF fields including a new TIFF ASCII TIFF tag.
    TIFFField tiffFields[] = new TIFFField[5];

    tiffFields[0] = new TIFFField(269, TIFFField.TIFF_ASCII, docName.length, docName);
    tiffFields[1] = new TIFFField(282, TIFFField.TIFF_RATIONAL, 1, new long[][] { { 200, 1 } });
    tiffFields[2] = new TIFFField(283, TIFFField.TIFF_RATIONAL, 1, new long[][] { { 200, 1 } });
    // resolution unit 
    tiffFields[3] = new TIFFField(296, TIFFField.TIFF_SHORT, 1, new char[] { 2 });
    tiffFields[4] = new TIFFField(305, TIFFField.TIFF_ASCII, SoftwareVersion.length, SoftwareVersion);

    encodeParam.setExtraFields(tiffFields);

    TIFFImageEncoder encoder = new TIFFImageEncoder(out, encodeParam);
    encoder.encode(planarImage);
}

From source file:eu.novait.imagerenamer.model.ImageFile.java

private void createThumbnail() {
    try {/*from   w  w w. ja  v a 2  s. com*/
        BufferedImage tmp = ImageIO.read(this.filepath);
        int tmpW = tmp.getWidth();
        int tmpH = tmp.getHeight();
        double ratio = (double) tmpW / (double) tmpH;
        int w = 400;
        int h = (int) Math.round(w / ratio);
        BufferedImage bi = getCompatibleImage(w, h);
        Graphics2D g2d = bi.createGraphics();
        double xScale = (double) w / tmp.getWidth();
        double yScale = (double) h / tmp.getHeight();
        AffineTransform at = AffineTransform.getScaleInstance(xScale, yScale);
        g2d.drawRenderedImage(tmp, at);
        g2d.dispose();
        this.setThumbnail(bi);
    } catch (IOException ex) {
        Logger.getLogger(ImageFile.class.getName()).log(Level.SEVERE, null, ex);
    }
}

From source file:org.n52.oxf.render.sos.ScatterPlotChartRenderer.java

/**
 * The resulting IVisualization shows a scatterplot.
 * //from  w  ww.  j a v  a  2  s .co m
 * Auf X- und Y-Achse wird jeweils eine observedProperty abgetragen.
 * 
 * Ein Punkt steht dann fr ein Wertepaar (X,Y) fr ein FOI zu einem bestimmten Zeitpunkt.
 * 
 * TODO PROBLEM: Macht nur Sinn, wenn fr jedes (FOI, Time)-Tuple jeweils ein Wert fr BEIDE
 * observedPropertys enthalten ist !!!!
 */
public IVisualization renderChart(OXFFeatureCollection observationCollection, ParameterContainer paramCon,
        int width, int height) {
    JFreeChart chart = renderChart(observationCollection, paramCon);

    Plot plot = chart.getPlot();

    // draw plot into image:

    BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);

    Graphics2D g = image.createGraphics();

    plot.draw(g, new Rectangle2D.Float(0, 0, width, height), null, null, null);

    return new StaticVisualization(image);
}

From source file:ScreenCapture.java

public boolean crop() {
    if (startPoint.equals(endPoint))
        return true;

    boolean succeeded = true;

    int x1 = (startPoint.x < endPoint.x) ? startPoint.x : endPoint.x;
    int y1 = (startPoint.y < endPoint.y) ? startPoint.y : endPoint.y;

    int x2 = (startPoint.x > endPoint.x) ? startPoint.x : endPoint.x;
    int y2 = (startPoint.y > endPoint.y) ? startPoint.y : endPoint.y;

    int width = (x2 - x1) + 1;
    int height = (y2 - y1) + 1;

    BufferedImage biCrop = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
    Graphics2D g2d = biCrop.createGraphics();
    BufferedImage bi = (BufferedImage) image;
    BufferedImage bi2 = bi.getSubimage(x1, y1, width, height);
    g2d.drawImage(bi2, null, 0, 0);/*from w  w w.j  a  v a  2  s .c  o m*/

    g2d.dispose();

    if (succeeded)
        setImage(biCrop);
    else {
        startPoint.x = endPoint.x;
        startPoint.y = endPoint.y;
        repaint();
    }

    return succeeded;
}

From source file:de.jwic.ecolib.controls.chart.ChartControl.java

public void renderImage() throws IOException {
    // create image to draw into
    BufferedImage bi = new BufferedImage(width < 10 ? 10 : width, height < 10 ? 10 : height,
            BufferedImage.TYPE_INT_ARGB);
    Graphics2D g2d = bi.createGraphics();

    if (chart != null) {
        chart.setBackgroundPaint(Color.WHITE);
        chart.draw(g2d, new Rectangle2D.Double(0, 0, width < 10 ? 10 : width, height < 10 ? 10 : height));
    } else {//from  w w w  .j a va 2 s  . c o m
        g2d.setColor(Color.BLACK);
        g2d.drawString("No chart has been assigned.", 1, 20);
    }
    // finish drawing
    g2d.dispose();

    // write image data into output stream
    ByteArrayOutputStream out = getImageOutputStream();
    out.reset();
    // create a PNG image
    ImageWriter imageWriter = new PNGImageWriter(null);
    ImageWriteParam param = imageWriter.getDefaultWriteParam();
    imageWriter.setOutput(new MemoryCacheImageOutputStream(out));
    imageWriter.write(null, new IIOImage(bi, null, null), param);
    imageWriter.dispose();

    setMimeType(MIME_TYPE_PNG);
}