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:edu.csun.ecs.cs.multitouchj.application.whiteboard.ui.InteractiveCanvas.java

public void clearCanvas(Color color) {
    synchronized (resizableBufferedImage) {
        BufferedImage bufferedImage = resizableBufferedImage.getBufferedImage();

        Graphics2D graphics = bufferedImage.createGraphics();
        graphics.setPaint(color);/*  ww w  . j av a  2  s . c  om*/
        graphics.fillRect(0, 0, bufferedImage.getWidth(), bufferedImage.getHeight());
        graphics.dispose();

        setDirty(true);
    }
}

From source file:org.n52.server.sos.generator.Generator.java

protected String createAndSaveImage(DesignOptions options, JFreeChart chart, ChartRenderingInfo renderingInfo)
        throws Exception {
    int width = options.getWidth();
    int height = options.getHeight();
    BufferedImage image = chart.createBufferedImage(width, height, renderingInfo);
    Graphics2D chartGraphics = image.createGraphics();
    chartGraphics.setColor(Color.white);
    chartGraphics.fillRect(0, 0, width, height);
    chart.draw(chartGraphics, new Rectangle2D.Float(0, 0, width, height));

    try {//from w  ww  .j  av a 2s  . c  o  m
        return ServletUtilities.saveChartAsPNG(chart, width, height, renderingInfo, null);
    } catch (IOException e) {
        throw new Exception("Could not save PNG!", e);
    }
}

From source file:org.n52.server.sos.generator.DiagramGenerator.java

/**
 * Produce legend.//w w  w. j av a 2 s  .  c  o m
 * 
 * @param options
 *            the options
 * @param out
 *            the out
 * @throws OXFException
 *             the oXF exception
 */
public void createLegend(DesignOptions options, OutputStream out) throws OXFException, IOException {

    int topMargin = 10;
    int leftMargin = 30;
    int iconWidth = 15;
    int iconHeight = 15;
    int verticalSpaceBetweenEntries = 20;
    int horizontalSpaceBetweenIconAndText = 15;

    DesignDescriptionList ddList = buildUpDesignDescriptionList(options);
    int width = 800;
    int height = topMargin + (ddList.size() * (iconHeight + verticalSpaceBetweenEntries));

    BufferedImage legendImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
    Graphics2D legendGraphics = legendImage.createGraphics();
    legendGraphics.setColor(Color.white);
    legendGraphics.fillRect(0, 0, width, height);

    int offset = 0;
    for (RenderingDesign dd : ddList.getAllDesigns()) {
        int yPos = topMargin + offset * iconHeight + offset * verticalSpaceBetweenEntries;

        // icon:
        legendGraphics.setColor(dd.getColor());
        legendGraphics.fillRect(leftMargin, yPos, iconWidth, iconHeight);

        // text:
        legendGraphics.setColor(Color.black);

        legendGraphics.drawString(dd.getFeature().getLabel() + " - " + dd.getLabel(),
                leftMargin + iconWidth + horizontalSpaceBetweenIconAndText, yPos + iconHeight);

        offset++;
    }

    // draw legend into image:
    JPEGEncodeParam p = new JPEGEncodeParam();
    p.setQuality(1f);
    ImageEncoder encoder = ImageCodec.createImageEncoder("jpeg", out, p);

    encoder.encode(legendImage);
}

From source file:mod.rankshank.arbitraria.client.texture.TextureTiled.java

@Override
public void loadTexture(IResourceManager resourceManager) throws IOException {
    this.deleteGlTexture();
    IResource iresource = null;//from ww w.  ja va  2  s. co m
    try {
        iresource = resourceManager.getResource(tileSource);
        BufferedImage img = TextureUtil.readBufferedImage(iresource.getInputStream());
        boolean blur = false;
        boolean clamp = false;

        if (iresource.hasMetadata()) {
            try {
                TextureMetadataSection texturemetadatasection = iresource.getMetadata("texture");

                if (texturemetadatasection != null) {
                    blur = texturemetadatasection.getTextureBlur();
                    clamp = texturemetadatasection.getTextureClamp();
                }
            } catch (RuntimeException runtimeexception) {
                Arbitraria.error(new Throwable(
                        String.format("Issue loading tiled texture for %s, aborting", tileSource.toString()),
                        runtimeexception));
            }
        }

        int width = img.getWidth();
        int height = img.getHeight();
        if (width != height) {
            Arbitraria.error(new Throwable(String.format("Tiled image failed for %s [Width = %s, Height = %s",
                    tileSource, width, height)));
            return;
        }
        int factor = targetTile / width;

        BufferedImage finalImg = new BufferedImage(width * factor, height * factor, img.getType());

        int num = 0;
        for (int i = 0; i < factor; i++) {
            for (int j = 0; j < factor; j++) {
                finalImg.createGraphics().drawImage(img, width * j, height * i, null);
                num++;
            }
        }
        TextureUtil.uploadTextureImageAllocate(this.getGlTextureId(), finalImg, blur, clamp);
    } finally {
        IOUtils.closeQuietly(iresource);
    }
}

