Example usage for java.awt AlphaComposite getInstance

List of usage examples for java.awt AlphaComposite getInstance

Introduction

In this page you can find the example usage for java.awt AlphaComposite getInstance.

Prototype

public static AlphaComposite getInstance(int rule, float alpha) 

Source Link

Document

Creates an AlphaComposite object with the specified rule and the constant alpha to multiply with the alpha of the source.

Usage

From source file:ucar.unidata.idv.control.chart.MyXYPlot.java

/**
 * Draws the plot within the specified area on a graphics device.
 *
 * @param g2  the graphics device.// w  w w  . jav  a  2s .  com
 * @param area  the plot area (in Java2D space).
 * @param anchor  an anchor point in Java2D space (<code>null</code>
 *                permitted).
 * @param parentState  the state from the parent plot, if there is one
 *                     (<code>null</code> permitted).
 * @param info  collects chart drawing information (<code>null</code>
 *              permitted).
 */
public void draw(Graphics2D g2, Rectangle2D area, Point2D anchor, PlotState parentState,
        PlotRenderingInfo info) {

    // if the plot area is too small, just return...
    boolean b1 = (area.getWidth() <= MINIMUM_WIDTH_TO_DRAW);
    boolean b2 = (area.getHeight() <= MINIMUM_HEIGHT_TO_DRAW);
    if (b1 || b2) {
        return;
    }

    // record the plot area...
    if (info != null) {
        info.setPlotArea(area);
    }

    // adjust the drawing area for the plot insets (if any)...
    RectangleInsets insets = getInsets();
    insets.trim(area);

    AxisSpace space = calculateAxisSpace(g2, area);
    Rectangle2D dataArea = space.shrink(area, null);
    this.axisOffset.trim(dataArea);

    if (info != null) {
        info.setDataArea(dataArea);
    }

    // draw the plot background and axes...
    drawBackground(g2, dataArea);
    Map axisStateMap = drawAxes(g2, area, dataArea, info);

    if ((anchor != null) && !dataArea.contains(anchor)) {
        anchor = null;
    }
    CrosshairState crosshairState = new CrosshairState();
    crosshairState.setCrosshairDistance(Double.POSITIVE_INFINITY);
    crosshairState.setAnchor(anchor);
    crosshairState.setCrosshairX(getDomainCrosshairValue());
    crosshairState.setCrosshairY(getRangeCrosshairValue());
    Shape originalClip = g2.getClip();
    Composite originalComposite = g2.getComposite();

    g2.clip(dataArea);
    g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, getForegroundAlpha()));

    AxisState domainAxisState = (AxisState) axisStateMap.get(getDomainAxis());
    if (domainAxisState == null) {
        if (parentState != null) {
            domainAxisState = (AxisState) parentState.getSharedAxisStates().get(getDomainAxis());
        }
    }
    if (domainAxisState != null) {
        drawDomainTickBands(g2, dataArea, domainAxisState.getTicks());
        drawDomainGridlines(g2, dataArea, domainAxisState.getTicks());
    }

    AxisState rangeAxisState = (AxisState) axisStateMap.get(getRangeAxis());
    if (rangeAxisState == null) {
        if (parentState != null) {
            rangeAxisState = (AxisState) parentState.getSharedAxisStates().get(getRangeAxis());
        }
    }
    if (rangeAxisState != null) {
        drawRangeTickBands(g2, dataArea, rangeAxisState.getTicks());
        drawRangeGridlines(g2, dataArea, rangeAxisState.getTicks());
        drawZeroRangeBaseline(g2, dataArea);
    }

    // draw the markers that are associated with a specific renderer...
    for (int i = 0; i < this.renderers.size(); i++) {
        drawDomainMarkers(g2, dataArea, i, Layer.BACKGROUND);
    }
    for (int i = 0; i < this.renderers.size(); i++) {
        drawRangeMarkers(g2, dataArea, i, Layer.BACKGROUND);
    }

    // now draw annotations and render data items...
    boolean foundData = false;
    DatasetRenderingOrder order = getDatasetRenderingOrder();
    if (order == DatasetRenderingOrder.FORWARD) {

        // draw background annotations
        int rendererCount = this.renderers.size();
        for (int i = 0; i < rendererCount; i++) {
            XYItemRenderer r = getRenderer(i);
            if (r != null) {
                ValueAxis domainAxis = getDomainAxisForDataset(i);
                ValueAxis rangeAxis = getRangeAxisForDataset(i);
                r.drawAnnotations(g2, dataArea, domainAxis, rangeAxis, Layer.BACKGROUND, info);
            }
        }

        // render data items...
        for (int i = 0; i < getDatasetCount(); i++) {
            foundData = render(g2, dataArea, i, info, crosshairState) || foundData;
        }

        // draw foreground annotations
        for (int i = 0; i < rendererCount; i++) {
            XYItemRenderer r = getRenderer(i);
            if (r != null) {
                ValueAxis domainAxis = getDomainAxisForDataset(i);
                ValueAxis rangeAxis = getRangeAxisForDataset(i);
                r.drawAnnotations(g2, dataArea, domainAxis, rangeAxis, Layer.FOREGROUND, info);
            }
        }

    } else if (order == DatasetRenderingOrder.REVERSE) {

        // draw background annotations
        int rendererCount = this.renderers.size();
        for (int i = rendererCount - 1; i >= 0; i--) {
            XYItemRenderer r = getRenderer(i);
            if (r != null) {
                ValueAxis domainAxis = getDomainAxisForDataset(i);
                ValueAxis rangeAxis = getRangeAxisForDataset(i);
                r.drawAnnotations(g2, dataArea, domainAxis, rangeAxis, Layer.BACKGROUND, info);
            }
        }

        ucar.unidata.util.Trace.call1("renderData");
        for (int i = getDatasetCount() - 1; i >= 0; i--) {
            foundData = render(g2, dataArea, i, info, crosshairState) || foundData;
        }
        ucar.unidata.util.Trace.call2("renderData");

        // draw foreground annotations
        for (int i = rendererCount - 1; i >= 0; i--) {
            XYItemRenderer r = getRenderer(i);
            if (r != null) {
                ValueAxis domainAxis = getDomainAxisForDataset(i);
                ValueAxis rangeAxis = getRangeAxisForDataset(i);
                r.drawAnnotations(g2, dataArea, domainAxis, rangeAxis, Layer.FOREGROUND, info);
            }
        }

    }

    PlotOrientation orient = getOrientation();

    // draw domain crosshair if required...
    if (!this.domainCrosshairLockedOnData && (anchor != null)) {
        double xx = getDomainAxis().java2DToValue(anchor.getX(), dataArea, getDomainAxisEdge());
        crosshairState.setCrosshairX(xx);
    }
    setDomainCrosshairValue(crosshairState.getCrosshairX(), false);
    if (isDomainCrosshairVisible()) {
        double x = getDomainCrosshairValue();
        Paint paint = getDomainCrosshairPaint();
        Stroke stroke = getDomainCrosshairStroke();
        if (orient == PlotOrientation.HORIZONTAL) {
            drawHorizontalLine(g2, dataArea, x, stroke, paint);
        } else if (orient == PlotOrientation.VERTICAL) {
            drawVerticalLine(g2, dataArea, x, stroke, paint);
        }
    }

    // draw range crosshair if required...
    if (!this.rangeCrosshairLockedOnData && (anchor != null)) {
        double yy = getRangeAxis().java2DToValue(anchor.getY(), dataArea, getRangeAxisEdge());
        crosshairState.setCrosshairX(yy);
    }
    setRangeCrosshairValue(crosshairState.getCrosshairY(), false);
    if (isRangeCrosshairVisible() && getRangeAxis().getRange().contains(getRangeCrosshairValue())) {
        double y = getRangeCrosshairValue();
        Paint paint = getRangeCrosshairPaint();
        Stroke stroke = getRangeCrosshairStroke();
        if (orient == PlotOrientation.HORIZONTAL) {
            drawVerticalLine(g2, dataArea, y, stroke, paint);
        } else if (orient == PlotOrientation.VERTICAL) {
            drawHorizontalLine(g2, dataArea, y, stroke, paint);
        }
    }

    if (!foundData) {
        drawNoDataMessage(g2, dataArea);
    }

    for (int i = 0; i < this.renderers.size(); i++) {
        drawDomainMarkers(g2, dataArea, i, Layer.FOREGROUND);
    }
    for (int i = 0; i < this.renderers.size(); i++) {
        drawRangeMarkers(g2, dataArea, i, Layer.FOREGROUND);
    }

    drawAnnotations(g2, dataArea, info);
    g2.setClip(originalClip);
    g2.setComposite(originalComposite);

    drawOutline(g2, dataArea);

}

