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:GUIUtils.java

public static Rectangle getScreenBoundsFor(Rectangle rc) {
    final GraphicsDevice[] gds = GraphicsEnvironment.getLocalGraphicsEnvironment().getScreenDevices();
    final List<GraphicsConfiguration> configs = new ArrayList<GraphicsConfiguration>();

    for (int i = 0; i < gds.length; i++) {
        GraphicsConfiguration gc = gds[i].getDefaultConfiguration();
        if (rc.intersects(gc.getBounds())) {
            configs.add(gc);/*from  w  w  w  .  ja v  a  2 s.  c  om*/
        }
    }

    GraphicsConfiguration selected = null;
    if (configs.size() > 0) {
        for (Iterator<GraphicsConfiguration> it = configs.iterator(); it.hasNext();) {
            GraphicsConfiguration gcc = it.next();
            if (selected == null)
                selected = gcc;
            else {
                if (gcc.getBounds().contains(rc.x + 20, rc.y + 20)) {
                    selected = gcc;
                    break;
                }
            }
        }
    } else {
        selected = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice()
                .getDefaultConfiguration();
    }

    int x = selected.getBounds().x;
    int y = selected.getBounds().y;
    int w = selected.getBounds().width;
    int h = selected.getBounds().height;

    return new Rectangle(x, y, w, h);
}

From source file:Main.java

public void paint(Graphics g) {
    Graphics2D g2 = (Graphics2D) g;

    Rectangle r = new Rectangle(10, 10, 50, 40);

    r.intersects(new Rectangle(40, 40, 20, 20));

    g2.fill(r);//  w w  w  .  j  a v a2  s.co  m

}

From source file:Main.java

MyPanel() {
    JScrollPane scrollpane = new JScrollPane();
    scrollpane.setViewport(viewport);//from   w  w w  .  ja va  2 s  .  c o m
    viewport.add(innerPanel);
    scrollpane.setPreferredSize(SCROLLPANE_SIZE);
    viewport.addChangeListener(e -> {
        Rectangle viewRect = viewport.getViewRect();
        if (viewRect.intersects(RECT)) {
            statusLabel.setText(VISIBLE);
        } else {
            statusLabel.setText(NOT_VISIBLE);
        }
    });

    setLayout(new BorderLayout());
    add(scrollpane, BorderLayout.CENTER);
    add(statusLabel, BorderLayout.SOUTH);
}

From source file:edu.umn.cs.spatialHadoop.nasa.StockQuadTree.java

/**
 * Perform a selection query that retrieves all points in the given range.
 * The range is specified in the two-dimensional array positions. 
 * @param in/* w  w w.j  ava2 s . c  o  m*/
 * @param query_mbr
 * @return
 * @throws IOException
 */
