code: private WiresMagnet getCloserMagnet(final WiresShape shape, final WiresContainer parent, final boolean allowOverlap)
      {
          final WiresShape parentShape = (WiresShape) parent;
          final MagnetManager.Magnets magnets = parentShape.getMagnets();
          final Point2D shapeLocation = shape.getComputedLocation();
          final Point2D shapeCenter = Geometry.findCenter(shape.getPath().getBoundingBox());
          final double shapeX = shapeCenter.getX() + shapeLocation.getX();
          final double shapeY = shapeCenter.getX() + shapeLocation.getY();
          int magnetIndex = -1;
          Double minDistance = null;
  
          //not considering the zero magnet, that is the center.
          for (int i = 1; i < magnets.size(); i++)
          {
              final WiresMagnet magnet = magnets.getMagnet(i);
              //skip magnet that has shape over it
              if (allowOverlap || !hasShapeOnMagnet(magnet, parentShape))
              {
                  final double magnetX = magnet.getControl().getLocation().getX();
                  final double magnetY = magnet.getControl().getLocation().getY();
                  final double distance = Geometry.distance(magnetX, magnetY, shapeX, shapeY);
                  //getting shorter distance
                  if ((minDistance == null) || (distance < minDistance))
                  {
                      minDistance = distance;
                      magnetIndex = i;
                  }
              }
          }
          return (magnetIndex > 0 ? magnets.getMagnet(magnetIndex) : null);
      }
docs: Reurn the closer magnet
  @param shape
  @param parent
  @param allowOverlap should allow overlapping docked shape or not
  @return closer magnet or null if none are available