Example usage for java.awt Shape contains

List of usage examples for java.awt Shape contains

Introduction

In this page you can find the example usage for java.awt Shape contains.

Prototype

public boolean contains(Rectangle2D r);

Source Link

Document

Tests if the interior of the Shape entirely contains the specified Rectangle2D .

Usage

From source file:Main.java

public static void main(String[] args) throws Exception {
    Shape s = new Rectangle2D.Double(0, 0, 72, 72);

    System.out.println(s.contains(new Rectangle(30, 40, 30, 30)));
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    Shape s = new Rectangle2D.Double(0, 0, 72, 72);

    System.out.println(s.contains(new Point(30, 40)));
}

From source file:edu.uci.ics.jung.visualization.picking.ShapePickSupport.java

/**
 * Returns the vertices whose layout coordinates are contained in 
 * <code>Shape</code>.//from w w  w.  j a  va  2s . c  om
 * The shape is in screen coordinates, and the graph vertices
 * are transformed to screen coordinates before they are tested
 * for inclusion.
 * @return the <code>Collection</code> of vertices whose <code>layout</code>
 * coordinates are contained in <code>shape</code>.
 */
public Collection<V> getVertices(Layout<V, E> layout, Shape shape) {
    Set<V> pickedVertices = new HashSet<V>();

    // remove the view transform from the rectangle
    shape = vv.getRenderContext().getMultiLayerTransformer().inverseTransform(Layer.VIEW, shape);

    while (true) {
        try {
            for (V v : getFilteredVertices(layout)) {
                Point2D p = layout.transform(v);
                if (p == null)
                    continue;

                p = vv.getRenderContext().getMultiLayerTransformer().transform(Layer.LAYOUT, p);
                if (shape.contains(p)) {
                    pickedVertices.add(v);
                }
            }
            break;
        } catch (ConcurrentModificationException cme) {
        }
    }
    return pickedVertices;
}

From source file:edu.uci.ics.jung.visualization.PluggableRenderer.java

/**
 * Passed Line's point2 must be inside the passed shape or
 * an IllegalArgumentException is thrown
 * @param line line to subdivide//from   ww  w.  j  a va  2s . c  o  m
 * @param shape shape to compare with line
 * @return a line that intersects the shape boundary
 * @throws IllegalArgumentException if the passed line's point1 is not inside the shape
 */
protected Line2D getLastOutsideSegment(Line2D line, Shape shape) {
    if (shape.contains(line.getP2()) == false) {
        String errorString = "line end point: " + line.getP2() + " is not contained in shape: "
                + shape.getBounds2D();
        throw new IllegalArgumentException(errorString);
        //return null;
    }
    Line2D left = new Line2D.Double();
    Line2D right = new Line2D.Double();
    // subdivide the line until its left segment intersects
    // the shape boundary
    int iterations = 0;
    do {
        subdivide(line, left, right);
        line = right;
    } while (shape.contains(line.getP1()) == false && iterations++ < MAX_ITERATIONS);
    // now that right is completely inside shape,
    // return left, which must be partially outside
    return left;
}

From source file:edu.uci.ics.jung.visualization.PluggableRenderer.java

/**
 * Passed Line's point1 must be inside the passed shape or
 * an IllegalArgumentException is thrown
 * @param line line to subdivide/*from  www.jav  a2s  .c om*/
 * @param shape shape to compare with line
 * @return a line that intersects the shape boundary
 * @throws IllegalArgumentException if the passed line's point1 is not inside the shape
 */
protected Line2D getFirstOutsideSegment(Line2D line, Shape shape) {

    if (shape.contains(line.getP1()) == false) {
        String errorString = "line start point: " + line.getP1() + " is not contained in shape: "
                + shape.getBounds2D();
        throw new IllegalArgumentException(errorString);
    }
    Line2D left = new Line2D.Float();
    Line2D right = new Line2D.Float();
    // subdivide the line until its right side intersects the
    // shape boundary
    do {
        subdivide(line, left, right);
        line = left;
    } while (shape.contains(line.getP2()) == false);
    // now that left is completely inside shape,
    // return right, which must be partially outside
    return right;
}

From source file:edu.uci.ics.jung.visualization.PluggableRenderer.java