public static Node aggregateQuery(FSDataInputStream in, Rectangle query_mbr) throws IOException {
    long treeStartPosition = in.getPos();
    Node result = new Node();
    int numOfSelectedRecords = 0;
    int resolution = in.readInt();
    short fillValue = in.readShort();
    int cardinality = in.readInt();
    final Vector<Integer> selectedNodesPos = new Vector<Integer>();
    final Vector<Integer> selectedStarts = new Vector<Integer>();
    final Vector<Integer> selectedEnds = new Vector<Integer>();
    StockQuadTree stockQuadTree = getOrCreateStockQuadTree(resolution);
    // Nodes to be searched. Contains node positions in the array of nodes
    Stack<Integer> nodes_2b_searched = new Stack<Integer>();
    nodes_2b_searched.add(0); // Root node (ID=1)
    Rectangle node_mbr = new Rectangle();
    while (!nodes_2b_searched.isEmpty()) {
        int node_pos = nodes_2b_searched.pop();
        stockQuadTree.getNodeMBR(node_pos, node_mbr);
        if (query_mbr.contains(node_mbr)) {
            // Add this node to the selection list and stop this branch
            selectedNodesPos.add(node_pos);
        } else if (query_mbr.intersects(node_mbr)) {
            int first_child_id = stockQuadTree.nodesID[node_pos] * 4 + 0;
            int first_child_pos = Arrays.binarySearch(stockQuadTree.nodesID, first_child_id);
            if (first_child_pos < 0) {
                // No children. Hit a leaf node
                // Scan and add matching points only
                java.awt.Point record_coords = new Point();
                for (int record_pos = stockQuadTree.nodesStartPosition[node_pos]; record_pos < stockQuadTree.nodesEndPosition[node_pos]; record_pos++) {
                    stockQuadTree.getRecordCoords(record_pos, record_coords);
                    if (query_mbr.contains(record_coords)) {
                        // matched a record.
                        if (!selectedEnds.isEmpty() && selectedEnds.lastElement() == record_pos) {
                            // Merge with an adjacent range
                            selectedEnds.set(selectedEnds.size() - 1, record_pos + 1);
                        } else {
                            // Add a new range of unit width
                            selectedStarts.add(record_pos);
                            selectedEnds.add(record_pos + 1);
                        }
                        numOfSelectedRecords++;
                    }
                }
            } else {
                // Non-leaf node. Add all children to the list of nodes to search
                // Add in reverse order to the stack so that results come in sorted order
                nodes_2b_searched.add(first_child_pos + 3);
                nodes_2b_searched.add(first_child_pos + 2);
                nodes_2b_searched.add(first_child_pos + 1);
                nodes_2b_searched.add(first_child_pos + 0);
            }
        }
    }
    // Result 1: Accumulate all values
    // Sort disk offsets to eliminate backward seeks
    if (!selectedStarts.isEmpty()) {
        LOG.debug("Aggregate query selected " + selectedNodesPos.size() + " nodes and " + numOfSelectedRecords
                + " records");

        final IndexedSortable sortable = new IndexedSortable() {
            @Override
            public int compare(int i, int j) {
                return selectedStarts.get(i) - selectedStarts.get(j);
            }

            @Override
            public void swap(int i, int j) {
                int temp = selectedStarts.get(i);
                selectedStarts.set(i, selectedStarts.get(j));
                selectedStarts.set(j, temp);

                temp = selectedEnds.get(i);
                selectedEnds.set(i, selectedEnds.get(j));
                selectedEnds.set(j, temp);
            }
        };
        new QuickSort().sort(sortable, 0, selectedStarts.size());

        long dataStartPosition = getValuesStartOffset(cardinality);
        Point resultCoords = new Point();
        // Return all values in the selected ranges
        for (int iRange = 0; iRange < selectedStarts.size(); iRange++) {
            int treeStart = selectedStarts.get(iRange);
            int treeEnd = selectedEnds.get(iRange);
            long startPosition = dataStartPosition + selectedStarts.get(iRange) * cardinality * 2;
            in.seek(startPosition);
            for (int treePos = treeStart; treePos < treeEnd; treePos++) {
                // Retrieve the coords for the point at treePos
                stockQuadTree.getRecordCoords(treePos, resultCoords);
                // Read all entries at current position
                for (int iValue = 0; iValue < cardinality; iValue++) {
                    short value = in.readShort();
                    if (value != fillValue)
                        result.accumulate(value);
                }
            }
        }

    }

    // Result 2: Accumulate all nodes
    if (!selectedNodesPos.isEmpty()) {
        long nodesStartPosition = treeStartPosition + getNodesStartOffset(resolution, cardinality);
        // Sort node positions to eliminate backward seeks
        IndexedSortable nodeSortable = new IndexedSortable() {
            @Override
            public int compare(int i, int j) {
                return selectedNodesPos.get(i) - selectedNodesPos.get(j);
            }

            @Override
            public void swap(int i, int j) {
                int temp = selectedNodesPos.get(i);
                selectedNodesPos.set(i, selectedNodesPos.get(j));
                selectedNodesPos.set(j, temp);
            }
        };
        new QuickSort().sort(nodeSortable, 0, selectedNodesPos.size());

        Node selectedNode = new Node();
        for (int node_pos : selectedNodesPos) {
            long nodePosition = nodesStartPosition + node_pos * NodeSize;
            in.seek(nodePosition);
            selectedNode.readFields(in);
            result.accumulate(selectedNode);
        }
    }
    return result;
}

