code: public static Rectangle2D calculateBounds(IAtomContainer atomContainer) {
          // this is essential, otherwise a rectangle
          // of (+INF, -INF, +INF, -INF) is returned!
          if (atomContainer.getAtomCount() == 0) {
              return new Rectangle2D.Double();
          } else if (atomContainer.getAtomCount() == 1) {
              Point2d point = atomContainer.getAtom(0).getPoint2d();
              if (point == null) {
                  throw new IllegalArgumentException("Cannot calculate bounds when 2D coordinates are missing.");
              }
              return new Rectangle2D.Double(point.x, point.y, 0, 0);
          }
  
          double xmin = Double.POSITIVE_INFINITY;
          double xmax = Double.NEGATIVE_INFINITY;
          double ymin = Double.POSITIVE_INFINITY;
          double ymax = Double.NEGATIVE_INFINITY;
  
          for (IAtom atom : atomContainer.atoms()) {
              Point2d point = atom.getPoint2d();
              if (point == null) {
                  throw new IllegalArgumentException("Cannot calculate bounds when 2D coordinates are missing.");
              }
              xmin = Math.min(xmin, point.x);
              xmax = Math.max(xmax, point.x);
              ymin = Math.min(ymin, point.y);
              ymax = Math.max(ymax, point.y);
          }
          double width = xmax - xmin;
          double height = ymax - ymin;
          return new Rectangle2D.Double(xmin, ymin, width, height);
      }
docs: Calculate the bounding rectangle for an atom container.
  
  @param atomContainer the atom container to use
  @return the bounding rectangle of the atom container