/**
 * <p>Returns a transform to position the arrowhead on this edge shape at the
 * point where it intersects the passed vertex shape.</p>
 * /*from  www.j av  a2  s.com*/
 * <p>The Loop edge is a special case because its staring point is not inside
 * the vertex. The passedGo flag handles this case.</p>
 * 
 * @param edgeShape
 * @param vertexShape
 * @param passedGo - used only for Loop edges
 */
public AffineTransform getReverseArrowTransform(GeneralPath edgeShape, Shape vertexShape, boolean passedGo) {
    float[] seg = new float[6];
    Point2D p1 = null;
    Point2D p2 = null;

    AffineTransform at = new AffineTransform();
    for (PathIterator i = edgeShape.getPathIterator(null, 1); !i.isDone(); i.next()) {
        int ret = i.currentSegment(seg);
        if (ret == PathIterator.SEG_MOVETO) {
            p2 = new Point2D.Float(seg[0], seg[1]);
        } else if (ret == PathIterator.SEG_LINETO) {
            p1 = p2;
            p2 = new Point2D.Float(seg[0], seg[1]);
            if (passedGo == false && vertexShape.contains(p2)) {
                passedGo = true;
            } else if (passedGo == true && vertexShape.contains(p2) == false) {
                at = getReverseArrowTransform(new Line2D.Float(p1, p2), vertexShape);
                break;
            }
        }
    }
    return at;
}

From source file:edu.uci.ics.jung.visualization.PluggableRenderer.java

/**
 * Returns a transform to position the arrowhead on this edge shape at the
 * point where it intersects the passed vertex shape.
 *//*w ww  .  j  a  v  a2s  .  c om*/
public AffineTransform getArrowTransform(GeneralPath edgeShape, Shape vertexShape) {
    float[] seg = new float[6];
    Point2D p1 = null;
    Point2D p2 = null;
    AffineTransform at = new AffineTransform();
    // when the PathIterator is done, switch to the line-subdivide
    // method to get the arrowhead closer.
    for (PathIterator i = edgeShape.getPathIterator(null, 1); !i.isDone(); i.next()) {
        int ret = i.currentSegment(seg);
        if (ret == PathIterator.SEG_MOVETO) {
            p2 = new Point2D.Float(seg[0], seg[1]);
        } else if (ret == PathIterator.SEG_LINETO) {
            p1 = p2;
            p2 = new Point2D.Float(seg[0], seg[1]);
            if (vertexShape.contains(p2)) {
                at = getArrowTransform(new Line2D.Float(p1, p2), vertexShape);
                break;
            }
        }
    }
    return at;
}

From source file:org.dwfa.ace.graph.AceGraphRenderer.java

/**
 * Passed Line's point2 must be inside the passed shape or
 * an IllegalArgumentException is thrown
 * // ww w  .j a v  a 2 s  .co  m
 * @param line line to subdivide
 * @param shape shape to compare with line
 * @return a line that intersects the shape boundary
 * @throws IllegalArgumentException if the passed line's point1 is not
 *             inside the shape
 */
protected Line2D getLastOutsideSegment(Line2D line, Shape shape) {
    if (shape.contains(line.getP2()) == false) {
        String errorString = "line end point: " + line.getP2() + " is not contained in shape: "
                + shape.getBounds2D();
        throw new IllegalArgumentException(errorString);
        // return null;
    }
    Line2D left = new Line2D.Double();
    Line2D right = new Line2D.Double();
    // subdivide the line until its left segment intersects
    // the shape boundary
    int iterations = 0;
    do {
        subdivide(line, left, right);
        line = right;
    } while (shape.contains(line.getP1()) == false && iterations++ < MAX_ITERATIONS);
    // now that right is completely inside shape,
    // return left, which must be partially outside
    return left;
}

From source file:org.squidy.designer.shape.ZoomShape.java

/**
 * @param goalDirectedZoom/*  ww w.  java2 s  .  co  m*/
 */
