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 {
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);
AttributeType[] types = new AttributeType[1];
types[0] =
AttributeTypeFactory.newAttributeType("centre", LineString.class);
FeatureType pointType =
FeatureTypeFactory.newFeatureType(types, "linefeature");
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 });
FeatureCollection aFeatureCollection =
FeatureCollections.newCollection();
aFeatureCollection.add(feature1);
aFeatureCollection.add(feature2);
aFeatureCollection.add(feature3);
StyleBuilder sb = new StyleBuilder();
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);
AffineTransform transform;
transform =
renderer.worldToScreenTransform(
aFeatureCollection.getBounds(),
p.getBounds());
System.out.println(transform);
renderer.paint((Graphics2D) p.getGraphics(), p.getBounds(), transform);
Thread.sleep(2000);
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();
transform.translate(0, p.getBounds().getHeight());
transform.scale(scale, -scale);
transform.translate(
-aFeatureCollection.getBounds().getMinX(),
-aFeatureCollection.getBounds().getMinY());
renderer.paint((Graphics2D) p.getGraphics(), p.getBounds(), transform);
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);
}
}