Example usage for java.awt Rectangle intersects

List of usage examples for java.awt Rectangle intersects

Introduction

In this page you can find the example usage for java.awt Rectangle intersects.

Prototype

public boolean intersects(Rectangle r) 

Source Link

Document

Determines whether or not this Rectangle and the specified Rectangle intersect.

Usage

From source file:corelyzer.ui.CorelyzerApp.java

public Component getPopupParent(CorelyzerGLCanvas srcCanvas) {
    if (srcCanvas == null) {
        return getMainFrame();
    }//w  ww  .  j a  va 2  s  .  com
    Rectangle mfBounds = getMainFrame().getBounds();
    Rectangle canvasBounds = srcCanvas.getCanvas().getParent().getBounds();
    Point canvasLoc = srcCanvas.getCanvas().getParent().getLocationOnScreen();
    canvasBounds.translate(canvasLoc.x, canvasLoc.y);
    if (mfBounds.intersects(canvasBounds)) {
        return getMainFrame();
    } else {
        return srcCanvas.getCanvas();
    }
}

From source file:org.broad.igv.variant.VariantTrack.java

/**
 * Render the features in the supplied rectangle.
 *
 * @param context/*  w  w w.  j av  a 2  s.c o m*/
 * @param trackRectangle
 * @param packedFeatures
 */
@Override
protected void renderFeatureImpl(RenderContext context, Rectangle trackRectangle,
        PackedFeatures packedFeatures) {

    Graphics2D g2D = context.getGraphics();

    top = trackRectangle.y;
    Rectangle visibleRectangle = context.getVisibleRect();

    // A disposable rect -- note this gets modified all over the place, bad practice
    Rectangle tmpRect = new Rectangle(trackRectangle);
    tmpRect.height = getGenotypeBandHeight();
    tmpRect.y = trackRectangle.y;

    Rectangle bandRect = new Rectangle(tmpRect);
    bandRect.y += getVariantsHeight();
    drawBackground(g2D, bandRect, visibleRectangle, BackgroundType.DATA);

    List<PackedFeatures.FeatureRow> rows = packedFeatures.getRows();

    int overallFeatureRectHeight = getVariantsHeight();
    int overallSampleRectHeight = trackRectangle.height - overallFeatureRectHeight;
    Rectangle overallSampleRect = new Rectangle(trackRectangle.x, top + overallFeatureRectHeight,
            trackRectangle.width, overallSampleRectHeight);

    int curRowTop = top;

    if (rows.size() > 0) {
        final double locScale = context.getScale();
        final double origin = context.getOrigin();

        final double pXMin = tmpRect.getMinX();
        final double pXMax = tmpRect.getMaxX();
        tmpRect.height = variantBandHeight;

        int lastEndX = -1;
        int minSpacing = 3;
        for (PackedFeatures.FeatureRow row : rows) {
            List<Feature> features = row.getFeatures();
            for (Feature feature : features) {
                Variant variant = (Variant) feature;

                if (hideFiltered && variant.isFiltered()) {
                    continue;
                }

                int start = variant.getStart();
                int end = variant.getEnd();
                int pX = (int) ((start - origin) / locScale);
                int dX = (int) Math.max(2, (end - start) / locScale);

                if (pX + dX < pXMin) {
                    continue;
                }
                if (pX > pXMax) {
                    break;
                }
                int w = dX;
                int x = pX;

                if (w < 3) {
                    w = 3;
                    x--;
                }

                //Make sure we have some whitespace between this
                //feature and the previous one, but only if they don't
                //actually overlap and the current size is reasonably large
                int spacing = x - lastEndX;
                if (spacing > 0 && spacing < minSpacing && w > 2 * minSpacing) {
                    x += minSpacing - spacing;
                }

                tmpRect.y = curRowTop;
                if (tmpRect.intersects(visibleRectangle)) {
                    renderer.renderSiteBand(variant, tmpRect, x, w, context);
                    lastEndX = x + w - 1;
                }

                renderSamples(g2D, visibleRectangle, variant, context, overallSampleRect, x, w);
                boolean isSelected = selectedVariant != null && selectedVariant == variant;
                if (isSelected) {
                    Graphics2D selectionGraphics = context.getGraphic2DForColor(Color.black);
                    selectionGraphics.drawRect(x, curRowTop, w, getHeight());
                }

            }
            if (areFeaturesStacked()) {
                curRowTop += variantBandHeight;
                lastEndX = -1;
            }
        }
    } else {
        tmpRect.height = variantBandHeight;
        tmpRect.y = trackRectangle.y;
        g2D.setColor(Color.gray);
        GraphicUtils.drawCenteredText("No Variants Found", trackRectangle, g2D);
    }

    renderBoundaryLines(g2D, trackRectangle, visibleRectangle);

}

