code: public static AffineTransform createScreenTransform(Rectangle2D userBounds, Rectangle2D screenBounds, boolean keepAspectRatio, AffineTransform transform)
      {
          double width = screenBounds.getWidth();
          double height = screenBounds.getHeight();
          double aspect = width / height;
          double xMin = userBounds.getMinX();
          double yMin = userBounds.getMinY();
          double xMax = userBounds.getMaxX();
          double yMax = userBounds.getMaxY();
          double xyWidth = xMax - xMin;
          double xyHeight = yMax - yMin;
          double scaleX;
          double scaleY;
          double xOff;
          double yOff;
          if (keepAspectRatio)
          {
              double xyAspect = xyWidth / xyHeight;
              if (aspect > xyAspect)
              {
                  scaleX = scaleY = height / xyHeight;
                  xOff = -scaleY*xMin + (width - scaleY*xyWidth) / 2.0;
                  yOff = scaleY*yMin + height;
              }
              else
              {
                  scaleX = scaleY = width / xyWidth;
                  xOff = -scaleY*xMin;
                  yOff = scaleY*yMin + height / 2.0 + scaleY*xyHeight / 2.0;
              }
          }
          else
          {
              scaleX = width / xyWidth;
              scaleY = height / xyHeight;
              xOff = -scaleX*xMin;
              yOff = scaleY*yMin + height / 2.0 + scaleY*xyHeight / 2.0;
          }
          transform.setTransform(scaleX, 0, 0, -scaleY, xOff, yOff);
          return transform;
      }
docs: Creates translation that translates userBounds in Cartesian coordinates
  to screenBound in screen coordinates.
  <p>In Cartesian y grows up while in screen y grows down
  @param userBounds
  @param screenBounds
  @param keepAspectRatio
  @param transform
  @return Returns parameter transform