public ZoomShape(boolean goalDirectedZoom) {
    setBounds(Constants.DEFAULT_NODE_BOUNDS);

    if (goalDirectedZoom) {
        addInputEventListener(new PBasicInputEventHandler() {

            @Override
            public void mouseClicked(PInputEvent event) {
                if (!event.isHandled() && event.isLeftMouseButton() && event.getClickCount() == 2) {

                    PNode store = event.getPickedNode();
                    while (store != null && !(store instanceof IModelStore)) {
                        store = store.getParent();
                    }

                    PNode node = event.getPickedNode();
                    while (node != null && !(node instanceof ZoomShape<?>)) {
                        node = node.getParent();
                    }

                    if (node instanceof ZoomShape<?>) {

                        //                     if (LOG.isDebugEnabled()) {
                        //                         LOG.debug("Selected node to zoom is " + node + " and this is " + ZoomShape.this);
                        //                     }

                        node.moveToFront();

                        //                     PBounds boundsView = event.getCamera()
                        //                           .getViewBounds();
                        //                     PBounds boundsNode = ((ZoomShape<?>) node)
                        //                           .getGlobalBoundsZoomedIn();
                        //                     boolean sameNode = (Math.abs(boundsView.x
                        //                           - boundsNode.x) < 0.1 && Math
                        //                           .abs(boundsView.width - boundsNode.width) < 0.1)
                        //                           || (Math.abs(boundsView.y - boundsNode.y) < 0.1 && Math
                        //                                 .abs(boundsView.height
                        //                                       - boundsNode.height) < 0.1);
                        //
                        //                     if (!sameNode) {
                        if (store != null) {
                            ((IModelStore) store).getModel().setZoomedShape((ZoomShape<?>) node);
                            ((IModelStore) store).getModel().setZoomedBounds(getGlobalBoundsZoomedIn());
                        }
                        animateToCenterView(event.getCamera());
                        //                     }

                        event.setHandled(true);
                    }
                }
            }
        });
    }

    if (this instanceof Draggable) {
        addInputEventListener(new PDragEventHandler() {

            private PNode draggable;

            /*
             * (non-Javadoc)
             * 
             * @seeedu.umd.cs.piccolo.event.PDragEventHandler#
             * shouldStartDragInteraction
             * (edu.umd.cs.piccolo.event.PInputEvent)
             */
            @Override
            protected boolean shouldStartDragInteraction(PInputEvent event) {
                if (!event.isHandled()) {
                    PNode node = event.getPickedNode();
                    while (node != null && !(node instanceof Draggable)) {
                        node = node.getParent();
                    }
                    // Set dragged node to allow drag transformation.
                    draggable = node;
                    if (node instanceof Draggable) {
                        if (((Draggable) node).isDraggable() && super.shouldStartDragInteraction(event)) {
                            return true;
                        }
                    }
                    return false;
                }
                return false;
            }

            /*
             * (non-Javadoc)
             * 
             * @see
             * edu.umd.cs.piccolo.event.PDragEventHandler#drag(edu.umd.cs
             * .piccolo .event.PInputEvent)
             */
            @Override
            protected void drag(PInputEvent event) {
                if (!event.isHandled()) {
                    if (!event.getPath().acceptsNode(draggable)) {
                        if (LOG.isDebugEnabled()) {
                            LOG.debug("Pick path doesn't accept node " + draggable.getClass().getName() + ".");
                        }
                        return;
                    }

                    PNode parent = draggable.getParent();

                    PDimension d = event.getDeltaRelativeTo(draggable);
                    draggable.localToParent(d);

                    PBounds parentBounds = parent.getBoundsReference();
                    Rectangle2D draggableBounds = draggable
                            .localToParent(draggable.getBounds().moveBy(d.getWidth(), d.getHeight()));

                    if (parentBounds.contains(draggableBounds)) {
                        draggable.offset(d.getWidth(), d.getHeight());
                    }

                    Point2D offset = draggable.getOffset();
                    if (offset.getX() < 0) {
                        draggable.setOffset(0, offset.getY());
                    }
                    if (offset.getY() < 0) {
                        draggable.setOffset(offset.getX(), 0);
                    }
                    if (offset.getX() > parentBounds.getWidth() - draggableBounds.getWidth()) {
                        draggable.setOffset(parentBounds.getWidth() - draggableBounds.getWidth(),
                                offset.getY());
                    }
                    if (offset.getY() > parentBounds.getHeight() - draggableBounds.getHeight()) {
                        draggable.setOffset(offset.getX(),
                                parentBounds.getHeight() - draggableBounds.getHeight());
                    }

                    event.setHandled(true);
                }

                // if (!event.isHandled()) {
                // if (!event.getPath().acceptsNode(draggable)) {
                // if (LOG.isDebugEnabled()) {
                // LOG.debug("Pick path doesn't accept node " +
                // draggable.getClass().getName() + ".");
                // }
                // return;
                // }
                //
                // Point2D current =
                // event.getPositionRelativeTo(ZoomShape.this);
                // draggable.localToParent(current);
                //                  
                // Point2D dest = new Point2D.Double();
                //                  
                // dest.setLocation((current.getX()), (current.getY()));
                //            
                // dest.setLocation(dest.getX() - (dest.getX() % 20),
                // dest.getY() - (dest.getY() % 20));
                //                  
                // // dest.setLocation(nodeStartPosition.getX() - (d.getX()
                // % 20), nodeStartPosition.getY() - (d.getY() % 20));
                //                  
                // System.out.println("OFFSET: " + dest);
                //                  
                // draggable.setOffset(dest.getX(), dest.getY());
                //
                // // }
                // event.setHandled(true);
                // }
            }
        });
    }

    MultiSelectionHandler multiSelectionHandler = new MultiSelectionHandler() {

        /* (non-Javadoc)
         * @see org.squidy.designer.event.MultiSelectionHandler#selectionAllowed(edu.umd.cs.piccolo.event.PInputEvent)
         */
        @Override
        protected boolean selectionAllowed(PInputEvent event) {
            PNode node = event.getPickedNode();
            return node instanceof Draggable && !((Draggable) node).isDraggable();
        }

        /*
         * (non-Javadoc)
         * 
         * @see
         * org.squidy.designer.event.MultiSelectionHandler#startSelection
         * (edu.umd.cs.piccolo.event.PInputEvent, java.awt.Shape)
         */
        @Override
        public void startSelection(PInputEvent event, Shape selectionShape) {
            if (!event.isHandled()) {
                multiSelection = selectionShape;

                event.setHandled(true);
                invalidatePaint();
            }
        }

        /*
         * (non-Javadoc)
         * 
         * @see
         * org.squidy.designer.event.MultiSelectionHandler#selection
         * (edu.umd.cs.piccolo.event.PInputEvent, java.awt.Shape)
         */
        @Override
        public void selection(PInputEvent event, Shape selectionShape) {
            if (!event.isHandled()) {
                multiSelection = selectionShape;

                event.setHandled(true);
                invalidatePaint();
            }
        }

        /*
         * (non-Javadoc)
         * 
         * @see
         * org.squidy.designer.event.MultiSelectionHandler#endSelection
         * (edu.umd.cs.piccolo.event.PInputEvent, java.awt.Shape)
         */
        @Override
        public void endSelection(PInputEvent event, Shape selectionShape) {
            if (!event.isHandled()) {
                multiSelection = null;

                for (Object o : getChildrenReference()) {
                    if (o instanceof VisualShape<?>) {
                        VisualShape<?> shape = (VisualShape<?>) o;

                        PBounds bounds = shape.getGlobalFullBounds();
                        if (selectionShape.contains(bounds)) {
                            System.out.println("containing: " + shape);
                        }
                    }
                }

                event.setHandled(true);
                invalidatePaint();
            }
        }
    };

    //       addInputEventListener(multiSelectionHandler);

    // Add knowledge base to zoom object if class is of type
    // <code>KnowledgeBased</code>.
    if (this instanceof NodeBased<?>) {
        boolean isWorkspace = this instanceof WorkspaceShape;

        knowledgeBase = new AdvancedKnowledgeBase<ZoomShape<VisualShape<?>>>(isWorkspace);

        addChild(knowledgeBase);
        knowledgeBase.setOffset(0, 895);
    }
}

From source file:savant.view.tracks.Track.java

/**
 * Given a location within a track window, determine the record which lies
 * at that location. If multiple records overlap at the given position, only
 * the first one will be returned./*from   www.  ja  va  2s  . c  om*/
 *
 * @param pt the point we're interested in
 * @return the record at that position, or <code>null</code> if no record is
 * there
 */
@Override
public Record getRecordAtPos(Point pt) {
    for (Record r : renderer.recordToShapeMap.keySet()) {
        Shape s = renderer.recordToShapeMap.get(r);
        if (s.contains(new Point2D.Double(pt.x, pt.y))) {
            return r;
        }
    }
    return null;
}