我使用此代码在拖动鼠标时绘制一个矩形。问题是我只能从左到右绘制。
这是我的代码:
public class functionalTest extends Application {
BorderPane pane;
Rectangle rect;
Group group;
SimpleDoubleProperty rectinitX = new SimpleDoubleProperty();
SimpleDoubleProperty rectinitY = new SimpleDoubleProperty();
SimpleDoubleProperty rectX = new SimpleDoubleProperty();
SimpleDoubleProperty rectY = new SimpleDoubleProperty();
@Override
public void start(Stage stage) {
pane = new BorderPane();
Scene scene = new Scene(pane, 800, 600);
stage.setScene(scene);
group = new Group();
Circle circle = new Circle(200, 200, 25);
circle.setFill(Color.HOTPINK);
group.getChildren().add(circle);
scene.setOnMouseDragged(mouseHandler);
scene.setOnMousePressed(mouseHandler);
scene.setOnMouseReleased(mouseHandler);
rect = getNewRectangle();
rect.widthProperty().bind(rectX.subtract(rectinitX));
rect.heightProperty().bind(rectY.subtract(rectinitY));
pane.getChildren().add(rect);
pane.getChildren().add(group);
stage.show();
ArrayList<Node> containedNodesArray = new ArrayList<Node>();
containedNodesArray = Main.dragBoxSelection(group, rect);
if (containedNodesArray.size() > 0) {
System.out.println("Success");
}
}
EventHandler<MouseEvent> mouseHandler = new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent mouseEvent) {
if (mouseEvent.getEventType() == MouseEvent.MOUSE_PRESSED) {
rect.setX(mouseEvent.getX());
rect.setY(mouseEvent.getY());
rectinitX.set(mouseEvent.getX());
rectinitY.set(mouseEvent.getY());
} else if (mouseEvent.getEventType() == MouseEvent.MOUSE_DRAGGED) {
rectX.set(mouseEvent.getX());
rectY.set(mouseEvent.getY());
System.out.println("RECTX"+rectX.getValue());
System.out.println("RECTY"+rectY.getValue());
ArrayList<Node> containedNodesArray = new ArrayList<Node>();
containedNodesArray = Main.dragBoxSelection(group, rect);
if (containedNodesArray.size() > 0) {
System.out.println("Success");
}
} else if (mouseEvent.getEventType() == MouseEvent.MOUSE_RELEASED) {
// Clone the rectangle
Rectangle r = getNewRectangle();
r.setX(rect.getX());
r.setY(rect.getY());
r.setWidth(rect.getWidth());
r.setHeight(rect.getHeight());
pane.getChildren().add(r);
// Hide the rectangle
rectX.set(0);
rectY.set(0);
}
}
};
private Rectangle getNewRectangle() {
Rectangle r = new Rectangle();
r.setFill(Color.web("blue", 0.1));
r.setStroke(Color.BLUE);
return r;
}
public static void main(String[] args) {
launch(args);
}
}
请任何人都可以帮我从右上到左,从左到右,从右到左画其他方向。