From source file:org.eurocarbdb.application.glycoworkbench.plugin.reporting.AnnotationReportCanvas.java

public Collection<AnnotationObject> getAnnotationsInside(Rectangle r) {
    Vector<AnnotationObject> ret = new Vector<AnnotationObject>();
    if (r != null) {
        for (Map.Entry<AnnotationObject, Rectangle> e : rectangles_complete.entrySet()) {
            if (r.intersects(e.getValue()))
                ret.add(e.getKey());/*  w  w  w.jav  a  2 s  .c  om*/
        }
    }
    return ret;
}

From source file:com.projity.pm.graphic.network.NetworkRenderer.java

public void paint(Graphics g, Rectangle visibleBounds) {
    Graphics2D g2 = (Graphics2D) g;

    Rectangle clipBounds = g2.getClipBounds();
    Rectangle svgClip = clipBounds;
    if (clipBounds == null) {
        clipBounds = getGraphInfo().getDrawingBounds();
        //start at O,O because it's already translated
        if (visibleBounds == null)
            clipBounds = new Rectangle(0, 0, clipBounds.width, clipBounds.height);
        else {//w ww. j a  v  a2  s . c om
            clipBounds = visibleBounds;
            g2.setClip(clipBounds);
        }
    }
    //Modif for offline graphics

    GraphicDependency dependency;
    for (Iterator i = getDependenciesIterator(); i.hasNext();) {
        dependency = (GraphicDependency) i.next();
        paintLink(g2, dependency);
    }

    GraphicNode node;
    Rectangle bounds;
    for (ListIterator i = graphInfo.getCache().getIterator(); i.hasNext();) {
        node = (GraphicNode) i.next();
        bounds = getBounds(node);
        if (bounds == null)
            continue;
        if (clipBounds.intersects(bounds))
            paintNode(g2, node);
    }

    if (visibleBounds != null)
        g2.setClip(svgClip);
}

From source file:org.yccheok.jstock.gui.charting.InvestmentFlowLayerUI.java

private void solveConflict() {
    if (this.investPoint == null || this.ROIPoint == null) {
        /* No conflict to be solved. */
        return;//from  w  ww  .  j  a v  a  2s.c  o  m
    }

    /* Take border into consideration. */
    final Rectangle ROIRectWithBorder = new Rectangle((Rectangle) this.ROIRect);
    final Rectangle investRectWithBorder = new Rectangle((Rectangle) this.investRect);
    ROIRectWithBorder.setLocation((int) ROIRectWithBorder.getX() - 1, (int) ROIRectWithBorder.getY() - 1);
    ROIRectWithBorder.setSize((int) ROIRectWithBorder.getWidth() + 2, (int) ROIRectWithBorder.getHeight() + 2);
    investRectWithBorder.setLocation((int) investRectWithBorder.getX() - 1,
            (int) investRectWithBorder.getY() - 1);
    investRectWithBorder.setSize((int) investRectWithBorder.getWidth() + 2,
            (int) investRectWithBorder.getHeight() + 2);
    if (false == ROIRectWithBorder.intersects(investRectWithBorder)) {
        return;
    }

    final Rectangle oldROIRect = new Rectangle((Rectangle) this.ROIRect);
    final Rectangle oldInvestRect = new Rectangle((Rectangle) this.investRect);

    // Move to Down.
    if (this.ROIRect.getY() > this.investRect.getY()) {
        ((Rectangle) this.ROIRect).translate(0,
                (int) (this.investRect.getY() + this.investRect.getHeight() - this.ROIRect.getY() + 4));
    } else {
        ((Rectangle) this.investRect).translate(0,
                (int) (this.ROIRect.getY() + this.ROIRect.getHeight() - this.investRect.getY() + 4));
    }

    if ((this.drawArea.getY() + this.drawArea.getHeight()) > (this.ROIRect.getY() + this.ROIRect.getHeight())
            && (this.drawArea.getY() + this.drawArea.getHeight()) > (this.investRect.getY()
                    + this.investRect.getHeight())) {
        return;
    }

    this.ROIRect.setRect(oldROIRect);
    this.investRect.setRect(oldInvestRect);

    // Move to Up.
    if (this.ROIRect.getY() > this.investRect.getY()) {
        ((Rectangle) this.investRect).translate(0,
                -(int) (this.investRect.getY() + this.investRect.getHeight() - this.ROIRect.getY() + 4));
    } else {
        ((Rectangle) this.ROIRect).translate(0,
                -(int) (this.ROIRect.getY() + this.ROIRect.getHeight() - this.investRect.getY() + 4));
    }

    if ((this.drawArea.getY() < this.ROIRect.getY()) && (this.drawArea.getY() < this.investRect.getY())) {
        return;
    }

    this.ROIRect.setRect(oldROIRect);
    this.investRect.setRect(oldInvestRect);
}