From source file:org.broad.igv.variant.VariantTrack.java

private void renderSamples(Graphics2D g2D, Rectangle visibleRectangle, Variant variant, RenderContext context,
        Rectangle overallSampleRect, int x, int w) {

    Rectangle tmpRect = new Rectangle(overallSampleRect);
    tmpRect.height = getGenotypeBandHeight();
    if (grouped) {
        for (Map.Entry<String, List<String>> entry : samplesByGroups.entrySet()) {
            for (String sample : entry.getValue()) {
                if (overallSampleRect.intersects(visibleRectangle)) {
                    renderer.renderGenotypeBandSNP(variant, context, tmpRect, x, w, sample, coloring,
                            hideFiltered);
                }// ww w  . j av  a  2 s .  c  o  m
                tmpRect.y += tmpRect.height;
            }
        }
    } else {
        for (String sample : allSamples) {
            if (tmpRect.intersects(visibleRectangle)) {
                renderer.renderGenotypeBandSNP(variant, context, tmpRect, x, w, sample, coloring, hideFiltered);
            }
            tmpRect.y += tmpRect.height;
        }
    }
}

From source file:org.broad.igv.variant.VariantTrack.java

/**
 * Render the name panel.//from  www.  j  a va  2  s  .c o m
 * <p/>
 * NOTE:  The sample names are actually drawn in the drawBackground method!
 *
 * @param g2D
 * @param trackRectangle
 * @param visibleRectangle
 */
@Override
public void renderName(Graphics2D g2D, Rectangle trackRectangle, Rectangle visibleRectangle) {

    top = trackRectangle.y;

    Rectangle rect = new Rectangle(trackRectangle);
    g2D.setFont(FontManager.getFont(fontSize));
    g2D.setColor(BAND2_COLOR);

    g2D.setColor(Color.black);
    rect.height = getVariantsHeight();
    if (rect.intersects(visibleRectangle)) {
        GraphicUtils.drawWrappedText(getName(), rect, g2D, false);
    }

    rect.y += rect.height;
    rect.height = getGenotypeBandHeight();
    if (areFeaturesStacked()) {
        // The sample bounds list will get reset when  the names are drawn.
        sampleBounds.clear();
        drawBackground(g2D, rect, visibleRectangle, BackgroundType.NAME);
    }

    renderBoundaryLines(g2D, trackRectangle, visibleRectangle);

}

From source file:org.broad.igv.variant.VariantTrack.java

/**
 * Render sample attributes, if any./*ww w. j  a  v  a2  s . com*/
 *
 * @param g2D
 * @param trackRectangle
 * @param visibleRectangle
 * @param attributeNames
 * @param mouseRegions
 */

public void renderAttributes(Graphics2D g2D, Rectangle trackRectangle, Rectangle visibleRectangle,
        List<String> attributeNames, List<MouseableRegion> mouseRegions) {

    top = trackRectangle.y;
    Rectangle rect = new Rectangle(trackRectangle);

    rect.height = getVariantsHeight();
    if (rect.intersects(visibleRectangle)) {
        super.renderAttributes(g2D, rect, visibleRectangle, attributeNames, mouseRegions);
    }

    if (getDisplayMode() == DisplayMode.COLLAPSED) {
        return;
    }

    rect.y += rect.height;
    rect.height = getGenotypeBandHeight();
    Rectangle bandRectangle = new Rectangle(rect); // Make copy for later use

    drawBackground(g2D, rect, visibleRectangle, BackgroundType.ATTRIBUTE);

    if (grouped) {
        for (List<String> sampleList : samplesByGroups.values()) {
            renderAttributeBand(g2D, bandRectangle, visibleRectangle, attributeNames, sampleList, mouseRegions);
            bandRectangle.y += GROUP_BORDER_WIDTH;

        }
    } else {
        renderAttributeBand(g2D, bandRectangle, visibleRectangle, attributeNames, allSamples, mouseRegions);

    }

    renderBoundaryLines(g2D, trackRectangle, visibleRectangle);

}

From source file:org.broad.igv.variant.VariantTrack.java

/**
 * Render attributes for a sample.   This is mostly a copy of AbstractTrack.renderAttributes().
 * TODO -- refactor to eliminate duplicate code from AbstractTrack
 *
 * @param g2D//from w ww  .j a v a  2  s  .c  o  m
 * @param bandRectangle
 * @param visibleRectangle
 * @param attributeNames
 * @param sampleList
 * @param mouseRegions
 * @return
 */
