Ответ 1
Я не знаю наверняка, если это то, что вы хотели, но я использовал Group
для этого. Я следил за кодом отсюда. Проверьте это обновление. Я попробую объяснить это завтра, если это сработает.
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.ResourceBundle;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.Group;
import javafx.scene.control.Button;
import javafx.scene.effect.PerspectiveTransform;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.scene.shape.Line;
/**
*
* @author blj0011
*/
public class FXMLDocumentController implements Initializable
{
@FXML
VBox vBox;
@FXML
AnchorPane anchorPane;
@FXML
Button button;
Group group = new Group();
Group group2 = new Group();
PerspectiveTransform pt = new PerspectiveTransform();
Line line_1, line_2, line_3, line_4, middle_1, middle_2;
private int numberOfClicks = 0;
private int W = 10;
//Store coordinates of the corresponding clicks
private double cx1, cy1,
cx2, cy2,
cx3, cy3,
cx4, cy4,
cx5, cy5;
@Override
public void initialize(URL location, ResourceBundle resources)
{
button.setDisable(true);
anchorPane.getChildren().addAll(group);
button.setOnAction((event) -> {
pt.setUlx(cx1);
pt.setUly(cy1);
pt.setUrx(cx2);
pt.setUry(cy2);
pt.setLrx(cx3);
pt.setLry(cy3);
pt.setLlx(cx4);
pt.setLly(cy4);
group.setEffect(pt);
});
anchorPane.addEventHandler(MouseEvent.MOUSE_CLICKED, (MouseEvent event) -> {
numberOfClicks++;
switch (numberOfClicks) {
case 1:
cx1 = event.getX();
cy1 = event.getY();
System.out.println(cx1 + " : " + cy1);
break;
case 2:
cx2 = event.getX();
cy2 = event.getY();
line_1 = new Line(cx1, cy1, cx2, cy2);
line_1.setStrokeWidth(W);
group.getChildren().add(line_1);
break;
case 3:
cx3 = event.getX();
cy3 = event.getY();
line_2 = new Line(cx2, cy2, cx3, cy3);
line_2.setStrokeWidth(W);
group.getChildren().add(line_2);
break;
case 4:
cx4 = event.getX();
cy4 = event.getY();
line_3 = new Line(cx3, cy3, cx4, cy4);
line_4 = new Line(cx4, cy4, cx1, cy1);
line_3.setStrokeWidth(W);
line_4.setStrokeWidth(W);
middle_1 = new Line((cx1 + cx2) / 2, (cy1 + cy2) / 2, (cx3 + cx4) / 2, (cy3 + cy4) / 2);
middle_2 = new Line((cx2 + cx3) / 2, (cy2 + cy3) / 2, (cx4 + cx1) / 2, (cy4 + cy1) / 2);
middle_1.setStrokeWidth(W);
middle_2.setStrokeWidth(W);
group.getChildren().addAll(line_3, line_4, middle_1, middle_2);
button.setDisable(false);
break;
case 5:
List<Double> centerOfTransform = findCenterOfTransForm(pt);
cx5 = event.getX();
cy5 = event.getY();
List<Double> list = new ArrayList();
list.add(cx5);
list.add(cy5);
List<Double> changeInCenterXAndCenterY = findChangeInCenterXAndY(centerOfTransform, list);
PerspectiveTransform ptCircle = new PerspectiveTransform();
ptCircle.setUlx(cx1 + changeInCenterXAndCenterY.get(0));
ptCircle.setUly(cy1 + changeInCenterXAndCenterY.get(1));
ptCircle.setUrx(cx2 + changeInCenterXAndCenterY.get(0));
ptCircle.setUry(cy2 + changeInCenterXAndCenterY.get(1));
ptCircle.setLrx(cx3 + changeInCenterXAndCenterY.get(0));
ptCircle.setLry(cy3 + changeInCenterXAndCenterY.get(1));
ptCircle.setLlx(cx4 + changeInCenterXAndCenterY.get(0));
ptCircle.setLly(cy4 + changeInCenterXAndCenterY.get(1));
System.out.println("cx5: " + cx5 + " cy5: " + cy5);
Circle circle = new Circle();
circle.setRadius(30);
circle.setFill(Color.TRANSPARENT);
circle.setStroke(Color.BLACK);
circle.setStrokeWidth(W);
circle.setCenterX(cx5);
circle.setCenterY(cy5);
circle.setEffect(ptCircle);
anchorPane.getChildren().add(circle);
System.out.println("centerx: " + circle.getTranslateX()+ " centery: " + circle.getTranslateY());
break;
default:
group.getChildren().clear();
numberOfClicks = 0;
break;
}
});
}
List<Double> findCenterOfTransForm(PerspectiveTransform pt)
{
List<Double> tempList = new ArrayList();
tempList.add((pt.getUlx() + pt.getUrx() + pt.getLrx() + pt.getLlx()) / 4);
tempList.add((pt.getUly() + pt.getUry() + pt.getLry() + pt.getLly()) / 4);
return tempList;
}
List<Double> findChangeInCenterXAndY(List<Double> originalXAndY, List<Double> newXAndY)
{
List<Double> tempList = new ArrayList();
tempList.add(newXAndY.get(0) - originalXAndY.get(0));
tempList.add(newXAndY.get(1) - originalXAndY.get(1));
return tempList;
}
}