1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
| public class Main {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Raccord de méthode auto-généré
new Main();
}
public Main() {
JFrame frm = new JFrame();
frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frm.setSize(800,600);
frm.setTitle("Ma Première carte avec GeoTools");
JPanel pfond = new JPanel(new BorderLayout());
frm.setContentPane(pfond);
frm.setJMenuBar(buildMenu());
frm.getContentPane().add(BorderLayout.CENTER,buildMap());
frm.getContentPane().add(BorderLayout.NORTH,buildTool());
frm.setVisible(true);
}
private JMenuBar buildMenu() {
JMenuBar menu = new JMenuBar();
JMenu mfichier = new JMenu("Fichier");
JMenuItem iquitter = new JMenuItem("Quitter");
iquitter.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
mfichier.add(iquitter);
menu.add(mfichier);
return menu;
}
private JMapPane mappane = new JMapPane();
private JPanel buildMap() {
mappane.setBackground(Color.WHITE);
try{
// Creation of the Map
DefaultMapContext context = new DefaultMapContext();
// adding the listeners if needed
context.addMapBoundsListener(new MapBoundsListener() {
public void mapBoundsChanged(MapBoundsEvent event) {
// anAction
}
});
context.addMapLayerListListener(new MapLayerListListener() {
public void layerAdded(MapLayerListEvent event) {
// anAction
}
public void layerRemoved(MapLayerListEvent event) {
// anAction
}
public void layerChanged(MapLayerListEvent event) {
// anAction
}
public void layerMoved(MapLayerListEvent event) {
// anAction
}
});
context.addPropertyChangeListener(new PropertyChangeListener() {
public void propertyChange(PropertyChangeEvent evt) {
// anAction
}
});
// set some extra information to the map
context.setTitle("ATitle");
context.setContactInformation("anInformation");
context.setAbstract("anAbstract");
// creating feature collection
//feature attributes creation
AttributeType[] types = new AttributeType[1];
types[0] = AttributeTypeFactory.newAttributeType("centre", Point.class);
// feature type creation
FeatureType pointType = FeatureTypeFactory.newFeatureType(types,"pointfeature");
//feature instance creation
// we could use also the WKT writers instead
GeometryFactory geomFac = new GeometryFactory();
Point point1 = geomFac.createPoint(new Coordinate((0.0), 0.0));
Point point2 = geomFac.createPoint(new Coordinate((10.0), 10.0));
Feature feature1 = pointType.create(new Object[] { point1 });
Feature feature2 = pointType.create(new Object[] { point2 });
//feature collection creation
FeatureCollection fc = FeatureCollections.newCollection();
fc.add(feature1);
fc.add(feature2);
// creating the map style
StyleFactoryFinder sf = (StyleFactoryFinder) StyleFactoryFinder.createStyleFactory();
PointSymbolizer ps = (PointSymbolizer) ((StyleFactory) sf).getDefaultPointSymbolizer();
Rule r = ((StyleFactory) sf).createRule();
r.setSymbolizers(new Symbolizer[] { (Symbolizer) ps });
Style s = (Style) sf.createStyleFactory();
s.setName("style");
s.addFeatureTypeStyle(((StyleFactory) sf).createFeatureTypeStyle(new Rule[] { r }));
// create the layer from the style
MapLayer aLayer = new DefaultMapLayer(fc, s, "layer1");
// adding the layer to the map
context.addLayer(aLayer);
StreamingRenderer render = new StreamingRenderer();
mappane.setRenderer(render);
mappane.setMapArea(context.getLayerBounds());
} catch(Exception e){
e.printStackTrace();
}
return mappane;
}
private JPanel buildTool() {
JPanel outil = new JPanel(new FlowLayout(FlowLayout.LEFT));
JButton plus = new JButton("+");
plus.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
mappane.setState(JMapPane.ZoomIn);
}
});
outil.add(plus);
JButton moins = new JButton("-");
moins.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
mappane.setState(JMapPane.ZoomOut);
}
});
outil.add(moins);
JButton pan = new JButton("Pan");
pan.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
mappane.setState(JMapPane.Pan);
}
});
outil.add(pan);
return outil;
}
} |
Partager