Example usage for java.awt Graphics2D setComposite

List of usage examples for java.awt Graphics2D setComposite

Introduction

In this page you can find the example usage for java.awt Graphics2D setComposite.

Prototype

public abstract void setComposite(Composite comp);

Source Link

Document

Sets the Composite for the Graphics2D context.

Usage

From source file:org.deegree.test.gui.StressTestController.java

private void renderResizedImage(HttpServletResponse response, String shot, int width, int height)
        throws IOException {
    BufferedImage originalImage = ImageIO.read(new File(shot));

    BufferedImage scaledImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
    Graphics2D graphics = scaledImage.createGraphics();
    graphics.setComposite(AlphaComposite.Src);
    graphics.drawImage(originalImage, 0, 0, width, height, null);
    graphics.dispose();//from   ww  w.j a  v a  2 s . com

    response.setContentType("image/jpeg");
    OutputStream out = response.getOutputStream();
    ImageIO.write(scaledImage, "jpg", out);
    out.close();

}

From source file:com.kahlon.guard.controller.PersonImageManager.java

private BufferedImage resizeImageWithHint(BufferedImage originalImage, int type) {

    BufferedImage resizedImage = new BufferedImage(IMG_WIDTH, IMG_HEIGHT, type);
    Graphics2D g = resizedImage.createGraphics();
    g.drawImage(originalImage, 0, 0, IMG_WIDTH, IMG_HEIGHT, null);
    g.dispose();/* w ww  .  j a va2s. c  o m*/
    g.setComposite(AlphaComposite.Src);

    g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
    g.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
    g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

    return resizedImage;
}

From source file:org.geomajas.plugin.rasterizing.layer.RasterDirectLayer.java

private BufferedImage makeOpaque(BufferedImage image) {
    if (image.getType() == BufferedImage.TYPE_CUSTOM) {
        log.warn("makeOpaque {} Unknown Image Type 0: ", getTitle());
        return image;
    }//from  ww w  .java2  s .c om
    BufferedImage opaqueCopy = new BufferedImage(image.getWidth(), image.getHeight(), image.getType());
    Graphics2D g1 = opaqueCopy.createGraphics();
    g1.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, getOpacity()));
    g1.drawImage(image, null, 0, 0);
    g1.dispose();
    return opaqueCopy;
}

From source file:net.sf.mzmine.modules.visualization.metamsecorrelate.visual.pseudospectra.PseudoSpectraRenderer.java

public void drawItem(Graphics2D g2, XYItemRendererState state, Rectangle2D dataArea, PlotRenderingInfo info,
        XYPlot plot, ValueAxis domainAxis, ValueAxis rangeAxis, XYDataset dataset, int series, int item,
        CrosshairState crosshairState, int pass) {

    if (isTransparent)
        g2.setComposite(alphaComp);

    super.drawItem(g2, state, dataArea, info, plot, domainAxis, rangeAxis, dataset, series, item,
            crosshairState, pass);/* w  ww.j av  a 2 s  . c  om*/

}

From source file:de.tuttas.restful.SchuelerManager.java

private BufferedImage createResizedCopy(Image originalImage, int scaledWidth, int scaledHeight,
        boolean preserveAlpha) {
    int imageType = preserveAlpha ? BufferedImage.TYPE_INT_RGB : BufferedImage.TYPE_INT_ARGB;
    BufferedImage scaledBI = new BufferedImage(scaledWidth, scaledHeight, imageType);
    Graphics2D g = scaledBI.createGraphics();
    if (preserveAlpha) {
        g.setComposite(AlphaComposite.Src);
    }//  w w w  .j  a v  a  2s .c om
    g.drawImage(originalImage, 0, 0, scaledWidth, scaledHeight, null);
    g.dispose();
    return scaledBI;
}

From source file:org.tsho.dmc2.core.chart.CowebRenderer.java

public void render(final Graphics2D g2, final Rectangle2D dataArea, final PlotRenderingInfo info) {

    state = STATE_RUNNING;//from  ww  w  .ja  v a2s.co m

    if (plot.isAlpha()) {
        g2.setComposite(AlphaComposite.SrcOver);
    }

    Stepper.Point2D result = stepper.getCurrentPoint2D();

    int transX, transY;

    double start = (int) dataArea.getMinX();
    double end = (int) dataArea.getMaxX();

    double[] value = new double[1];

    int prevY = 0;
    boolean flagOld = false;
    boolean flagNew = false;

    label: for (double i = start; i <= end; i += 1) {
        value[0] = this.domainAxis.java2DToValue(i, dataArea, RectangleEdge.BOTTOM);

        stepper.setInitialValue(value);
        stepper.initialize();

        for (int j = 0; j < power; j++) {
            stepper.step();
        }

        result = stepper.getCurrentPoint2D();

        transX = (int) i;
        transY = (int) rangeAxis.valueToJava2D(result.getX(), dataArea, RectangleEdge.LEFT);
        flagNew = Double.isNaN(result.getX());

        if (bigDots) {
            g2.fillRect(transX - 1, transY - 1, 3, 3);
        } else {
            g2.fillRect(transX, transY, 1, 1);
        }

        if (connectWithLines) {
            if (i > start) {
                if (!flagOld && !flagNew)
                    g2.drawLine(transX, transY, transX - 1, prevY);
            }

            prevY = transY;
            flagOld = flagNew;
        }

        if (stopped) {
            state = STATE_STOPPED;
            return;
        }

    }

    if (animate) {
        animateCowebPlot(g2, dataArea);
    }

    state = STATE_FINISHED;
}

