code: public static void scaleDownToFit( Rectangle2D rectToFitIn, Rectangle2D toScale ) {
          double fitWidth = rectToFitIn.getWidth();
          double fitHeight = rectToFitIn.getHeight();
  
          double toScaleWidth = toScale.getWidth();
          double toScaleHeight = toScale.getHeight();
  
          if (toScaleWidth > fitWidth) {
              double factor = toScaleWidth / fitWidth;
              toScaleWidth = fitWidth;
              toScaleHeight = toScaleHeight / factor;
          }
          if (toScaleHeight > fitHeight) {
              double factor = toScaleHeight / fitHeight;
              toScaleHeight = fitHeight;
              toScaleWidth = toScaleWidth / factor;
          }
          toScale.setRect(0, 0, toScaleWidth, toScaleHeight);
      }
docs: Scales a rectangle down to fit inside the given one, keeping the ratio.
  
  @param rectToFitIn the fixed rectangle to fit in.
  @param toScale the rectangle to scale.