From source file:hudson.jbpm.PluginImpl.java

/**
 * Draws a JPEG for a process definition
 * //from w  w  w .j a va 2 s. c  om
 * @param req
 * @param rsp
 * @param processDefinition
 * @throws IOException
 */
public void doProcessDefinitionImage(StaplerRequest req, StaplerResponse rsp,
        @QueryParameter("processDefinition") long processDefinition) throws IOException {

    JbpmContext context = getCurrentJbpmContext();
    ProcessDefinition definition = context.getGraphSession().getProcessDefinition(processDefinition);
    FileDefinition fd = definition.getFileDefinition();
    byte[] bytes = fd.getBytes("processimage.jpg");
    rsp.setContentType("image/jpeg");
    ServletOutputStream output = rsp.getOutputStream();

    BufferedImage loaded = ImageIO.read(new ByteArrayInputStream(bytes));
    BufferedImage aimg = new BufferedImage(loaded.getWidth(), loaded.getHeight(), BufferedImage.TYPE_INT_RGB);
    Graphics2D g = aimg.createGraphics();
    g.drawImage(loaded, null, 0, 0);
    g.dispose();
    ImageIO.write(aimg, "jpg", output);
    output.flush();
    output.close();
}

From source file:br.ufrgs.enq.jcosmo.ui.SigmaProfileAreaPanel.java

public void addProfile(String label, double[] sigma, double[] area, int rgb) {
    int n = sigma.length;
    XYSeries comp = new XYSeries(label, false, false);

    // charges represent the center of the segments
    comp.add(sigma[0], area[0]);//from  ww  w  . j  a v  a2s  .c o m
    for (int j = 1; j < n; ++j) {
        double dsigma = (sigma[j] - sigma[j - 1]) / 2;
        comp.add(sigma[j] - dsigma, area[j]);
        comp.add(sigma[j] + dsigma + 1e-6, area[j]);
    }
    dataset.addSeries(comp);
    int series = dataset.getSeriesCount() - 1;

    int patternSize = 6;
    BufferedImage bi = new BufferedImage(patternSize, patternSize, BufferedImage.TYPE_INT_RGB);
    Rectangle r = new Rectangle(0, 0, patternSize, patternSize);
    Graphics2D big = bi.createGraphics();
    big.setColor(new Color(rgb, rgb, rgb));
    big.fillRect(0, 0, patternSize, patternSize);

    int pattern = series % 4;
    if (pattern == 1 || pattern == 3) {
        big.setColor(Color.WHITE);
        big.drawLine(0, patternSize / 2, patternSize, patternSize / 2);
    }
    if (pattern == 2 || pattern == 3) {
        big.setColor(Color.WHITE);
        big.drawLine(patternSize / 2, 0, patternSize / 2, patternSize);
    }

    TexturePaint paint = new TexturePaint(bi, r);
    sigmaProfilePlot.getRenderer().setSeriesPaint(series, paint); // new Color(rgb, rgb, rgb));
}

From source file:net.sf.jasperreports.charts.util.ImageChartRendererFactory.java

@Override
public Renderable getRenderable(JasperReportsContext jasperReportsContext, JFreeChart chart,
        ChartHyperlinkProvider chartHyperlinkProvider, Rectangle2D rectangle) {
    int dpi = JRPropertiesUtil.getInstance(jasperReportsContext)
            .getIntegerProperty(Renderable.PROPERTY_IMAGE_DPI, 72);
    double scale = dpi / 72d;

    BufferedImage bi = new BufferedImage((int) (scale * (int) rectangle.getWidth()),
            (int) (scale * rectangle.getHeight()), BufferedImage.TYPE_INT_ARGB);

    List<JRPrintImageAreaHyperlink> areaHyperlinks = null;

    Graphics2D grx = bi.createGraphics();
    try {/*ww  w  . j a  v a  2s  .c o m*/
        grx.scale(scale, scale);

        if (chartHyperlinkProvider != null && chartHyperlinkProvider.hasHyperlinks()) {
            areaHyperlinks = ChartUtil.getImageAreaHyperlinks(chart, chartHyperlinkProvider, grx, rectangle);
        } else {
            chart.draw(grx, rectangle);
        }
    } finally {
        grx.dispose();
    }

    try {
        return new SimpleDataRenderer(
                JRImageLoader.getInstance(jasperReportsContext).loadBytesFromAwtImage(bi, ImageTypeEnum.PNG),
                areaHyperlinks);
    } catch (JRException e) {
        throw new JRRuntimeException(e);
    }
}

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

/**
 * //from   w  ww.  j  ava  2  s.  c om
 * @param observationCollection
 *        the observations belonging to the timeStamp (frame) which shall be rendered
 * @param screenW
 * @param screenH
 * @param bbox
 * @return
 */
