Hi Michael,

just tried drawing to the back buffer, and it worked like a charm!!!

Besides other uses, I use custom drawing to draw feature selection. 
Geotools SelectionLab uses style for this, but I found it ineffective.
It is slow, and it is difficult to show selection if user create some 
complex style (like graduated value). So I rather use my own code, which 
draws larger outline around each feature with yellow color.
However, it required some alchemy with symbolizers and stuff.

Code is like the following (maybe someone is interested):

After your layers was added, you need to append render listener to 
GtRenderer:
mapPane.getRenderer().addRenderListener(new RenderListener() {

                   @Override
                   public void featureRenderer(SimpleFeature feature) {
                     if (baseImage == null){
                         baseImage = new BufferedImage(
                             mapPane.getVisibleRect().width + 1,  
mapPane.getVisibleRect().height + 1,
                             BufferedImage.TYPE_INT_ARGB);
                         baseImageGraphics = baseImage.createGraphics();
                         
baseImageGraphics.setBackground(java.awt.Color.WHITE);
                       }

                        /// DO YOUR DRAWINGS HERE!!!!!
                   }

                   @Override
                   public void errorOccurred(Exception arg0) {
                   }
                 });

Now your image (additional to geotools image)  is in the backing buffer, 
you need to bitblt it to the MapPane. This will be onRenderingStopped 
method of the MapPaneListener:
this.mapPane.addMapPaneListener(new MapPaneListener() {


         @Override
         public void onRenderingStopped(MapPaneEvent ev) {
           mapPane.getGraphics().drawImage(baseImage, 0, 0, null);
           if (baseImageGraphics != null) {
             baseImageGraphics.dispose();
           }
           baseImage = null;
         }

        @Override
         public void onResized(MapPaneEvent ev) {
           // TODO Auto-generated method stub

         }

         @Override
         public void onRenderingStarted(MapPaneEvent ev) {
           // TODO Auto-generated method stub

         }

         @Override
         public void onRenderingProgress(MapPaneEvent ev) {
           // TODO Auto-generated method stub

         }

         @Override
         public void onNewRenderer(MapPaneEvent ev) {
           // TODO Auto-generated method stub

         }

         @Override
         public void onNewContext(MapPaneEvent ev) {
           // TODO Auto-generated method stub

         }

         @Override
         public void onDisplayAreaChanged(MapPaneEvent ev) {
           // TODO Auto-generated method stub

         }
       });

Many thanks and good luck,
Sergey

On 9/27/2010 5:51 PM, Michael Bedward wrote:
> Hi Sergey,
>
> The problem is that the RenderingExecutor helper class used by
> JMapPane is drawing into a BufferedImage (the backing image of the map
> pane) and then this gets blitted to the screen, overwriting your
> custom drawing.
>
> One solution would be for your listener to create a separate image
> with a transparent background and draw into that. The override
> onRenderingCompleted to blit that onto the screen after the pane's
> normal backing image has been displayed.
>
> I know that sounds like a bit clumsy - perhaps someone else here might
> have a better suggestion ?
>
> Michael
>
>
>
> On 27 September 2010 20:26, LSA<[email protected]>  wrote:
>    
>> Hi, Michael,
>>
>> I tried the following code to draw rectangle around each feature:
>>
>> mapPane.getRenderer().addRenderListener(new RenderListener() {
>>
>>              @Override
>>              public void featureRenderer(SimpleFeature feature) {
>>                AffineTransform transform =
>> RendererUtilities.worldToScreenTransform(
>>                    mapPane.getMapContext().getAreaOfInterest(),
>>                    mapPane.getVisibleRect());
>>
>>                Graphics2D g = ((Graphics2D) mapPane.getGraphics());
>>
>>                if
>> (!org.apache.commons.lang.ArrayUtils.isEmpty(((Geometry)feature.getDefaultGeometry()).getBoundary().getCoordinates())){
>>                  Coordinate c  =
>> ((Geometry)feature.getDefaultGeometry()).getBoundary().getCoordinates()[0];
>>                  Point2D point = new Point2D.Double(c.x, c.y);
>>                  Point2D result = new Point2D.Double();
>>                  transform.transform(point, result);
>>
>>                  g.setColor(Color.BLUE);
>>                  g.drawRect((int)result.getX(), (int)result.getY(), 20, 20);
>>                }
>>
>>
>> //UiGisUtils.highlightFeatureOnMap(ShapeFileRendererComposite.this,
>> feature, Color.YELLOW, 11);
>>              }
>>
>>              @Override
>>              public void errorOccurred(Exception arg0) {
>>              }
>>            });
>>
>> Rectangles appear around some features for a moment only to dissapear
>> after it...
>>
>> What am I doing wrong?
>>
>> Sergey
>>
>>      


------------------------------------------------------------------------------
Start uncovering the many advantages of virtual appliances
and start using them to simplify application deployment and
accelerate your shift to cloud computing.
http://p.sf.net/sfu/novell-sfdev2dev
_______________________________________________
Geotools-gt2-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/geotools-gt2-users

Reply via email to