display legend with Geotools JMapFrame -


shall give tips on how display legend shapefile in jmapframe of geotools? have created style shapefile , need way tell users how style defined, comes out need of legend.

there package org.geotools.legend. not know how use it.

thanks!

you need iterate through styles featuretypestyless rules symbolizers , draw representative feature each one. like:

private void drawlegend(bufferedimage img, rule r) {     (symbolizer sym : r.symbolizers()) {       simplefeature feature = null;       if (sym instanceof linesymbolizer) {         linestring line = drawer.line(new int[] { 1, 1, 10, 20, 20, 20 });         feature = drawer.feature(line);       } else if(sym instanceof polygonsymbolizer) {         polygon  p = drawer.polygon(new int[] { 1, 1, 1, 18, 18, 18, 18, 1, 1,1 });         feature = drawer.feature(p);       } else if(sym instanceof pointsymbolizer || sym instanceof textsymbolizer) {         point p = drawer.point(10, 10);         feature = drawer.feature(p);       }        if(feature == null)         continue;       drawer.drawdirect(img, feature, r);       graphics2d gr = img.creategraphics();       gr.setcolor(color.black);       if (r.getdescription() != null && r.getdescription().gettitle() != null) {         gr.drawstring(r.getdescription().gettitle().tostring(), 20, 18);       }     }   } 

and can draw images jpanel or map.

for worked example see how geoserver creates response getlegendgraphic request.


Comments