From source file:edu.umn.cs.spatialHadoop.nasa.StockQuadTree.java

/**
 * Perform a selection query that retrieves all points in the given range.
 * The range is specified in the two-dimensional array positions.
 * @param in/*from ww w  . j av a2 s  .c om*/
 * @param query_mbr
 * @param output
 * @return number of matched records
 * @throws IOException
 */
public static int selectionQuery(FSDataInputStream in, Rectangle query_mbr, ResultCollector<PointValue> output)
        throws IOException {
    long treeStartPosition = in.getPos();
    int numOfResults = 0;
    int resolution = in.readInt();
    short fillValue = in.readShort();
    int cardinality = in.readInt();
    long[] timestamps = new long[cardinality];
    for (int i = 0; i < cardinality; i++)
        timestamps[i] = in.readLong();
    Vector<Integer> selectedStarts = new Vector<Integer>();
    Vector<Integer> selectedEnds = new Vector<Integer>();
    StockQuadTree stockQuadTree = getOrCreateStockQuadTree(resolution);
    // Nodes to be searched. Contains node positions in the array of nodes
    Stack<Integer> nodes_2b_searched = new Stack<Integer>();
    nodes_2b_searched.add(0); // Root node (ID=1)
    Rectangle node_mbr = new Rectangle();
    while (!nodes_2b_searched.isEmpty()) {
        int node_pos = nodes_2b_searched.pop();
        stockQuadTree.getNodeMBR(node_pos, node_mbr);
        if (query_mbr.contains(node_mbr)) {
            // Add this node to the selection list and stop this branch
            if (!selectedEnds.isEmpty()
                    && selectedEnds.lastElement() == stockQuadTree.nodesStartPosition[node_pos]) {
                // Merge with an adjacent range
                selectedEnds.set(selectedEnds.size() - 1, stockQuadTree.nodesEndPosition[node_pos]);
            } else {
                // add a new range
                selectedStarts.add(stockQuadTree.nodesStartPosition[node_pos]);
                selectedEnds.add(stockQuadTree.nodesEndPosition[node_pos]);
            }
            numOfResults += stockQuadTree.nodesEndPosition[node_pos]
                    - stockQuadTree.nodesStartPosition[node_pos];
        } else if (query_mbr.intersects(node_mbr)) {
            int first_child_id = stockQuadTree.nodesID[node_pos] * 4 + 0;
            int first_child_pos = Arrays.binarySearch(stockQuadTree.nodesID, first_child_id);
            if (first_child_pos < 0) {
                // No children. Hit a leaf node
                // Scan and add matching points only
                java.awt.Point record_coords = new Point();
                for (int record_pos = stockQuadTree.nodesStartPosition[node_pos]; record_pos < stockQuadTree.nodesEndPosition[node_pos]; record_pos++) {
                    stockQuadTree.getRecordCoords(record_pos, record_coords);
                    if (query_mbr.contains(record_coords)) {
                        // matched a record.
                        if (!selectedEnds.isEmpty() && selectedEnds.lastElement() == record_pos) {
                            // Merge with an adjacent range
                            selectedEnds.set(selectedEnds.size() - 1, record_pos + 1);
                        } else {
                            // Add a new range of unit width
                            selectedStarts.add(record_pos);
                            selectedEnds.add(record_pos + 1);
                        }
                        numOfResults++;
                    }
                }
            } else {
                // Non-leaf node. Add all children to the list of nodes to search
                // Add in reverse order to the stack so that results come in sorted order
                nodes_2b_searched.add(first_child_pos + 3);
                nodes_2b_searched.add(first_child_pos + 2);
                nodes_2b_searched.add(first_child_pos + 1);
                nodes_2b_searched.add(first_child_pos + 0);
            }
        }
    }
    if (output != null) {
        PointValue returnValue = new PointValue();
        long dataStartPosition = treeStartPosition + getValuesStartOffset(cardinality);
        // Return all values in the selected ranges
        for (int iRange = 0; iRange < selectedStarts.size(); iRange++) {
            int treeStart = selectedStarts.get(iRange);
            int treeEnd = selectedEnds.get(iRange);
            long startPosition = dataStartPosition + selectedStarts.get(iRange) * cardinality * 2;
            in.seek(startPosition);
            for (int treePos = treeStart; treePos < treeEnd; treePos++) {
                // Retrieve the coords for the point at treePos
                stockQuadTree.getRecordCoords(treePos, returnValue);
                // Read all entries at current position
                for (int iValue = 0; iValue < cardinality; iValue++) {
                    short value = in.readShort();
                    if (value != fillValue) {
                        returnValue.value = value;
                        returnValue.timestamp = timestamps[iValue];
                        output.collect(returnValue);
                    }
                }
            }
        }
    }
    return numOfResults;
}

