package org.mwc.cmap.gt2plot.proj;

import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.RenderingHints;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;
import java.util.HashMap;
import java.util.Map;

import org.geotools.geometry.jts.ReferencedEnvelope;
import org.geotools.map.MapContent;
import org.geotools.renderer.lite.StreamingRenderer;

/** helper class that produces an image from a GeoTools map
 * 
 * @author ian
 *
 */
public class GeoToolsPainter
{

	public static BufferedImage drawAwtImage(int width, int height, GtProjection proj)
	{
		MapContent map = proj.getMapContent();
		StreamingRenderer renderer = new StreamingRenderer();
		renderer.setMapContent(map);

		RenderingHints hints = new RenderingHints(RenderingHints.KEY_ANTIALIASING,
				RenderingHints.VALUE_ANTIALIAS_ON);
		renderer.setJava2DHints(hints);

		Map<String, Object> rendererParams = new HashMap<String, Object>();
		rendererParams.put("optimizedDataLoadingEnabled", new Boolean(true));

		renderer.setRendererHints(rendererParams);

		BufferedImage baseImage = new BufferedImage(width + 1, height + 1,
				BufferedImage.TYPE_INT_ARGB);
		Graphics2D g2d = baseImage.createGraphics();
	//	g2d.fillRect(0, 0, width + 1, height + 1);
		g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
				RenderingHints.VALUE_ANTIALIAS_ON);

		// renderer.setContext(context);
		java.awt.Rectangle awtRectangle = new Rectangle(0, 0, width, height);
		final ReferencedEnvelope mapAOI = map.getViewport().getBounds();
		AffineTransform worldToScreen = map.getViewport().getWorldToScreen();
		renderer.paint(g2d, awtRectangle, mapAOI, worldToScreen);
		// swtImage.dispose();

		return baseImage;
	}

}

@@ -29,16 +29,39 @@
 .proj;%0A%0A
+import java.awt.Color;%0A
 import j
@@ -80,16 +80,16 @@
 hics2D;%0A
-
 import j
@@ -409,16 +409,19 @@
 er;%0A%0A/**
+%0A *
  helper 
@@ -490,16 +490,17 @@
 r ian%0A *
+ 
 %0A */%0Apub
@@ -528,16 +528,311 @@
 nter%0A%7B%0A%0A
+%09/** utlity function to produce a Java image from the supplied layers%0A%09 * %0A%09 * @param width width of required image%0A%09 * @param height height of reuqired image%0A%09 * @param proj projection containing the MapContent %0A%09 * @param bkColor (optional) background color to fill the image%0A%09 * @return%0A%09 */%0A
 %09public 
@@ -887,17 +887,20 @@
  height,
- 
+%0A%09%09%09
 GtProjec
@@ -908,16 +908,31 @@
 ion proj
+, Color bkColor
 )%0A%09%7B%0A%09%09M
@@ -1552,54 +1552,214 @@
 ();%0A
+%0A%09
 %09//
-%09g2d.fillRect(0, 0, width + 1, height + 1);
+ do we need to set the background color?%0A%09%09if (bkColor != null)%0A%09%09%7B%0A%09%09%09// fill in the background color%0A%09%09%09g2d.setPaint(bkColor);%0A%09%09%09g2d.fillRect(0, 0, baseImage.getWidth(), baseImage.getHeight());%0A%09%09%7D%0A
 %0A%09%09g

introduce ability to fill background