code: public BufferedImage drawImage( ReferencedEnvelope ref, int imageWidth, int imageHeight, double buffer ) {
          checkMapContent();
  
          if (buffer > 0.0)
              ref.expandBy(buffer, buffer);
  
          Rectangle2D refRect = new Rectangle2D.Double(ref.getMinX(), ref.getMinY(), ref.getWidth(), ref.getHeight());
          Rectangle2D imageRect = new Rectangle2D.Double(0, 0, imageWidth, imageHeight);
  
          GeometryUtilities.scaleToRatio(imageRect, refRect, false);
  
          ReferencedEnvelope newRef = new ReferencedEnvelope(refRect, ref.getCoordinateReferenceSystem());
  
          Rectangle imageBounds = new Rectangle(0, 0, imageWidth, imageHeight);
          BufferedImage dumpImage = new BufferedImage(imageWidth, imageHeight, BufferedImage.TYPE_INT_RGB);
          Graphics2D g2d = dumpImage.createGraphics();
          g2d.fillRect(0, 0, imageWidth, imageHeight);
          g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
  
          synchronized (renderer) {
              renderer.paint(g2d, imageBounds, newRef);
          }
  
          return dumpImage;
      }
docs: Draw the map on an image.
  
  @param bounds the area of interest.
  @param imageWidth the width of the image to produce.
  @param imageHeight the height of the image to produce.
  @param buffer the buffer to add around the map bounds in map units.
  @return the image.