From source file:RectListManager.java

public void subtract(RectListManager rlm, int overhead, int lineOverhead) {
    Rectangle r, sr;
    int cost;//from   www.  j  a  v a  2s.  c  o  m
    int jMin = 0;
    Rectangle[] splits = new Rectangle[4];

    for (int i = 0; i < size; i++) {
        r = rects[i]; // Canidate rect...
        cost = (overhead + (r.height * lineOverhead) + (r.height * r.width));
        for (int j = jMin; j < rlm.size; j++) {
            sr = rlm.rects[j]; // subtraction rect.

            // Check if the canidate rect starts after
            // the end of this rect in 'x' if so
            // go to the next one.
            if (sr.x + sr.width < r.x) {
                // If this was jMin then increment jMin (no
                // future canidate rect will intersect this rect).
                if (j == jMin)
                    jMin++;
                continue;
            }

            // Check if the rest of the rects from rlm are past
            // the end of the canidate rect.  If so we are
            // done with this canidate rect.
            if (sr.x > r.x + r.width)
                break;

            // If they don't insersect then go to next sub rect.
            if (!r.intersects(sr))
                continue;

            // Now we know they intersect one another lets
            // figure out how...

            splitRect(r, sr, splits);

            int splitCost = 0;
            Rectangle tmpR;
            for (int k = 0; k < 4; k++) {
                tmpR = splits[k];
                if (tmpR != null)
                    splitCost += (overhead + (tmpR.height * lineOverhead) + (tmpR.height * tmpR.width));
            }

            if (splitCost >= cost)
                // This isn't ideal as depending on the order
                // Stuff is done in we might later kill some of
                // these rectangles (hence lowering the cost).
                // For this reason it is probably best of the
                // subtract list has been merged as this will help
                // reduce the instances where this will happen.
                continue;

            // Collapse null entries in first three elements
            // split 0, 1, 2 (entries that share a common 'x').
            int l = 0;
            for (int k = 0; k < 3; k++) {
                if (splits[k] != null)
                    splits[l++] = splits[k];
            }

            // Fully covered (or only split 3 survived which we
            // will visit later) this canidate rect goes away.
            if (l == 0) {
                rects[i].width = 0;
                // Insert the third split (if any) at the
                // proper place in rects list.
                if (splits[3] != null)
                    add(splits[3], i, size - 1);
                break;
            }

            // Otherwise replace the canidate with the top of
            // the split, since it only shrunk it didn't grow,
            // we know that the previous subtract rects don't
            // intersect it.
            r = splits[0];
            rects[i] = r;
            cost = (overhead + (r.height * lineOverhead) + (r.height * r.width));

            // Add the remainder of the rects that
            // share 'r.x' (if any).  Possible
            // are split 1, and split 2.
            if (l > 1)
                insertRects(splits, 1, i + 1, l - 1);

            // Insert the third split (if any) at the
            // proper place in rects list.
            if (splits[3] != null)
                add(splits[3], i + l, size - 1);
        }
    }

    // Now we will go through collapsing the nulled entries.
    int j = 0, i = 0;
    while (i < size) {
        if (rects[i].width == 0)
            rects[i] = null;
        else
            rects[j++] = rects[i];
        i++;
    }
    size = j;
    bounds = null;
}