private void renderAttributeBand(Graphics2D g2D, Rectangle bandRectangle, Rectangle visibleRectangle,
        List<String> attributeNames, List<String> sampleList, List<MouseableRegion> mouseRegions) {

    for (String sample : sampleList) {

        if (bandRectangle.intersects(visibleRectangle)) {

            int x = bandRectangle.x;

            for (String name : attributeNames) {

                String key = name.toUpperCase();
                String attributeValue = AttributeManager.getInstance().getAttribute(sample, key);
                if (attributeValue != null) {
                    Rectangle rect = new Rectangle(x, bandRectangle.y,
                            AttributeHeaderPanel.ATTRIBUTE_COLUMN_WIDTH, bandRectangle.height);
                    g2D.setColor(AttributeManager.getInstance().getColor(key, attributeValue));
                    g2D.fill(rect);
                    mouseRegions.add(new MouseableRegion(rect, key, attributeValue));
                }
                x += AttributeHeaderPanel.ATTRIBUTE_COLUMN_WIDTH + AttributeHeaderPanel.COLUMN_BORDER_WIDTH;
            }

        }
        bandRectangle.y += bandRectangle.height;

    }
}

From source file:org.broad.igv.variant.VariantTrack.java

private boolean colorBand(Graphics2D g2D, Rectangle bandRectangle, Rectangle visibleRectangle,
        boolean coloredLast, Rectangle textRectangle, List<String> sampleList, BackgroundType type) {

    boolean supressFill = (getDisplayMode() == DisplayMode.SQUISHED && squishedHeight < 4);

    for (String sample : sampleList) {

        if (coloredLast) {
            g2D.setColor(BAND1_COLOR);/*from   ww  w.j av  a  2s  .c o m*/
            coloredLast = false;
        } else {
            g2D.setColor(BAND2_COLOR);
            coloredLast = true;
        }

        if (bandRectangle.intersects(visibleRectangle)) {
            if (!supressFill) {
                if (selectedSamples.contains(sample) && hasAlignmentFiles()) {
                    g2D.setColor(SELECTED_BAND_COLOR);
                }
                g2D.fillRect(bandRectangle.x, bandRectangle.y, bandRectangle.width, bandRectangle.height);
            }

            if (type == BackgroundType.NAME) {
                sampleBounds
                        .add(new SampleBounds(bandRectangle.y, bandRectangle.y + bandRectangle.height, sample));
                if (bandRectangle.height >= 3) {
                    String printName = sample;
                    textRectangle.y = bandRectangle.y + 1;
                    g2D.setColor(Color.black);
                    GraphicUtils.drawWrappedText(printName, bandRectangle, g2D, false);
                }

            } else if (type == BackgroundType.ATTRIBUTE) {

            }
        }
        bandRectangle.y += bandRectangle.height;

    }
    return coloredLast;
}

From source file:org.caleydo.view.bicluster.elem.ClusterElement.java

private boolean isClusterCollision() {
    Vec2f mySize = getSize();//from   w  w  w .  ja v a2  s  .  com
    Vec2f myLoc = getLocation();
    Rectangle myRec = new Rectangle((int) myLoc.x() - 10, (int) myLoc.y() - 10, (int) mySize.x() + 20,
            (int) mySize.y() + 20);
    for (GLElement jGLE : findAllClustersElement()) {
        ClusterElement j = (ClusterElement) jGLE;
        if (j == this)
            continue;
        Vec2f jSize = j.getSize();
        Vec2f jLoc = j.getLocation();
        Rectangle jRec = new Rectangle((int) jLoc.x() - 10, (int) jLoc.y() - 10, (int) jSize.x() + 20,
                (int) jSize.y() + 20);
        if (myRec.intersects(jRec)) {
            return true;
        }
    }
    return false;
}

From source file:org.esa.s1tbx.sar.gpf.geometric.MosaicOp.java

/**
 * Called by the framework in order to compute the stack of tiles for the given target bands.
 * <p>The default implementation throws a runtime exception with the message "not implemented".</p>
 *
 * @param targetTiles     The current tiles to be computed for each target band.
 * @param targetRectangle The area in pixel coordinates to be computed (same for all rasters in <code>targetRasters</code>).
 * @param pm              A progress monitor which should be used to determine computation cancelation requests.
 * @throws OperatorException if an error occurs during computation of the target rasters.
 *///  w  w w.jav a2  s.  com