protected Image renderFrame(ITimePosition timePos, int screenW, int screenH, IBoundingBox bbox,
        Set<OXFFeature> featuresWithCharts, ObservationSeriesCollection tupleFinder) {
    ContextBoundingBox contextBBox = new ContextBoundingBox(bbox);

    BufferedImage image = new BufferedImage(screenW, screenH, BufferedImage.TYPE_INT_RGB);

    Graphics2D g = image.createGraphics();

    // draw white background:
    g.setColor(Color.WHITE);
    g.fillRect(0, 0, screenW, screenH);

    for (OXFFeature chartFeature : featuresWithCharts) {

        //
        // create Plot for each "chart feature" and add it to the plotArray:
        //
        CategoryPlot plot = renderFoiBarChart(chartFeature.getID(), timePos.toString(),
                tupleFinder.getTuple(chartFeature, timePos));

        //
        // draw the plots into the image at the georeferenced position of the corresponding chartFeature:
        //
        Point pRealWorld = (Point) chartFeature.getGeometry();

        java.awt.Point pScreen = ContextBoundingBox.realworld2Screen(contextBBox.getActualBBox(), screenW,
                screenH, new Point2D.Double(pRealWorld.getCoordinate().x, pRealWorld.getCoordinate().y));

        plot.getRangeAxis(0).setRange((Double) tupleFinder.getMinimum(0), (Double) tupleFinder.getMaximum(0));

        plot.draw(g,
                new Rectangle2D.Float(pScreen.x + X_SHIFT + 10, pScreen.y + Y_SHIFT, CHART_WIDTH, CHART_HEIGHT),
                null, null, null);
    }

    return image;
}

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

protected String createAndSaveImage(DesignOptions options, JFreeChart chart, ChartRenderingInfo renderingInfo)
        throws GeneratorException {
    int width = options.getWidth();
    int height = options.getHeight();
    BufferedImage image = chart.createBufferedImage(width, height, renderingInfo);
    Graphics2D chartGraphics = image.createGraphics();
    chartGraphics.setColor(Color.white);
    chartGraphics.fillRect(0, 0, width, height);
    chart.draw(chartGraphics, new Rectangle2D.Float(0, 0, width, height));

    try {/* w  ww .  ja va 2 s .  c o m*/
        return ServletUtilities.saveChartAsPNG(chart, width, height, renderingInfo, null);
    } catch (IOException e) {
        throw new GeneratorException("Could not save PNG!", e);
    }
}

From source file:de.uni_siegen.wineme.come_in.thumbnailer.thumbnailers.PDFBoxThumbnailer.java

private BufferedImage convertToImage(final PDPage page, final int imageType, final int thumbWidth,
        final int thumbHeight)/*     */ throws IOException
/*     */ {/*from www  .j av a  2  s  . c o m*/
    /* 707 */ final PDRectangle mBox = page.findMediaBox();
    /* 708 */ final float widthPt = mBox.getWidth();
    /* 709 */ final float heightPt = mBox.getHeight();
    /* 711 */ final int widthPx = thumbWidth;
    // Math.round(widthPt * scaling);
    /* 712 */ final int heightPx = thumbHeight;
    // Math.round(heightPt * scaling);
    /* 710 */ final double scaling = Math.min((double) thumbWidth / widthPt, (double) thumbHeight / heightPt);
    // resolution / 72.0F;
    /*     */
    /* 714 */ final Dimension pageDimension = new Dimension((int) widthPt, (int) heightPt);
    /*     */
    /* 716 */ final BufferedImage retval = new BufferedImage(widthPx, heightPx, imageType);
    /* 717 */ final Graphics2D graphics = (Graphics2D) retval.getGraphics();
    /* 718 */ graphics.setBackground(PDFBoxThumbnailer.TRANSPARENT_WHITE);
    /* 719 */ graphics.clearRect(0, 0, retval.getWidth(), retval.getHeight());
    /* 720 */ graphics.scale(scaling, scaling);
    /* 721 */ final PageDrawer drawer = new PageDrawer();
    /* 722 */ drawer.drawPage(graphics, page, pageDimension);
    /*     */ try
    /*     */ {
        /* 728 */ final int rotation = page.findRotation();
        /* 729 */ if (rotation == 90 || rotation == 270)
        /*     */ {
            /* 731 */ final int w = retval.getWidth();
            /* 732 */ final int h = retval.getHeight();
            /* 733 */ final BufferedImage rotatedImg = new BufferedImage(w, h, retval.getType());
            /* 734 */ final Graphics2D g = rotatedImg.createGraphics();
            /* 735 */ g.rotate(Math.toRadians(rotation), w / 2, h / 2);
            /* 736 */ g.drawImage(retval, null, 0, 0);
            /*     */ }
        /*     */ }
    /*     */ catch (final ImagingOpException e)
    /*     */ {
        /* 741 */ //log.warn("Unable to rotate page image", e);
        /*     */ }
    /*     */
    /* 744 */ return retval;
    /*     */ }