gpt4 book ai didi

java - 如何创建 JavaFx 应用程序实例并推送自己的参数?

转载 作者:行者123 更新时间:2023-12-02 12:09:38 26 4
gpt4 key购买 nike

我正在尝试使用 Google Maps Api + JavaFx 创建 map 。在主类中,我读取 JSON 文件,然后创建集群,程序的要点是将这些集群显示为标记,其中包含位置数量。因此,在主类中,我读取 JSON 文件,然后创建 DBSCANClusterer clusterer = new DBSCANClusterer(coordinates, 2, 2); 并且我必须将其推送到类 GoogleMap 中,其中JavaFX正在创建webView等等。我还用它在我的 html 文件中的脚本中使用 java 代码:

JSObject jsobj = (JSObject) webView.getEngine()
.executeScript("window");
jsobj.setMember("BrowserJavaObject", new BrowserJavaObject(clusterer));

然后,必须在所有聚类计算所在的 BrowserJavaObject 类中进一步推进相同的操作。我尝试创建 GoogleMap 类的对象,但这不起作用。那么如何让它发挥作用呢?或者说这有可能吗?谢谢您的建议。

JSONMain类:

public class JsonMain {

static List<Coordinate> coordinates = new ArrayList<>();

private static final String ITEMS_NAME = "items";
private static final String LATITUDE_PROPERTY = "latitude";
private static final String LONGITUDE_PROPERTY = "longitude";
private static final String CRASH_NAME = "em_type_name";

static void parseCrashCoordinates(final JsonReader jsonReader, final ICoordinatesListener listener)
throws IOException {
// Reading JSON file
}

// Collecting all coordinates in ArrayList.
private static void testCollecting()
throws IOException {
// List<Coordinate> coordinates = new ArrayList<>();
readAndParse((lat, lng) -> coordinates.add(new Coordinate(lat, lng)));
System.out.println(coordinates.size());
}

public static void main(String[] args) throws IOException, URISyntaxException {
testCollecting();


// Initialize our clustering class with locations, minimum points in cluster and max Distance
DBSCANClusterer clusterer = new DBSCANClusterer(coordinates, 2, 2);

GoogleMap gm = new GoogleMap(clusterer);
gm.launch(GoogleMap.class);
}

GoogleMap 类:

public class GoogleMap extends Application {

private DBSCANClusterer clusterer ;

public GoogleMap(DBSCANClusterer c) {
this.clusterer = c;
}


@Override
public void start(Stage stage) throws MalformedURLException {

File file = new File("C:/Users/Evgeny/git/Diploma_MSU/diploma/html/map.html");
URL url222 = file.toURI().toURL();

WebView webView = new WebView();
final WebEngine webEngine = webView.getEngine();

JSObject jsobj = (JSObject) webView.getEngine()
.executeScript("window");
jsobj.setMember("BrowserJavaObject", new BrowserJavaObject(clusterer));

webEngine.load(url222.toString());

// create scene
stage.setTitle("Web Map");
Scene scene = new Scene(webView,1000,700, Color.web("#666970"));
stage.setScene(scene);
// show stage
stage.show();

}
}

BrowserJavaObject 类:

public class BrowserJavaObject {

private DBSCANClusterer clusterer ;

public BrowserJavaObject(DBSCANClusterer c) {
this.clusterer = c;
}

public String showMarkers() {
ArrayList<ArrayList<Coordinate>> clusters_raw= this.clusterer.performClustering();
ArrayList<Cluster> clusters = new ArrayList<>();

String pointsArray = " [ ";
for(int i=0; i<clusters_raw.size(); i++) {
Cluster c = new Cluster(clusters_raw.get(i));
clusters.add(c);

pointsArray += c.getLocationAsArray() + " , ";
}
pointsArray += "]";

System.out.println("Number Of Clusters Created: "+clusters.size());
return pointsArray;

}
}

ma​​p.html:

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Map</title>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
<style type="text/css">
html { height: 100% }
body { height: 100%; margin: 0; padding: 0 }
#map_canvas { height: 100% }
</style>
<script async defer type="text/javascript"
src="https://maps.googleapis.com/maps/api/js?callback=loadmap">
</script>
</head>
<body onload="loadmap()">
<div id="map_canvas" style="width:100%; height:100%"></div>
<script>
var map;
function loadmap(){

var options = {
zoom: 16,
center: new google.maps.LatLng(55.7558, 37.6173),
mapTypeId: google.maps.MapTypeId.ROADMAP
}
map = new google.maps.Map(document.getElementById("map_canvas"), options);

var markers = [];

// Call the Java code to calculate clusters data, and save the returned clusters into a variable
var markers_data = BrowserJavaObject.showMarkers();

for( var i=0; i<markers_data.length; i++ ){
var position = markers_data[i];
var icon = "data:image/svg+xml,%3Csvg%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%20width%3D%2238%22%20height%3D%2238%22%20viewBox%3D%220%200%2038%2038%22%3E%3Cpath%20fill%3D%22%23a22%22%20stroke%3D%22%23ccc%22%20stroke-width%3D%22.5%22%20d%3D%22M34.305%2016.234c0%208.83-15.148%2019.158-15.148%2019.158S3.507%2025.065%203.507%2016.1c0-8.505%206.894-14.304%2015.4-14.304%208.504%200%2015.398%205.933%2015.398%2014.438z%22%2F%3E%3Ctext%20transform%3D%22translate%2819%2018.5%29%22%20fill%3D%22%23fff%22%20style%3D%22font-family%3A%20Arial%2C%20sans-serif%3Bfont-weight%3Abold%3Btext-align%3Acenter%3B%22%20font-size%3D%2212%22%20text-anchor%3D%22middle%22%3E"
+ position[2]
+ "%3C%2Ftext%3E%3C%2Fsvg%3E";

// if(zoom > 11) icon = null; //Default to marker with no number if at city zom level

markers.push( new google.maps.Marker({
position: new google.maps.LatLng( position[0], position[1] ),
map: map,
title: position[2],
text: position[2],
icon: icon
})
);
}
}
</script>
</body>
</html>

因此 map 已加载,但那里没有集群。最终,所有这些聚类方法都不会被调用,因为我的 clusterer 对象没有在其他类中进一步传递。

非常感谢!

最佳答案

您使用 Application 类的方式是错误的。 Application 类代表您的整个应用程序并负责其生命周期;特别是,它有一个 start() 方法,当通过调用 launch() 启动 JavaFX 工具包时,会为您调用该方法。 Application 类并不代表 UI 的特定部分(这正是您的 GoogleMap 类的用途)。

因此,您应该使 JsonMain 成为 Application 的子类,而不是 GoogleMap,并且您应该将启动代码移至(恰当命名的) start() 方法:

public class GoogleMap {

private DBSCANClusterer clusterer ;

private final WebView view ;

public GoogleMap(DBSCANClusterer c) throws MalformedURLException {
this.clusterer = c;

File file = new File("C:/Users/Evgeny/git/Diploma_MSU/diploma/html/map.html");
URL url222 = file.toURI().toURL();

view = new WebView();
final WebEngine webEngine = webView.getEngine();

JSObject jsobj = (JSObject) webView.getEngine()
.executeScript("window");
jsobj.setMember("BrowserJavaObject", new BrowserJavaObject(clusterer));

webEngine.load(url222.toString());

}

public Node getView() {
return view ;
}

}

public class JsonMain extends Application {

static List<Coordinate> coordinates = new ArrayList<>();

private static final String ITEMS_NAME = "items";
private static final String LATITUDE_PROPERTY = "latitude";
private static final String LONGITUDE_PROPERTY = "longitude";
private static final String CRASH_NAME = "em_type_name";

static void parseCrashCoordinates(final JsonReader jsonReader, final ICoordinatesListener listener)
throws IOException {
// Reading JSON file
}

// Collecting all coordinates in ArrayList.
private static void testCollecting()
throws IOException {
// List<Coordinate> coordinates = new ArrayList<>();
readAndParse((lat, lng) -> coordinates.add(new Coordinate(lat, lng)));
System.out.println(coordinates.size());
}

public static void main(String[] args) {
launch(args);
}

@Override
public void start(Stage stage)
throws IOException, URISyntaxException, MalformedURLException {

testCollecting();


// Initialize our clustering class with locations, minimum points in cluster and max Distance
DBSCANClusterer clusterer = new DBSCANClusterer(coordinates, 2, 2);

GoogleMap gm = new GoogleMap(clusterer);

// create scene
stage.setTitle("Web Map");
Scene scene = new Scene(gm.getView(), 1000, 700, Color.web("#666970"));
stage.setScene(scene);
// show stage
stage.show();

}

}

您可能应该进一步重构,以便正确分离您的关注点;例如解析数据等可能不是您的 Application 类的工作。但这至少可以让您将参数传递给您的 GoogleMap 类并让您启动应用程序以预期的方式。

关于java - 如何创建 JavaFx 应用程序实例并推送自己的参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46633632/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com