我有两个Polygon类对象,我想使用javafx.scene.shape.Shape中的union()方法联合起来,我需要输出也是Polygon类对象。我找到了几个输出为Path对象的例子:
Path path = Path.union(shape1, shape2)
或者可以将路径转换为多边形吗?我试着去谷歌,但没有运气。
答案 0 :(得分:0)
这就是我所说的。
import java.util.ArrayList;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.AnchorPane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Polygon;
import javafx.scene.shape.Shape;
import javafx.stage.Stage;
public class JavaFXApplication31 extends Application {
@Override
public void start(Stage primaryStage) {
Polygon p1 = new Polygon();
p1.getPoints().addAll(new Double[]{
60.0, 60.0,
80.0, 70.0,
70.0, 80.0 });
Polygon p2 = new Polygon();
p2.getPoints().addAll(new Double[]{
40.0, 40.0,
60.0, 50.0,
50.0, 60.0 });
AnchorPane root = new AnchorPane();
Button btn = new Button();
btn.setText("Click me!'");
btn.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
System.out.println("Polygon unioned!");
Shape union = Polygon.union(p1, p2);
union.setFill(Color.BLUE);
root.getChildren().add(union);
}
});
Button btn2 = new Button();
btn2.setText("New Polygon union!'");
btn2.setLayoutX(100);
btn2.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
Polygon p3 = createNewPolygonUnion(p1, p2);
p3.setFill(Color.RED);
root.getChildren().add(p3);
}
});
root.getChildren().addAll(p1, p2, btn, btn2);
Scene scene = new Scene(root, 300, 250);
primaryStage.setTitle("Hello World!");
primaryStage.setScene(scene);
primaryStage.show();
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
Polygon createNewPolygonUnion(Polygon one, Polygon two)
{
Polygon p3 = new Polygon();
ArrayList<Double> onePoints = new ArrayList();
ArrayList<Double> twoPoints = new ArrayList();
ArrayList<Double> threePoints = new ArrayList();
for(int i = 0; i < one.getPoints().size(); i++)
{
onePoints.add(one.getPoints().get(i));
twoPoints.add(two.getPoints().get(i));
}
for(int i = 0; i < onePoints.size(); i++)
{
threePoints.add(onePoints.get(i) + twoPoints.get(i));
}
p3.getPoints().addAll(threePoints);
return p3;
}
}
原始的两个多边形。
两个原始多边形的联盟。两个多边形的联合没有 导致多边形。
从我创建的方法联合。这个方法使用了来自的点 原始的两个多边形并创建一个新的多边形。
答案 1 :(得分:0)
当您获取两个多边形的并集时,您将获得一个Path对象,如您所述。可以将此路径对象转换为Polygon对象(不是最好的代码,但可行)。
//createing both polygon objects p1 and p2
Polygon p1 = new Polygon();
p1.getPoints().addAll(new Double[]{
0.0, 0.0,
20.0, 10.0,
10.0, 20.0 });
Polygon p2 = new Polygon();
p2.getPoints().addAll(new Double[]{
10.0, 0.0,
25.0, 10.0,
5.0, 20.0 });
// Taking the union that results in a Path object
Path p3 = (Path) Polygon.union(p1, p2);
// Array of points for the new polygon
Double[] points = new Double[(p3.getElements().size() - 1)*2];
int i = 0;
// going through all the path elements in the path and adding the x and y coordinates to points
for(PathElement el : p3.getElements()){
if(el instanceof MoveTo){
MoveTo mt = (MoveTo) el;
points[i] = mt.getX();
points[i+1] = mt.getY();
}
if(el instanceof LineTo){
LineTo lt = (LineTo) el;
points[i] = lt.getX();
points[i+1] = lt.getY();
}
i += 2;
}
// creating new Polygon with these points
Polygon newPolygon = new Polygon();
newPolygon.getPoints().addAll(points);
如果多边形不相交,这可能不起作用,但应该起作用。如果没有交集,您将有两个MoveTo语句而不是一个。