From source file:au.org.ala.biocache.web.WMSController.java

/**
 * Method that produces the downloadable map integrated in AVH/OZCAM/Biocache.
 *
 * @param requestParams//from   ww w  .  j a v a 2 s .  c o  m
 * @param format
 * @param extents
 * @param widthMm
 * @param pointRadiusMm
 * @param pradiusPx
 * @param pointColour
 * @param pointOpacity
 * @param baselayer
 * @param scale
 * @param dpi
 * @param outlinePoints
 * @param outlineColour
 * @param fileName
 * @param request
 * @param response
 * @throws Exception
 */
@RequestMapping(value = { "/webportal/wms/image", "/mapping/wms/image" }, method = RequestMethod.GET)
public void generatePublicationMap(SpatialSearchRequestParams requestParams,
        @RequestParam(value = "format", required = false, defaultValue = "jpg") String format,
        @RequestParam(value = "extents", required = true) String extents,
        @RequestParam(value = "widthmm", required = false, defaultValue = "60") Double widthMm,
        @RequestParam(value = "pradiusmm", required = false, defaultValue = "2") Double pointRadiusMm,
        @RequestParam(value = "pradiuspx", required = false) Integer pradiusPx,
        @RequestParam(value = "pcolour", required = false, defaultValue = "FF0000") String pointColour,
        @RequestParam(value = "popacity", required = false, defaultValue = "0.8") Double pointOpacity,
        @RequestParam(value = "baselayer", required = false, defaultValue = "world") String baselayer,
        @RequestParam(value = "scale", required = false, defaultValue = "off") String scale,
        @RequestParam(value = "dpi", required = false, defaultValue = "300") Integer dpi,
        @RequestParam(value = "outline", required = true, defaultValue = "false") boolean outlinePoints,
        @RequestParam(value = "outlineColour", required = true, defaultValue = "#000000") String outlineColour,
        @RequestParam(value = "fileName", required = false) String fileName, HttpServletRequest request,
        HttpServletResponse response) throws Exception {

    String[] bb = extents.split(",");

    double long1 = Double.parseDouble(bb[0]);
    double lat1 = Double.parseDouble(bb[1]);
    double long2 = Double.parseDouble(bb[2]);
    double lat2 = Double.parseDouble(bb[3]);

    if (lat1 <= -90) {
        lat1 = -89.999;
    }
    if (lat2 >= 90) {
        lat2 = 89.999;
    }

    int pminx = convertLngToPixel(long1);
    int pminy = convertLatToPixel(lat1);
    int pmaxx = convertLngToPixel(long2);
    int pmaxy = convertLatToPixel(lat2);

    int width = (int) ((dpi / 25.4) * widthMm);
    int height = (int) Math.round(width * ((pminy - pmaxy) / (double) (pmaxx - pminx)));

    if (height * width > MAX_IMAGE_PIXEL_COUNT) {
        String errorMessage = "Image size in pixels " + width + "x" + height + " exceeds "
                + MAX_IMAGE_PIXEL_COUNT + " pixels.  Make the image smaller";
        response.sendError(response.SC_NOT_ACCEPTABLE, errorMessage);
        throw new Exception(errorMessage);
    }

    int pointSize = -1;
    if (pradiusPx != null) {
        pointSize = (int) pradiusPx;
    } else {
        pointSize = (int) ((dpi / 25.4) * pointRadiusMm);
    }

    double[] boundingBox = transformBbox4326To900913(Double.parseDouble(bb[0]), Double.parseDouble(bb[1]),
            Double.parseDouble(bb[2]), Double.parseDouble(bb[3]));

    //"http://biocache.ala.org.au/ws/webportal/wms/reflect?
    //q=macropus&ENV=color%3Aff0000%3Bname%3Acircle%3Bsize%3A3%3Bopacity%3A1
    //&BBOX=12523443.0512,-2504688.2032,15028131.5936,0.33920000120997&WIDTH=256&HEIGHT=256");
    String speciesAddress = baseWsUrl + "/ogc/wms/reflect?" + "ENV=color%3A" + pointColour
            + "%3Bname%3Acircle%3Bsize%3A" + pointSize + "%3Bopacity%3A" + pointOpacity + "&BBOX="
            + boundingBox[0] + "," + boundingBox[1] + "," + boundingBox[2] + "," + boundingBox[3] + "&WIDTH="
            + width + "&HEIGHT=" + height + "&OUTLINE=" + outlinePoints + "&OUTLINECOLOUR=" + outlineColour
            + "&" + request.getQueryString();

    URL speciesURL = new URL(speciesAddress);
    BufferedImage speciesImage = ImageIO.read(speciesURL);

    //"http://spatial.ala.org.au/geoserver/wms/reflect?
    //LAYERS=ALA%3Aworld&SERVICE=WMS&VERSION=1.1.1&REQUEST=GetMap&STYLES=
    //&FORMAT=image%2Fjpeg&SRS=EPSG%3A900913&BBOX=12523443.0512,-1252343.932,13775787.3224,0.33920000004582&WIDTH=256&HEIGHT=256"
    String layout = "";
    if (!scale.equals("off")) {
        layout += "layout:scale";
    }
    String basemapAddress = geoserverUrl + "/wms/reflect?" + "LAYERS=ALA%3A" + baselayer
            + "&SERVICE=WMS&VERSION=1.1.1&REQUEST=GetMap&STYLES=" + "&FORMAT=image%2Fpng&SRS=EPSG%3A900913" //specify the mercator projection
            + "&BBOX=" + boundingBox[0] + "," + boundingBox[1] + "," + boundingBox[2] + "," + boundingBox[3]
            + "&WIDTH=" + width + "&HEIGHT=" + height + "&OUTLINE=" + outlinePoints + "&format_options=dpi:"
            + dpi + ";" + layout;

    BufferedImage basemapImage = ImageIO.read(new URL(basemapAddress));

    BufferedImage img = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
    Graphics2D combined = (Graphics2D) img.getGraphics();

    combined.drawImage(basemapImage, 0, 0, Color.WHITE, null);
    //combined.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, pointOpacity.floatValue()));
    combined.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 1.0f));
    combined.drawImage(speciesImage, null, 0, 0);
    combined.dispose();

    //if filename supplied, force a download
    if (fileName != null) {
        response.setContentType("application/octet-stream");
        response.setHeader("Content-Description", "File Transfer");
        response.setHeader("Content-Disposition", "attachment; filename=" + fileName);
        response.setHeader("Content-Transfer-Encoding", "binary");
    } else if (format.equalsIgnoreCase("png")) {
        response.setContentType("image/png");
    } else {
        response.setContentType("image/jpeg");
    }

    if (format.equalsIgnoreCase("png")) {
        OutputStream os = response.getOutputStream();
        ImageIO.write(img, format, os);
        os.close();
    } else {
        //handle jpeg + BufferedImage.TYPE_INT_ARGB
        BufferedImage img2;
        Graphics2D c2;
        (c2 = (Graphics2D) (img2 = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB)).getGraphics())
                .drawImage(img, 0, 0, Color.WHITE, null);
        c2.dispose();
        OutputStream os = response.getOutputStream();
        ImageIO.write(img2, format, os);
        os.close();
    }
}