@Override
public void computeTileStack(Map<Band, Tile> targetTiles, Rectangle targetRectangle, ProgressMonitor pm)
        throws OperatorException {

    try {
        final List<Product> validProducts = new ArrayList<>(sourceProduct.length);

        for (final Product srcProduct : selectedProducts) {
            final Rectangle srcRect = srcRectMap.get(srcProduct);
            if (srcRect == null || !srcRect.intersects(targetRectangle)) {
                continue;
            }
            validProducts.add(srcProduct);
        }

        if (validProducts.isEmpty()) {
            return;
        }

        final GeoPos geoPos = new GeoPos();
        final PixelPos pixelPos = new PixelPos();
        final int minX = targetRectangle.x;
        final int minY = targetRectangle.y;
        final int maxX = targetRectangle.x + targetRectangle.width - 1;
        final int maxY = targetRectangle.y + targetRectangle.height - 1;

        final TileGeoreferencing tileGeoRef = new TileGeoreferencing(targetProduct, minX, minY, maxX - minX + 1,
                maxY - minY + 1);

        final List<PixelPos[]> srcPixelCoords = new ArrayList<>(validProducts.size());
        final int numPixelPos = targetRectangle.width * targetRectangle.height;
        for (Product validProduct : validProducts) {
            srcPixelCoords.add(new PixelPos[numPixelPos]);
        }

        int coordIndex = 0;
        int prodIndex;
        for (int y = minY; y <= maxY; ++y) {
            for (int x = minX; x <= maxX; ++x) {
                tileGeoRef.getGeoPos(x, y, geoPos);

                prodIndex = 0;
                for (final Product srcProduct : validProducts) {
                    srcProduct.getSceneGeoCoding().getPixelPos(geoPos, pixelPos);

                    if (pixelPos.x >= feather && pixelPos.y >= feather
                            && pixelPos.x < srcProduct.getSceneRasterWidth() - feather
                            && pixelPos.y < srcProduct.getSceneRasterHeight() - feather) {

                        srcPixelCoords.get(prodIndex)[coordIndex] = new PixelPos(pixelPos.x, pixelPos.y);
                    } else {
                        srcPixelCoords.get(prodIndex)[coordIndex] = null;
                    }
                    ++prodIndex;
                }
                ++coordIndex;
            }
        }

        final Resampling resampling = ResamplingFactory.createResampling(resamplingMethod);

        if (gradientDomainMosaic) {
            performGradientDomainMosaic(targetTiles, targetRectangle, srcPixelCoords, validProducts, resampling,
                    pm);
            return;
        }

        final List<SourceData> validSourceData = new ArrayList<>(validProducts.size());
        for (final Map.Entry<Band, Tile> bandTileEntry : targetTiles.entrySet()) {
            final String trgBandName = bandTileEntry.getKey().getName();
            validSourceData.clear();

            prodIndex = 0;
            for (final Product srcProduct : validProducts) {
                final Band srcBand = srcProduct.getBand(trgBandName);
                if (srcBand == null) {
                    continue;
                }

                final PixelPos[] pixPos = srcPixelCoords.get(prodIndex);

                final Rectangle sourceRectangle = getBoundingBox(pixPos, feather, feather,
                        srcProduct.getSceneRasterWidth() - feather, srcProduct.getSceneRasterHeight() - feather,
                        4);

                if (sourceRectangle != null) {
                    double min = 0, max = 0, mean = 0, std = 0;
                    if (normalizeByMean) { // get stat values
                        try {
                            final Stx stats = srcBand.getStx(true, ProgressMonitor.NULL);
                            mean = stats.getMean();
                            min = stats.getMinimum();
                            max = stats.getMaximum();
                            std = stats.getStandardDeviation();
                        } catch (Throwable e) {
                            //OperatorUtils.catchOperatorException(getId(), e);
                            normalizeByMean = false; // temporary disable
                        }
                    }

                    try {
                        final Tile srcTile = getSourceTile(srcBand, sourceRectangle);
                        if (srcTile != null) {
                            validSourceData
                                    .add(new SourceData(srcTile, pixPos, resampling, min, max, mean, std));
                        }
                    } catch (Exception e) {
                        SystemUtils.LOG.severe("Mosaic getSourceTile failed " + e.getMessage());
                        //continue
                    }
                }
                ++prodIndex;
            }

            if (!validSourceData.isEmpty()) {
                collocateSourceBand(validSourceData, resampling, bandTileEntry.getValue());
            }
        }
    } catch (Throwable e) {
        OperatorUtils.catchOperatorException(getId(), e);
    } finally {
        pm.done();
    }
}

