我有一个java类,它将csv转换为shapefile,它工作得很好......现在我想创建一个带有bouttons的w java应用程序,可以这样...所以我必须将按钮关联到csv2shape类.. 。我真的不知道该怎么做..这里是我的代码的一部分,解释了我试图做的事情并且它有效吗
public class GestionTournee {
private JFrame frame;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
GestionTournee window = new GestionTournee();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public GestionTournee() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Button button = new Button("New button");
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
new Thread(new Runnable() {
public void run() {
new Csv2shape();
}
}).start();
}
});
frame.getContentPane().add(button, BorderLayout.NORTH);
}
}
以及csv转换器的代码
public class Csv2shape {
public static void main(String[] args) throws Exception {
// Set cross-platform look & feel for compatability
UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
File file = JFileDataStoreChooser.showOpenFile("csv", null);
if (file == null) {
return;
}
/*
* We use the DataUtilities class to create a FeatureType that will describe the data in our
* shapefile.
*
* See also the createFeatureType method below for another, more flexible approach.
*/
final SimpleFeatureType TYPE = DataUtilities.createType( "Location",
"the_geom:Point:srid=4326," + // <- the geometry attribute: Point type
"Date:String," + // <- a String attribute
"NombreAr:Double," + // <- a String attribute
"vitesse:String," + // <- a String attribute
"distance:Double" // a number attribute
);
System.out.println("TYPE:"+TYPE);
/* A list to collect features as we create them.
*/
List<SimpleFeature> features = new ArrayList<SimpleFeature>();
/*
* GeometryFactory will be used to create the geometry attribute of each feature,
* using a Point object for the location.
*/
GeometryFactory geometryFactory = JTSFactoryFinder.getGeometryFactory();
SimpleFeatureBuilder featureBuilder = new SimpleFeatureBuilder(TYPE);
BufferedReader reader = new BufferedReader(new FileReader(file));
try {
/* First line of the data file is the header */
String line = reader.readLine();
System.out.println("Header: " + line);
boolean foundAnyRowHigherThan4 = false;
double s =0;
boolean foundAnyRowHigherThan5 = false;
boolean checker = true;
double Longitude2 = 0;
double Latitude2 = 0;
double Dis = 0;
double dis = 0;
for (line = reader.readLine(); line != null; line = reader.readLine()) {
if (line.trim().length() > 0) { // skip blank lines
{
if(checker){
String currentTokens[] = line.split("\\,");
String currentName1 = currentTokens[0].trim();
String currentName2 = currentTokens[1].trim();
Longitude2 = Double.parseDouble(currentTokens[2]);
Latitude2 = Double.parseDouble(currentTokens[3]);
checker = false ;
continue;
}
String tokens[] = line.split("\\,");
String name1 = tokens[0].trim();
String name2 = tokens[1].trim();
double longitude = Double.parseDouble(tokens[2]);
double latitude = Double.parseDouble(tokens[3]);
String speedString = tokens[5].trim();
double dist = Double.parseDouble(tokens[4]);
float speedFloat = Float.parseFloat(speedString);
if(foundAnyRowHigherThan5 || speedFloat > 5.0) {
// a partir de ce point on ajoutera touts les points ,
if(!foundAnyRowHigherThan5) {
foundAnyRowHigherThan5 = true;
}
if(foundAnyRowHigherThan4 || speedFloat < 1.0) {
// a partir de ce point on ajoutera touts les points d'arrets,
if(speedFloat <0.1)
s = s+1 ;
else
foundAnyRowHigherThan4 = true;
}
/* Longitude (= x coord) first ! */
Point point = geometryFactory.createPoint(new Coordinate(longitude, latitude));
featureBuilder.add(point);
double earthRadius = 6371; //km pour la changer en metres faut ajouter 000 a la fin
double dLat = Math.toRadians(latitude-Latitude2);
double dLng = Math.toRadians(longitude-Longitude2);
double a = Math.sin(dLat/2) * Math.sin(dLat/2) +
Math.cos(Math.toRadians(Latitude2)) * Math.cos(Math.toRadians(latitude)) *
Math.sin(dLng/2) * Math.sin(dLng/2);
double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
double d = (float) (earthRadius * c);
dis = d+dis;
featureBuilder.add(name1);
featureBuilder.add(s);
featureBuilder.add(speedString);
featureBuilder.add(dis);
SimpleFeature feature = featureBuilder.buildFeature(null);
features.add(feature);
Longitude2 = longitude;
Latitude2 = latitude;
}
}
}
}
}
finally {
reader.close();
}
任何帮助我都会感激不尽......谢谢
答案 0 :(得分:1)
您正在正确连接事件处理程序 (稍后会详细介绍)。
主要问题是您的CSV逻辑在main
的{{1}}方法范围内。您没有在事件处理程序中调用此方法,因为您只是在执行Csv2shape
。
要修复这个,您可以简单地执行:new Csv2shape();
,这会调用具有逻辑的Csv2shape.main(null);
方法,但不推荐这样做(你应该有) ,最多,代码的一个主要方法,你应该避免自己调用它)。因此,解决您的问题,我建议您将main
中的逻辑移动到其他方法,例如main
。在private void createCSV()
构造函数中,您调用此方法:Csv2shape
。
上面的应该触发你需要的电话。
正如我在开始时所说,事件处理程序的创建主要是正确。问题在于您正在处理事件调度线程(EDT)上的文件,该线程处理所有UI操作,因此,在此线程上执行大量非UI操作将对您的用户产生不利影响&# 39;经历。
要解决此问题,只需在新线程中启动public Csv2shape() { this.createCSV();}
构造函数即可。这将从EDT中取消负担:
因此,将Csv2shape
替换为:
new Csv2shape();
编辑:
如果您在哪里关注我的示例,这就是new Thread(new Runnable() {
@Override
public void run() {
new Csv2shape();
}
}).start();
类的外观:
Csv2shape
答案 1 :(得分:0)
我在actionPerformed方法中看到你只是制作Csv2shape类的对象。为了完成您的工作,请将代码从main方法拉到其他方法,例如convertToShape()。现在从你的actionPerformed方法调用你的this方法作为新的Csv2shape()。convertToShape()。这会奏效。