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 java.io.File;

import javax.imageio.ImageIO;
import java.awt.Graphics2D;

import javax.swing.JFrame;
import javax.swing.JPanel;

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.factory.FactoryConfigurationError;
import org.geotools.feature.GeometryAttributeType;
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.PointSymbolizer;
import org.geotools.styling.Style;
import org.geotools.styling.StyleBuilder;

import com.vividsolutions.jts.geom.Coordinate;
import com.vividsolutions.jts.geom.GeometryFactory;
import com.vividsolutions.jts.io.ParseException;
import com.vividsolutions.jts.geom.Point;

public class CreateMap {

	public static void main(String arg[]) throws
			FactoryConfigurationError,
			SchemaException,
			IllegalAttributeException,
			ParseException,
			InterruptedException {

		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);

		Coordinate ptc1 = new Coordinate(566469.0d,4181502.0d);
		Coordinate ptc2 = new Coordinate(567309.0d,4177908.0d);
		Coordinate ptc3 = new Coordinate(567306.0d,4178221.0d);
		Coordinate ptc4 = new Coordinate(563567.0d,4182488.0d);
		Coordinate ptc5 = new Coordinate(563601.0d,4182221.0d);
		Coordinate ptc6 = new Coordinate(560531.0d,4181786.0d);

		GeometryFactory geomFac = new GeometryFactory();

		Point ptG1 = geomFac.createPoint(ptc1);
		Point ptG2 = geomFac.createPoint(ptc2);
		Point ptG3 = geomFac.createPoint(ptc3);
		Point ptG4 = geomFac.createPoint(ptc4);
		Point ptG5 = geomFac.createPoint(ptc5);
		Point ptG6 = geomFac.createPoint(ptc6);


		GeometryAttributeType ptGA =
            (GeometryAttributeType)AttributeTypeFactory.newAttributeType("the_geom", Point.class);

        AttributeType[] ptATs = new AttributeType[1];
		ptATs[0] = ptGA;


		FeatureType ptFT = FeatureTypeFactory.newFeatureType(ptATs, "pointfeature");

		Feature feature1 = ptFT.create(new Object[] { ptG1 });
		Feature feature2 = ptFT.create(new Object[] { ptG2 });
		Feature feature3 = ptFT.create(new Object[] { ptG3 });
		Feature feature4 = ptFT.create(new Object[] { ptG4 });
		Feature feature5 = ptFT.create(new Object[] { ptG5 });
		Feature feature6 = ptFT.create(new Object[] { ptG6 });

		FeatureCollection aFeatureCollection = FeatureCollections.newCollection();
		aFeatureCollection.add(feature1);
		aFeatureCollection.add(feature2);
		aFeatureCollection.add(feature3);
		aFeatureCollection.add(feature4);
		aFeatureCollection.add(feature5);
		aFeatureCollection.add(feature6);

		StyleBuilder sb = new StyleBuilder();

		PointSymbolizer ptSymb = (PointSymbolizer) sb.createPointSymbolizer();

		Style aStyle = sb.createStyle(ptSymb);
		aStyle.setTile("Earthquake Prone Regions");


		MapContext map = new DefaultMapContext();
		map.addLayer(aFeatureCollection, aStyle);
		map.setTitle("Earthquake Prone Regions");
		LiteRenderer renderer = new LiteRenderer(map);

		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 = 900, h = 1200;
		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);
		//Thread.sleep(2000);
		try {
			ImageIO.write(image, "jpeg", new File("c:\\temp\\test.jpeg"));
		} catch(Exception e) {}

	}
}