From source file:org.geotools.coverage.io.util.Utilities.java

/**
 * Evaluates the requested envelope and builds a new adjusted version of it fitting this coverage envelope.
 * /*from ww w. j ava 2 s  .  c om*/
 * <p>
 * While adjusting the requested envelope this methods also compute the source region as a rectangle which is suitable for a successive read
 * operation with {@link ImageIO} to do crop-on-read.
 * 
 * @param originalGridToWorld
 * 
 * @param coordinateReferenceSystem
 * 
 * 
 * @param requestedEnvelope is the envelope we are requested to load.
 * @param sourceRegion represents the area to load in raster space. This parameter cannot be null since it gets filled with whatever the crop
 *        region is depending on the <code>requestedEnvelope</code>.
 * @param requestedDim is the requested region where to load data of the specified envelope.
 * @param readGridToWorld the Grid to world transformation to be used
 * @param wgs84BaseEnvelope2D
 * @return the adjusted requested envelope, empty if no requestedEnvelope has been specified, {@code null} in case the requested envelope does not
 *         intersect the coverage envelope or in case the adjusted requested envelope is covered by a too small raster region (an empty region).
 * 
 * @throws DataSourceException in case something bad occurs
 */
public static GeneralEnvelope evaluateRequestedParams(GridEnvelope originalGridRange, Envelope2D baseEnvelope2D,
        CoordinateReferenceSystem spatialReferenceSystem2D, MathTransform originalGridToWorld,
        GeneralEnvelope requestedEnvelope, Rectangle sourceRegion, Rectangle requestedDim,
        MathTransform2D readGridToWorld, Envelope2D wgs84BaseEnvelope2D) throws DataSourceException {

    GeneralEnvelope adjustedRequestedEnvelope = new GeneralEnvelope(2);
    GeneralGridEnvelope baseGridRange = (GeneralGridEnvelope) originalGridRange;

    try {
        // ////////////////////////////////////////////////////////////////
        //
        // Check if we have something to load by intersecting the
        // requested envelope with the bounds of this data set.
        //
        // ////////////////////////////////////////////////////////////////
        if (requestedEnvelope != null) {
            final GeneralEnvelope requestedEnvelope2D = Utilities.getRequestedEnvelope2D(requestedEnvelope);

            // ////////////////////////////////////////////////////////////
            //
            // INTERSECT ENVELOPES AND CROP Destination REGION
            //
            // ////////////////////////////////////////////////////////////
            adjustedRequestedEnvelope = Utilities.getIntersection(baseEnvelope2D, spatialReferenceSystem2D,
                    requestedEnvelope2D, requestedDim, readGridToWorld, wgs84BaseEnvelope2D);
            if (adjustedRequestedEnvelope == null)
                return null;

            // /////////////////////////////////////////////////////////////////////
            //
            // CROP SOURCE REGION
            //
            // /////////////////////////////////////////////////////////////////////
            sourceRegion.setRect(Utilities.getCropRegion(adjustedRequestedEnvelope,
                    Utilities.getOriginalGridToWorld(originalGridToWorld, PixelInCell.CELL_CORNER)));
            if (sourceRegion.isEmpty()) {
                if (LOGGER.isLoggable(Level.INFO)) {
                    LOGGER.log(Level.INFO, "Too small envelope resulting in empty cropped raster region");
                }
                return null;
                // TODO: Future versions may define a 1x1 rectangle starting
                // from the lower coordinate
            }
            if (!sourceRegion.intersects(baseGridRange.toRectangle()) || sourceRegion.isEmpty())
                throw new DataSourceException("The crop region is invalid.");
            sourceRegion.setRect(sourceRegion.intersection(baseGridRange.toRectangle()));

            if (LOGGER.isLoggable(Level.FINE)) {
                StringBuilder sb = new StringBuilder("Adjusted Requested Envelope = ")
                        .append(adjustedRequestedEnvelope.toString()).append("\n")
                        .append("Requested raster dimension = ").append(requestedDim.toString()).append("\n")
                        .append("Corresponding raster source region = ").append(sourceRegion.toString());
                LOGGER.log(Level.FINE, sb.toString());
            }

        } else {
            // don't use the source region. Set an empty one
            sourceRegion.setBounds(new Rectangle(0, 0, Integer.MIN_VALUE, Integer.MIN_VALUE));
        }
    } catch (TransformException e) {
        throw new DataSourceException("Unable to create a coverage for this source", e);
    } catch (FactoryException e) {
        throw new DataSourceException("Unable to create a coverage for this source", e);
    }
    return adjustedRequestedEnvelope;
}