From source file:ucar.unidata.idv.control.chart.MyXYPlot.java

/**
 * Draws the quadrants./*from   w  w  w . j a v a  2  s  . com*/
 *
 * @param g2  the graphics device.
 * @param area  the area.
 */
protected void drawQuadrants(Graphics2D g2, Rectangle2D area) {

    //  0 | 1
    //  --+--
    //  2 | 3
    boolean somethingToDraw = false;

    ValueAxis xAxis = getDomainAxis();
    double x = this.quadrantOrigin.getX();
    double xx = xAxis.valueToJava2D(x, area, getDomainAxisEdge());

    ValueAxis yAxis = getRangeAxis();
    double y = this.quadrantOrigin.getY();
    double yy = yAxis.valueToJava2D(y, area, getRangeAxisEdge());

    double xmin = xAxis.getLowerBound();
    double xxmin = xAxis.valueToJava2D(xmin, area, getDomainAxisEdge());

    double xmax = xAxis.getUpperBound();
    double xxmax = xAxis.valueToJava2D(xmax, area, getDomainAxisEdge());

    double ymin = yAxis.getLowerBound();
    double yymin = yAxis.valueToJava2D(ymin, area, getRangeAxisEdge());

    double ymax = yAxis.getUpperBound();
    double yymax = yAxis.valueToJava2D(ymax, area, getRangeAxisEdge());

    Rectangle2D[] r = new Rectangle2D[] { null, null, null, null };
    if (this.quadrantPaint[0] != null) {
        if ((x > xmin) && (y < ymax)) {
            if (this.orientation == PlotOrientation.HORIZONTAL) {
                r[0] = new Rectangle2D.Double(Math.min(yymax, yy), Math.min(xxmin, xx), Math.abs(yy - yymax),
                        Math.abs(xx - xxmin));
            } else { // PlotOrientation.VERTICAL
                r[0] = new Rectangle2D.Double(Math.min(xxmin, xx), Math.min(yymax, yy), Math.abs(xx - xxmin),
                        Math.abs(yy - yymax));
            }
            somethingToDraw = true;
        }
    }
    if (this.quadrantPaint[1] != null) {
        if ((x < xmax) && (y < ymax)) {
            if (this.orientation == PlotOrientation.HORIZONTAL) {
                r[1] = new Rectangle2D.Double(Math.min(yymax, yy), Math.min(xxmax, xx), Math.abs(yy - yymax),
                        Math.abs(xx - xxmax));
            } else { // PlotOrientation.VERTICAL
                r[1] = new Rectangle2D.Double(Math.min(xx, xxmax), Math.min(yymax, yy), Math.abs(xx - xxmax),
                        Math.abs(yy - yymax));
            }
            somethingToDraw = true;
        }
    }
    if (this.quadrantPaint[2] != null) {
        if ((x > xmin) && (y > ymin)) {
            if (this.orientation == PlotOrientation.HORIZONTAL) {
                r[2] = new Rectangle2D.Double(Math.min(yymin, yy), Math.min(xxmin, xx), Math.abs(yy - yymin),
                        Math.abs(xx - xxmin));
            } else { // PlotOrientation.VERTICAL
                r[2] = new Rectangle2D.Double(Math.min(xxmin, xx), Math.min(yymin, yy), Math.abs(xx - xxmin),
                        Math.abs(yy - yymin));
            }
            somethingToDraw = true;
        }
    }
    if (this.quadrantPaint[3] != null) {
        if ((x < xmax) && (y > ymin)) {
            if (this.orientation == PlotOrientation.HORIZONTAL) {
                r[3] = new Rectangle2D.Double(Math.min(yymin, yy), Math.min(xxmax, xx), Math.abs(yy - yymin),
                        Math.abs(xx - xxmax));
            } else { // PlotOrientation.VERTICAL
                r[3] = new Rectangle2D.Double(Math.min(xx, xxmax), Math.min(yymin, yy), Math.abs(xx - xxmax),
                        Math.abs(yy - yymin));
            }
            somethingToDraw = true;
        }
    }
    if (somethingToDraw) {
        Composite originalComposite = g2.getComposite();
        g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, getBackgroundAlpha()));
        for (int i = 0; i < 4; i++) {
            if ((this.quadrantPaint[i] != null) && (r[i] != null)) {
                g2.setPaint(this.quadrantPaint[i]);
                g2.fill(r[i]);
            }
        }
        g2.setComposite(originalComposite);
    }
}

From source file:edu.ku.brc.ui.UIHelper.java

/**
 * @param g2/*from  ww  w .  j  a  v  a 2  s  .c o m*/
 * @param shape
 * @param glowWidth
 */
public static void paintBorderGlow(final Graphics2D g2, final Shape shape, final int glowWidth) {
    int gw = glowWidth * 2;
    for (int i = gw; i >= 2; i -= 2) {
        float pct = (float) (gw - i) / (gw - 1);

        Color mixHi = getMixedColor(clrGlowInnerHi, pct, clrGlowOuterHi, 1.0f - pct);
        Color mixLo = getMixedColor(clrGlowInnerLo, pct, clrGlowOuterLo, 1.0f - pct);
        g2.setPaint(new GradientPaint(0.0f, 40 * 0.25f, mixHi, 0.0f, 40, mixLo));

        g2.setColor(Color.WHITE);

        // See my "Java 2D Trickery: Soft Clipping" entry for more
        // on why we use SRC_ATOP here
        g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, pct));
        g2.setStroke(new BasicStroke(i));
        g2.draw(shape);
    }
}