GeoTools : LiteRendering

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;

import javax.swing.JFrame;
import javax.swing.JPanel;

import org.geotools.factory.FactoryConfigurationError;
import org.geotools.feature.AttributeType;
import org.geotools.feature.AttributeTypeFactory;
import org.geotools.feature.Feature;
import org.geotools.feature.FeatureCollection;
import org.geotools.feature.FeatureCollections;
import org.geotools.feature.FeatureType;
import org.geotools.feature.FeatureTypeFactory;
import org.geotools.feature.IllegalAttributeException;
import org.geotools.feature.SchemaException;
import org.geotools.map.DefaultMapContext;
import org.geotools.map.MapContext;
import org.geotools.renderer.lite.LiteRenderer;
import org.geotools.styling.LineSymbolizer;
import org.geotools.styling.Style;
import org.geotools.styling.StyleBuilder;

import com.vividsolutions.jts.geom.Coordinate;
import com.vividsolutions.jts.geom.Envelope;
import com.vividsolutions.jts.geom.GeometryFactory;
import com.vividsolutions.jts.geom.LineString;
import com.vividsolutions.jts.io.ParseException;
import com.vividsolutions.jts.io.WKTReader;

public class LiteRendererTest2 {

	public static void main(String[] args)
		throws
			FactoryConfigurationError,
			SchemaException,
			IllegalAttributeException,
			ParseException,
			InterruptedException {
		//		Set up GUI elements		
		JFrame frame = new JFrame();
		frame.addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent e) {
				e.getWindow().dispose();
			}
		});

		JPanel p = new JPanel();
		frame.getContentPane().add(p);
		frame.setSize(400, 400);
		frame.setVisible(true);

		//		feature attributes creation 
		AttributeType[] types = new AttributeType[1];
		types[0] =
			AttributeTypeFactory.newAttributeType("centre", LineString.class);

		// feature type creation
		FeatureType pointType =
			FeatureTypeFactory.newFeatureType(types, "linefeature");

		//feature instance creation
		WKTReader wktReader = new WKTReader();
		LineString line3 =
			(LineString) wktReader.read("LINESTRING (-76 42, -75 43)");
		LineString line1 =
			(LineString) wktReader.read("LINESTRING (-76 42, -76 43)");
		LineString line2 =
			(LineString) wktReader.read("LINESTRING (-76 42, -74 42)");

		Feature feature1 = pointType.create(new Object[] { line1 });
		Feature feature2 = pointType.create(new Object[] { line2 });
		Feature feature3 = pointType.create(new Object[] { line3 });
		//feature collection creation
		FeatureCollection aFeatureCollection =
			FeatureCollections.newCollection();
		aFeatureCollection.add(feature1);
		aFeatureCollection.add(feature2);
		aFeatureCollection.add(feature3);

		// Prepare styles
		StyleBuilder sb = new StyleBuilder();

		// Create line symbolizer
		LineSymbolizer lineSymb =
			(LineSymbolizer) sb.createLineSymbolizer(Color.RED, 2);

		Style aStyle = sb.createStyle(lineSymb);

		MapContext map = new DefaultMapContext();
		map.addLayer(aFeatureCollection, aStyle);
		LiteRenderer renderer = new LiteRenderer(map);

		// Create a transform that maps the feature collection extents to the GUI Pane extents	
		AffineTransform transform;
		transform =
			renderer.worldToScreenTransform(
				aFeatureCollection.getBounds(),
				p.getBounds());
		System.out.println(transform);
		renderer.paint((Graphics2D) p.getGraphics(), p.getBounds(), transform);
		Thread.sleep(2000);

		// Unfortunately, worldToScreenTransform generally results in different x and y
		// scales which is not desireable.  The following is an approach that
		// uses a uniform scale.  First we get the smaller scale value.
		double scaleX =
			p.getBounds().getWidth()
				/ aFeatureCollection.getBounds().getWidth();
		double scaleY =
			p.getBounds().getHeight()
				/ aFeatureCollection.getBounds().getHeight();
		double scale = scaleX < scaleY ? scaleX : scaleY;

		transform = new AffineTransform();
		// Translate by the GUI Pane height to compensate for
		// y-origin at the top
		transform.translate(0, p.getBounds().getHeight());
		// Scale by value we calculated above
		// Y scale is negative to compensate for y-origin at the top
		transform.scale(scale, -scale);
		// Translate by the feature collection bounds lower-left corner
		transform.translate(
			-aFeatureCollection.getBounds().getMinX(),
			-aFeatureCollection.getBounds().getMinY());
		//		p.getGraphics().clearRect(0,0,(int)p.getBounds().getWidth(),(int)p.getBounds().getHeight());
		renderer.paint((Graphics2D) p.getGraphics(), p.getBounds(), transform);

		// Render to an image buffer instead
		int w = 300, h = 600;
		BufferedImage image =
			new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
		Graphics g = image.getGraphics();
		g.setColor(Color.white);
		g.fillRect(0, 0, w, h);
		renderer.paint((Graphics2D) g, new Rectangle(0, 0, w, h), transform);
	}
}