我在拖动鼠标时使用此代码绘制矩形。问题是我只能从左到右画画。
这是我的代码:
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);
}
}
任何人都可以帮助我从右到左绘制其他方向,左右向左到左边。
答案 0 :(得分:1)
这是你需要做的:
拖动鼠标时:获取dx = mouseEvent.getX() - initial X
;如果dx < 0
,那么您需要setTranslateX(dx)
和setWidth(-dx)
其他setTranslateX(0)
和setWidth(dx)
对Y坐标执行相同操作,但这次使用的是高度而不是宽度;
这就是全部。
答案 1 :(得分:0)
我将代码更换为: EventHandler mouseHandler = new EventHandler(){
@Override
public void handle(MouseEvent mouseEvent) {
if (mouseEvent.getEventType() == MouseEvent.MOUSE_PRESSED) {
rectinitX.set(mouseEvent.getX());
rectinitY.set(mouseEvent.getY());
} else if (mouseEvent.getEventType() == MouseEvent.MOUSE_DRAGGED) {
Double width = mouseEvent.getX() - rectinitX.getValue();
if (width < 0) {
rectX.set(mouseEvent.getX());
rect.setTranslateX(width);
rect.widthProperty().bind(rectinitX.subtract(rectX));
System.out.println(rect.getWidth());
} else {
rectX.set(mouseEvent.getX());
rect.setTranslateX(0);
rect.widthProperty().bind(rectX.subtract(rectinitX));
}
} 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);
}
}
};
我只是想首先尝试宽度值的解决方案,但它似乎不起作用。你对我的实施有什么意见吗?
答案 2 :(得分:0)
您缺少高度参数。
if (event.getEventType() == MouseEvent.MOUSE_PRESSED) {
rect.setX(event.getX());
rect.setY(event.getY());
rectinitX.set(event.getX());
rectinitY.set(event.getY());
} else if (event.getEventType() == MouseEvent.MOUSE_DRAGGED) {
Double dx = event.getX() - rectinitX.getValue();
Double dy = event.getY() - rectinitY.getValue();
if (dx < 0) {
rectX.set(event.getX());
rect.setTranslateX(dx);
rect.widthProperty().bind(rectinitX.subtract(rectX));
} else {
rectX.set(event.getX());
rect.setTranslateX(0);
rect.widthProperty().bind(rectX.subtract(rectinitX));
}
if(dy < 0) {
rectY.set(event.getY());
rect.setTranslateY(dy);
rect.heightProperty().bind(rectinitX.subtract(rectX));
} else {
rectY.set(event.getY());
rect.setTranslateY(0);
rect.heightProperty().bind(rectY.subtract(rectinitY));
}
} else if (event.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);
}}}};
这是在不同方向绘制的确切代码,但不幸的是从左下到右顶部工作,我无法弄明白。 在默认情况下从左到右完美地工作,但从右到左绘制时存在代码错误。 如果有人能够弄清楚,我会被大肆宣传
答案 3 :(得分:0)
应该阅读
if(dy < 0)
{
rectY.set(mouseEvent.getY());
rect.setTranslateY(dy);
rect.heightProperty().bind(rectinitY.subtract(rectY));
}
答案 4 :(得分:0)
不知道它是否对任何人都有效,但对我来说却没有运气。但是,这在JavaFX 8中确实对我有用:
if( event.getEventType() == MouseDragEvent.MOUSE_DRRAGGED ) {
double deltaX = event.getX() - initialPt.getX();
double deltaY = event.getY() - initialPt.getY();
if( deltaX < 0 ) {
rect.setX( event.getX() );
rect.setWidth( -deltaX );
}
else {
rect.setX( initialPt.getX() );
rect.setWidth( event.getX() - initialPt.getX() );
}
if( deltaY < 0 ) {
rect.setY( event.getY() );
rect.setHeight( -deltaY );
}
else {
rect.setY( initialPt.getY() );
rect.setHeight( event.getY() - initialPt.getY() );
}
}
无需致电setTranslateX()
或setTranslateY()
。不知道这是否与您使用的FX版本有关。