From source file:edu.cuny.jfree.chart.annotations.CategoryIntervalAnnotation.java

public void draw(final Graphics2D g2, final CategoryPlot plot, final Rectangle2D dataArea,
        final CategoryAxis domainAxis, final ValueAxis rangeAxis) {

    final AlphaComposite alphaComposite = AlphaComposite.getInstance(AlphaComposite.SRC_OVER,
            plot.getForegroundAlpha());/*from  ww w.j  a v a 2 s.  co m*/
    final Composite oldComposite = g2.getComposite();
    g2.setComposite(alphaComposite);

    final CategoryDataset dataset = plot.getDataset();
    final int catIndex = dataset.getColumnIndex(category);
    final int catCount = dataset.getColumnCount();
    double lineX1 = 0.0D;
    double lineY = 0.0D;
    double lineX2 = 0.0D;
    final PlotOrientation orientation = plot.getOrientation();
    final RectangleEdge domainEdge = Plot.resolveDomainAxisLocation(plot.getDomainAxisLocation(), orientation);
    final RectangleEdge rangeEdge = Plot.resolveRangeAxisLocation(plot.getRangeAxisLocation(), orientation);
    if (orientation == PlotOrientation.HORIZONTAL) {
        lineY = domainAxis.getCategoryJava2DCoordinate(CategoryAnchor.MIDDLE, catIndex, catCount, dataArea,
                domainEdge);
        lineX1 = rangeAxis.valueToJava2D(value1, dataArea, rangeEdge);
        lineX2 = rangeAxis.valueToJava2D(value2, dataArea, rangeEdge);
    } else if (orientation == PlotOrientation.VERTICAL) {
        lineY = rangeAxis.valueToJava2D(value1, dataArea, rangeEdge);
        lineX1 = domainAxis.getCategoryJava2DCoordinate(CategoryAnchor.MIDDLE, catIndex, catCount, dataArea,
                domainEdge);
        lineX2 = domainAxis.getCategoryJava2DCoordinate(CategoryAnchor.MIDDLE, catIndex, catCount, dataArea,
                domainEdge);
    }
    g2.setPaint(paint);
    g2.setStroke(stroke);
    g2.drawLine((int) lineX1, (int) lineY, (int) lineX2, (int) lineY);

    g2.setComposite(oldComposite);
}

From source file:com.celements.photo.image.GenerateThumbnail.java

private void drawBackground(String copyright, int width, int height, int bottomSpace, int rightSpace,
        int vSpacing, int hSpacing, int rounding, int stringHeight, FontMetrics metrics, Graphics2D g2d) {
    g2d.setColor(new Color(0, 0, 0));
    AlphaComposite transprency = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.4f);
    g2d.setComposite(transprency);
    g2d.fillRoundRect(width - metrics.stringWidth(copyright) - rightSpace - 2 * hSpacing,
            height - stringHeight - bottomSpace - 2 * vSpacing, metrics.stringWidth(copyright) + 2 * hSpacing,
            stringHeight + 2 * vSpacing, rounding, rounding);
}

From source file:de.fhg.igd.mapviewer.server.wms.overlay.WMSTileOverlay.java

/**
 * @see AbstractTileOverlayPainter#repaintTile(int, int, int, int,
 *      PixelConverter, int)//from w  ww  . ja  va  2  s.co m
 */