From source file:RectListManager.java

public void mergeRects(int overhead, int lineOverhead) {
    if (size == 0)
        return;//from  w ww  .ja  va  2 s  . com
    Rectangle r, cr, mr;
    int cost1, cost2, cost3;
    mr = new Rectangle();
    Rectangle[] splits = new Rectangle[4];
    for (int j, i = 0; i < size; i++) {
        r = rects[i];
        if (r == null)
            continue;
        cost1 = (overhead + (r.height * lineOverhead) + (r.height * r.width));
        do {
            int maxX = r.x + r.width + overhead / r.height;
            for (j = i + 1; j < size; j++) {
                cr = rects[j];
                if ((cr == null) || (cr == r))
                    continue;
                if (cr.x >= maxX) {
                    // No more merges can happen.
                    j = size;
                    break;
                }
                cost2 = (overhead + (cr.height * lineOverhead) + (cr.height * cr.width));

                mr = r.union(cr);
                cost3 = (overhead + (mr.height * lineOverhead) + (mr.height * mr.width));
                if (cost3 <= cost1 + cost2) {
                    r = rects[i] = mr;
                    rects[j] = null;
                    cost1 = cost3;
                    j = -1;
                    break;
                }

                if (!r.intersects(cr))
                    continue;

                splitRect(cr, r, splits);
                int splitCost = 0;
                int l = 0;
                for (int k = 0; k < 4; k++) {
                    if (splits[k] != null) {
                        Rectangle sr = splits[k];
                        // Collapse null entries in first three
                        // (That share common 'x').
                        if (k < 3)
                            splits[l++] = sr;
                        splitCost += (overhead + (sr.height * lineOverhead) + (sr.height * sr.width));
                    }
                }
                if (splitCost >= cost2)
                    continue;

                // Insert the splits.
                if (l == 0) {
                    // only third split may be left (no common 'x').
                    rects[j] = null;
                    if (splits[3] != null)
                        add(splits[3], j, size - 1);
                    continue;
                }

                rects[j] = splits[0];
                if (l > 1)
                    insertRects(splits, 1, j + 1, l - 1);
                if (splits[3] != null)
                    add(splits[3], j, size - 1);
            }

            // if we merged it with another rect then
            // we need to check all the rects up to i again,
            // against the merged rect.
        } while (j != size);
    }

    // Now we will go through collapsing the nulled entries.
    int j = 0, i = 0;
    float area = 0;
    while (i < size) {
        if (rects[i] != null) {
            r = rects[i];
            rects[j++] = r;
            area += overhead + (r.height * lineOverhead) + (r.height * r.width);
        }
        i++;
    }
    size = j;
    bounds = null;
    r = getBounds();
    if (r == null)
        return;
    if (overhead + (r.height * lineOverhead) + (r.height * r.width) < area) {
        rects[0] = r;
        size = 1;
    }
}