Hi
I have an problem on running my code, when we adding flashlinelayer on
mouse move event it show very high fluctuation.
Please check it out and solve my problem as soon as possible.
Here is the code:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package bhuna8;
import com.vividsolutions.jts.geom.Point;
import com.vividsolutions.jts.geom.LineString;
import com.vividsolutions.jts.geom.Coordinate;
import com.vividsolutions.jts.geom.GeometryFactory;
import java.awt.event.MouseWheelEvent;
import java.awt.event.MouseWheelListener;
import java.io.File;
import java.util.ArrayList;
import javax.swing.SwingUtilities;
import org.geotools.styling.Style;
import org.geotools.data.FileDataStore;
import org.geotools.data.FileDataStoreFinder;
import org.geotools.map.FeatureLayer;
import org.geotools.map.Layer;
import org.geotools.map.MapContent;
import org.geotools.styling.SLD;
import org.geotools.swing.data.JFileDataStoreChooser;
import org.geotools.swing.JMapFrame;
import org.geotools.data.FeatureSource;
import org.geotools.data.simple.SimpleFeatureCollection;
import org.geotools.feature.FeatureCollections;
import org.geotools.feature.simple.SimpleFeatureBuilder;
import org.geotools.feature.simple.SimpleFeatureTypeBuilder;
import org.geotools.geometry.jts.JTSFactoryFinder;
import org.geotools.geometry.jts.ReferencedEnvelope;
import org.geotools.swing.event.MapMouseEvent;
import org.geotools.swing.tool.CursorTool;
import org.opengis.feature.simple.SimpleFeature;
import org.opengis.feature.simple.SimpleFeatureType;
/**
*
* @author nic4026
*/
public class Bhuna8 {
/**
* @param args the command line arguments
*/
MapContent map = new MapContent();
int click =0,i=0;
ArrayList<Coordinate> coords = new ArrayList<Coordinate>();
JMapFrame mframe = new JMapFrame(map);
Layer flashlayer;
Layer linelayer;
public static void main(String[] args) throws Exception {
Bhuna8 bh = new Bhuna8();
File file = JFileDataStoreChooser.showOpenFile("shp", null);
if(file==null)
{
return;
}
bh.displayFile(file);
// System.out.println("kapil");
}
public void displayFile(File file) throws Exception
{
FileDataStore store = FileDataStoreFinder.getDataStore(file);
FeatureSource featuresource = store.getFeatureSource();
map.setTitle("show point");
Style style = SLD.createSimpleStyle(featuresource.getSchema());
Layer layer = new FeatureLayer(featuresource,style);
map.addLayer(layer);
//mframe.showMap(map);
mframe.setMapContent(map);
mframe.enableToolBar(true);
mframe.enableStatusBar(true);
// JToolBar toolbar = mframe.getToolBar();
// JButton but = new JButton("Create Layer");
// toolbar.addSeparator();
// toolbar.add(but);
// but.setVisible(true);
// but.addActionListener(new ActionListener() {
//System.out.println("kapil2");
//
// public void actionPerformed(ActionEvent e) {
mframe.getMapPane().setCursorTool(new CursorTool()
{
@Override
public void onMouseClicked(MapMouseEvent mme) {
drawPoint(mme);
}
@Override
public void onMouseDragged(MapMouseEvent mme)
{
// drawLine(mme);
}
@Override
public void onMouseMoved(final MapMouseEvent mme)
{
Runnable doWorkRunnable = new Runnable() {
@Override
public void run() {
try {
if(i==1 )
{
flashLine(mme);
System.out.println("mouse moved");
}
}catch(Exception ex)
{
}
}
};
SwingUtilities.invokeLater(doWorkRunnable);
}
});
mframe.getMapPane().addMouseWheelListener(new
MouseWheelListener()
{
@Override
public void mouseWheelMoved(MouseWheelEvent mev)
{
zoom(mev);
}
});
mframe.setSize(600, 600);
mframe.setVisible(true);
}
//
void drawPoint(MapMouseEvent mme)
{
i++;//System.out.println("i = "+i);
java.awt.geom.Point2D scpos = mme.getWorldPos();
// click = mme.getClickCount();
//AffineTransform tr =
mframe.getMapPane().getScreenToWorldTransform();
double x = scpos.getX();
double y = scpos.getY();
Coordinate c = new Coordinate(x,y);
coords.add( c);
// System.out.print("CoordList"+coords);
// System.out.println("screen Position"+scpos);
try {
SimpleFeatureTypeBuilder tbuild = new
SimpleFeatureTypeBuilder();
tbuild.setName("Location");
tbuild.add("location",Point.class);
SimpleFeatureType type = tbuild.buildFeatureType();
SimpleFeatureBuilder featureBuilder = new
SimpleFeatureBuilder(type);
GeometryFactory geometryFactory =
JTSFactoryFinder.getGeometryFactory(null);
com.vividsolutions.jts.geom.Point point =
geometryFactory.createPoint(new Coordinate(x,y));
featureBuilder.add(point);
SimpleFeature feature = featureBuilder.buildFeature(null);
SimpleFeatureCollection collection =
FeatureCollections.newCollection();
collection.add(feature);
// MapContent map = new MapContent();
Style style = SLD.createSimpleStyle(type);
Layer player = new FeatureLayer(collection,style);
mframe.getMapContent().addLayer(player);
//map.addLayer(player);
if(i>1)
drawLine(mme);
// Point point = geometryFactory.createPoint( coord );
} catch (Exception ex) {
// Logger.getLogger(Mousetest.class.getName()).log(Level.SEVERE,
null, ex);
}
}
void zoom(MouseWheelEvent mev)
{
double zoomclick = 0.1;
int click = mev.getWheelRotation();
int sign = (click < 0 ? -1 : 1);
ReferencedEnvelope env = mframe.getMapPane().getDisplayArea();
double width = env.getWidth();
double height = env.getHeight();
double expvalx = width*zoomclick*sign;
double expvaly = height*zoomclick*sign;
env.expandBy(expvalx,expvaly);
mframe.getMapPane().setDisplayArea(env);
}
void drawLine(MapMouseEvent mme)
{System.out.println("size "+coords.size());
for(int i=0;i<coords.size();i++) {
System.out.println("Coordinate at "+i+" "+coords.get(i));
}
// i++;
java.awt.geom.Point2D scpos = mme.getWorldPos();
double x = scpos.getX();
double y = scpos.getY();
//Coordinate c = new Coordinate(x,y);
// coords.add( c);
try
{
SimpleFeatureTypeBuilder tbuild = new SimpleFeatureTypeBuilder();
tbuild.setName("LineString");
tbuild.add("Linestring", LineString.class);
SimpleFeatureType type = tbuild.buildFeatureType();
SimpleFeatureBuilder featureBuilder = new
SimpleFeatureBuilder(type);
//System.out.println("first : "+coords.get(coords.size()-2));
// System.out.println("last : "+coords.get(coords.size()-1));
Coordinate[] coord = new
Coordinate[]{coords.get(coords.size()-2),coords.get(coords.size()-1)};
GeometryFactory geometryFactory =
JTSFactoryFinder.getGeometryFactory(null);
LineString line = geometryFactory.createLineString(coord);
featureBuilder.add(line);
SimpleFeature feature = featureBuilder.buildFeature(null);
SimpleFeatureCollection collection =
FeatureCollections.newCollection();
collection.add(feature);
Style style = SLD.createSimpleStyle(type);
linelayer = new FeatureLayer(collection,style);
//MapContent map = new MapContent();
//if(lineLayer!=null)
// map.removeLayer(player);
mframe.getMapPane().getMapContent().addLayer(linelayer);
// map.addLayer(linelayer);
} catch (Exception ex) {
}
}
public void removeLayer(Layer layer,JMapFrame mframe) {
if (layer != null) {
mframe.getMapContent().removeLayer(layer);
}
}
public void flashLine(MapMouseEvent mme) {
removeLayer(flashlayer, mframe);
java.awt.geom.Point2D scpos = mme.getWorldPos();
try
{
SimpleFeatureTypeBuilder tbuild = new SimpleFeatureTypeBuilder();
tbuild.setName("FlashLine");
tbuild.add("FlashLine", LineString.class);
SimpleFeatureType type = tbuild.buildFeatureType();
SimpleFeatureBuilder featureBuilder = new
SimpleFeatureBuilder(type);
double x = scpos.getX();
double y = scpos.getY();
Coordinate c = new Coordinate(x,y);
Coordinate[] coord = new
Coordinate[]{coords.get(coords.size()-1),c};
GeometryFactory geometryFactory =
JTSFactoryFinder.getGeometryFactory(null);
LineString line = geometryFactory.createLineString(coord);
featureBuilder.add(line);
SimpleFeature feature = featureBuilder.buildFeature(null);
SimpleFeatureCollection collection =
FeatureCollections.newCollection();
collection.add(feature);
Style style = SLD.createSimpleStyle(type);
flashlayer = new FeatureLayer(collection,style);
mframe.getMapPane().getMapContent().addLayer(flashlayer);
// map.addLayer(flashlayer);
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
------------------------------------------------------------------------------
Live Security Virtual Conference
Exclusive live event will cover all the ways today's security and
threat landscape has changed and how IT managers can respond. Discussions
will include endpoint security, mobile security and the latest in malware
threats. http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/
_______________________________________________
GeoTools-GT2-Users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/geotools-gt2-users