@Override
public BufferedImage repaintTile(int posX, int posY, int width, int height, PixelConverter converter,
        int zoom) {
    // the first converter isn't regarded as a new converter because it's
    // always the empty map
    boolean isNewConverter = lastConverter != null && !converter.equals(lastConverter);
    lastConverter = converter;

    if (!converter.supportsBoundingBoxes()) {
        if (isNewConverter) {
            handleError(Messages.WMSTileOverlay_0 + configuration.getName() + Messages.WMSTileOverlay_1);
        }
        return null;
    }

    synchronized (this) {
        if (capabilities == null) {
            try {
                capabilities = WMSUtil.getCapabilities(configuration.getBaseUrl());
            } catch (WMSCapabilitiesException e) {
                log.error("Error getting WMS capabilities"); //$NON-NLS-1$
            }
        }
    }

    if (capabilities != null) {
        int mapEpsg = converter.getMapEpsg();

        WMSBounds box;
        synchronized (this) {
            if (capabilities.getSupportedSRS().contains("EPSG:" + mapEpsg)) { //$NON-NLS-1$
                // same SRS supported
            } else {
                // SRS not supported
                if (isNewConverter) {
                    StringBuilder message = new StringBuilder();
                    message.append(Messages.WMSTileOverlay_2);
                    message.append(configuration.getName());
                    message.append(Messages.WMSTileOverlay_3);
                    boolean init = true;
                    for (String srs : capabilities.getSupportedSRS()) {
                        if (init) {
                            init = false;
                        } else {
                            message.append(", "); //$NON-NLS-1$
                        }
                        message.append(srs);
                    }
                    handleError(message.toString());
                }
                return null;
            }

            box = WMSUtil.getBoundingBox(capabilities, mapEpsg);
        }

        String srs = box.getSRS();

        if (srs.startsWith("EPSG:")) { //$NON-NLS-1$
            // determine format
            String format = null;
            Iterator<String> itFormat = supportedFormats.iterator();
            synchronized (this) {
                while (format == null && itFormat.hasNext()) {
                    String supp = itFormat.next();
                    if (capabilities.getFormats().contains(supp)) {
                        format = supp;
                    }
                }
            }
            if (format == null) {
                // no compatible format
                return null;
            }

            try {
                // check if tile lies within the bounding box
                int epsg = Integer.parseInt(srs.substring(5));

                GeoPosition topLeft = converter.pixelToGeo(new Point(posX, posY), zoom);
                GeoPosition bottomRight = converter.pixelToGeo(new Point(posX + width, posY + height), zoom);

                // WMS bounding box
                BoundingBox wms = new BoundingBox(box.getMinX(), box.getMinY(), -1, box.getMaxX(),
                        box.getMaxY(), 1);

                GeoConverter geotools = GeotoolsConverter.getInstance();
                GeoPosition bbTopLeft = geotools.convert(topLeft, epsg);
                GeoPosition bbBottomRight = geotools.convert(bottomRight, epsg);

                double minX = Math.min(bbTopLeft.getX(), bbBottomRight.getX());
                double minY = Math.min(bbTopLeft.getY(), bbBottomRight.getY());
                double maxX = Math.max(bbTopLeft.getX(), bbBottomRight.getX());
                double maxY = Math.max(bbTopLeft.getY(), bbBottomRight.getY());

                BoundingBox tile = new BoundingBox(minX, minY, -1, maxX, maxY, 1);

                // check if bounding box and tile overlap
                if (wms.intersectsOrCovers(tile) || tile.covers(wms)) {
                    WMSBounds bounds;
                    if (epsg == mapEpsg) {
                        bounds = new WMSBounds(srs, minX, minY, maxX, maxY);
                    } else {
                        // determine bounds for request
                        minX = Math.min(topLeft.getX(), bottomRight.getX());
                        minY = Math.min(topLeft.getY(), bottomRight.getY());
                        maxX = Math.max(topLeft.getX(), bottomRight.getX());
                        maxY = Math.max(topLeft.getY(), bottomRight.getY());
                        bounds = new WMSBounds("EPSG:" + mapEpsg, minX, minY, maxX, maxY); //$NON-NLS-1$
                    }

                    URI uri;
                    synchronized (this) {
                        uri = WMSUtil.getMapURI(capabilities, configuration, width, height, bounds, null,
                                format, true);
                    }

                    Proxy proxy = ProxyUtil.findProxy(uri);

                    InputStream in = uri.toURL().openConnection(proxy).getInputStream();

                    BufferedImage image = GraphicsUtilities.loadCompatibleImage(in);

                    // apply transparency to the image
                    BufferedImage result = GraphicsUtilities.createCompatibleTranslucentImage(image.getWidth(),
                            image.getHeight());
                    Graphics2D g = result.createGraphics();
                    try {
                        AlphaComposite ac = AlphaComposite.getInstance(AlphaComposite.SRC, 0.5f);
                        g.setComposite(ac);
                        g.drawImage(image, 0, 0, null);
                    } finally {
                        g.dispose();
                    }

                    return result;
                }
            } catch (Throwable e) {
                log.warn("Error painting WMS overlay", e); //$NON-NLS-1$
            }
        }
    }

    return null;
}

From source file:Diva.java

@Override
public void paint(Graphics g, JComponent c) {
    Graphics2D g2 = (Graphics2D) g.create();

    // Paint the view.
    super.paint(g2, c);

    if (mActive) {
        // Create a radial gradient, transparent in the middle.
        java.awt.geom.Point2D center = new java.awt.geom.Point2D.Float(mX, mY);
        float radius = 72;
        float[] dist = { 0.0f, 1.0f };
        Color[] colors = { new Color(0.0f, 0.0f, 0.0f, 0.0f), Color.BLACK };
        RadialGradientPaint p = new RadialGradientPaint(center, radius, dist, colors);
        g2.setPaint(p);/*from  ww w.  j  a  v  a 2 s  . com*/
        g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, .6f));
        g2.fillRect(0, 0, c.getWidth(), c.getHeight());
    }

    g2.dispose();
}