Example usage for java.awt.geom Rectangle2D.Float getY

List of usage examples for java.awt.geom Rectangle2D.Float getY

Introduction

In this page you can find the example usage for java.awt.geom Rectangle2D.Float getY.

Prototype

public abstract double getY();

Source Link

Document

Returns the Y coordinate of the upper-left corner of the framing rectangle in double precision.

Usage

From source file:tufts.vue.LWComponent.java

/**
 * Useful for drawing drag images into an existing graphics buffer, or drawing exportable images.
 *
 * @param alpha 0.0 (invisible) to 1.0 (no alpha -- completely opaque)
 * @param maxSize max dimensions for image. May be null.  Image may be smaller than maxSize.
 * @param fillColor -- if non-null, will be rendered as background for image.
 * @param zoomRequest -- desired zoom; ignored if maxSize is non-null
 * also set, background fill will have transparency of alpha^3 to enhance contrast.
 *//*w  w w. ja va  2  s .  com*/

public void drawImage(Graphics2D g, double alpha, Dimension maxSize, Color fillColor, double zoomRequest) {
    //if (DEBUG.IMAGE) out("drawImage; size " + maxSize);

    final boolean drawBorder = false;// this instanceof LWMap; // hack for dragged images of LWMaps

    final Rectangle2D.Float bounds = getImageBounds();
    final Rectangle clip = g.getClipBounds();
    final Size fillSize = new Size(bounds);
    final double zoom = computeZoomAndSize(bounds, maxSize, zoomRequest, fillSize);

    if (DEBUG.IMAGE)
        out(TERM_GREEN + "drawImage:" + "\n\t   mapBounds: " + fmt(bounds) + "\n\t        fill: " + fillColor
                + "\n\t     maxSize: " + maxSize + "\n\t zoomRequest: " + zoomRequest + "\n\t     fitZoom: "
                + zoom + "\n\t    fillSize: " + fillSize + "\n\t          gc: " + g + "\n\t        clip: "
                + fmt(clip) + "\n\t       alpha: " + alpha + TERM_CLEAR);

    final int width = fillSize.pixelWidth();
    final int height = fillSize.pixelHeight();

    final DrawContext dc = new DrawContext(g, this);

    dc.setInteractive(false);

    if (alpha == OPAQUE) {
        dc.setPrintQuality();
    } else {
        // if alpha, assume drag image (todo: better specified as an argument)
        dc.setDraftQuality();
    }

    dc.setBackgroundFill(getRenderFillColor(null)); // sure we want null here?
    dc.setClipOptimized(false); // always draw all children -- don't bother to check bounds
    if (DEBUG.IMAGE)
        out(TERM_GREEN + "drawImage: " + dc + TERM_CLEAR);

    if (fillColor != null) {
        //             if (false && alpha != OPAQUE) {
        //                 Color c = fillColor;
        //                 // if we have an alpha and a fill, amplify the alpha on the background fill
        //                 // by changing the fill to one that has alpha*alpha, for a total of
        //                 // alpha*alpha*alpha given our GC already has an alpha set.
        //                 fillColor = new Color(c.getRed(), c.getGreen(), c.getBlue(), (int) (alpha*alpha*255+0.5));
        //             }
        if (alpha != OPAQUE)
            dc.setAlpha(alpha, AlphaComposite.SRC); // erase any underlying in cache
        if (DEBUG.IMAGE)
            out("drawImage: fill=" + fillColor);
        g.setColor(fillColor);
        g.fillRect(0, 0, width, height);
    } else { //if (alpha != OPAQUE) {
        // we didn't have a fill, but we have an alpha: make sure any cached data is cleared
        // todo?: if fill is null, we need to clear as well -- it means we have implied alpha on any non-drawn bits
        // TODO: if this is a selection drag, we usually want to fill with the map color (or ideally, the color
        // of the common parent, e.g., a slide, if there's one common parent)
        dc.g.setComposite(AlphaComposite.Clear);
        g.fillRect(0, 0, width, height);
    }

    //if (alpha != OPAQUE)
    dc.setAlpha(alpha, AlphaComposite.SRC);

    if (DEBUG.IMAGE && DEBUG.META) {
        // Fill the entire imageable area
        g.setColor(Color.green);
        g.fillRect(0, 0, Short.MAX_VALUE, Short.MAX_VALUE);
    }

    final AffineTransform rawTransform = g.getTransform();

    if (zoom != 1.0)
        dc.g.scale(zoom, zoom);

    // translate so that the upper left corner of the map region
    // we're drawing is at 0,0 on the underlying image

    g.translate(-bounds.getX(), -bounds.getY());

    // GC *must* have a bounds set or we get NPE's in JComponent (textBox) rendering
    dc.setMasterClip(bounds);

    if (DEBUG.IMAGE && DEBUG.META) {
        // fill the clipped area so we can check our clip bounds
        dc.g.setColor(Color.red);
        dc.g.fillRect(-Short.MAX_VALUE / 2, -Short.MAX_VALUE / 2, // larger values than this can blow out internal GC code and we get nothing
                Short.MAX_VALUE, Short.MAX_VALUE);
    }

    if (this instanceof LWImage) {
        // for some reason, raw images don't seem to want to draw unless we fill first
        dc.g.setColor(Color.white);
        dc.g.fill(bounds);
    }

    // render to the image through the DrawContext/GC pointing to it
    draw(dc);

    if (drawBorder) {
        g.setTransform(rawTransform);
        //g.setColor(Color.red);
        //g.fillRect(0,0, Short.MAX_VALUE, Short.MAX_VALUE);
        if (DEBUG.IMAGE) {
            g.setColor(Color.black);
            dc.setAntiAlias(false);
        } else
            g.setColor(Color.darkGray);
        g.drawRect(0, 0, width - 1, height - 1);
    }

    if (DEBUG.IMAGE)
        out(TERM_GREEN + "drawImage: completed\n" + TERM_CLEAR);

}