Q:

Build tiles from a feature collection

I am trying to create tiles given a collection of features. 
UPDATE
Let's say I got two tiles per zoom level 1 and my features are about a small area near Berlin. I expect one of the two tiles empty and the other one filled with all the features with proper dimension (I expect to see something like a point because features are about a small area). What I get is an empty tile (correct) and a image 256 x 256 representing the features like it was a thumbnail, not a tile. 
This is what I did so far to render features in a tile:
...
MapContent content = new MapContent();
GTRenderer renderer = new StreamingRenderer();
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
Graphics2D graphics = image.createGraphics();
ReferencedEnvelope mapArea = featureCollection.getBounds();
Style shpStyle = SLD.createSimpleStyle(featureCollection.getSchema());
Layer layer = new FeatureLayer(featureCollection, shpStyle);
content.addLayer(layer);
renderer.setMapContent(content);
Rectangle tileArea = new Rectangle(0, 0, 256, 256);
renderer.paint(graphics, tileArea, mapArea);
File fileToSave = File.createTempFile("mytesttile.png");
ImageIO.write(image, "png", fileToSave);
...

but this code produces a small (256 x 256) image of the given collection, not a tile. I am in short of time and I can't find some good docs or a working example. Can you point me to the right direction? I am using geotools version 17.2.

A:

I was in the right path, just confusing rendering areas. mapArea must represent the area your tile want to cover about the entire map. I just change my code this way.
...
ReferencedEnvelope mapArea = new ReferencedEnvelope(tile.getEnvelope().getEastCoordinateMin(),
    tile.getEnvelope().getEastCoordinateMax(), tile.getEnvelope().getNorthCoordinateMin(),
    tile.getEnvelope().getNorthCoordinateMax(), org.geotools.referencing.CRS.decode("EPSG:4326"));
Style shpStyle = SLD.createSimpleStyle(featureCollection.getSchema());
Layer layer = new FeatureLayer(featureCollection, shpStyle);
content.addLayer(layer);
renderer.setMapContent(content);
Rectangle imageArea = new Rectangle(0, 0, width, height);
renderer.paint(graphics, imageArea, mapArea);
...

tileArea variable name changed in imageArea, it still implements tile's size.

