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(double x, double y);

Source Link

Document

Tests if the specified coordinates are inside the boundary of the Shape , as described by the definition of insideness.

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(30, 40));
}

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

/** 
  * Iterates over Vertices, checking to see if x,y is contained in the
  * Vertex's Shape. If (x,y) is contained in more than one vertex, use
  * the vertex whose center is closest to the pick point.
  * @see edu.uci.ics.jung.visualization.picking.PickSupport#getVertex(double, double)
  */// ww  w . ja v  a 2s . c  om
public V getVertex(Layout<V, E> layout, double x, double y) {

    V closest = null;
    double minDistance = Double.MAX_VALUE;
    Point2D ip = vv.getRenderContext().getMultiLayerTransformer().inverseTransform(Layer.VIEW,
            new Point2D.Double(x, y));
    x = ip.getX();
    y = ip.getY();

    while (true) {
        try {
            for (V v : getFilteredVertices(layout)) {

                Shape shape = vv.getRenderContext().getVertexShapeTransformer().transform(v);
                // get the vertex location
                Point2D p = layout.transform(v);
                if (p == null)
                    continue;
                // transform the vertex location to screen coords
                p = vv.getRenderContext().getMultiLayerTransformer().transform(Layer.LAYOUT, p);

                double ox = x - p.getX();
                double oy = y - p.getY();

                if (shape.contains(ox, oy)) {

                    if (style == Style.LOWEST) {
                        // return the first match
                        return v;
                    } else if (style == Style.HIGHEST) {
                        // will return the last match
                        closest = v;
                    } else {

                        // return the vertex closest to the
                        // center of a vertex shape
                        Rectangle2D bounds = shape.getBounds2D();
                        double dx = bounds.getCenterX() - ox;
                        double dy = bounds.getCenterY() - oy;
                        double dist = dx * dx + dy * dy;
                        if (dist < minDistance) {
                            minDistance = dist;
                            closest = v;
                        }
                    }
                }
            }
            break;
        } catch (ConcurrentModificationException cme) {
        }
    }
    return closest;
}

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

public Map<Record, Shape> searchPoint(Point p) {

    if (!hasMappedValues() || data == null)
        return null;

    DrawingMode mode = (DrawingMode) instructions.get(DrawingInstruction.MODE);

    Map<Record, Shape> map = new HashMap<Record, Shape>();
    boolean allowFuzzySNPs = true;

    Rectangle2D testIntersection = new Rectangle2D.Double(p.x - 3, p.y - 3, 7, 7);
    for (Record rec : recordToShapeMap.keySet()) {
        Shape s = recordToShapeMap.get(rec);

        if (s != null) {
            //if (contains AND (notArc OR (isEdge...))
            boolean hit = false;
            if (mode == DrawingMode.ARC || mode == DrawingMode.ARC_PAIRED) {
                hit = s.intersects(testIntersection)
                        && (!s.contains(p.x - 3, p.y - 3) || !s.contains(p.x + 3, p.y - 3));
            } else {
                hit = s.contains(p);/*w w  w  .ja v a  2s . co  m*/
            }
            // At low resolutions, SNPs can be hard to hit with the mouse, so give a second chance with a fuzzier check.
            if (mode == DrawingMode.SNP || mode == DrawingMode.STRAND_SNP || mode == DrawingMode.MATRIX) {
                if (hit) {
                    if (allowFuzzySNPs) {
                        // We may have accumulated some fuzzy SNP hits.  We now have an exact one, so dump the fuzzies.
                        map.clear();
                        allowFuzzySNPs = false;
                    }
                } else {
                    if (allowFuzzySNPs) {
                        hit = s.intersects(testIntersection);
                    }
                }
            }
            if (hit) {
                map.put(rec, s);
                continue;
            }
        } else {
            LOG.info("Why is shape null for " + rec);
        }

        //check other artifacts
        Shape artifact = artifactMap.get(rec);
        if (artifact != null && artifact.contains(p.x, p.y)) {
            map.put(rec, s);
        }
    }
    return map.isEmpty() ? null : map;
}