我正在尝试在拖动鼠标时在javafx中绘制一个带圆角的简单矩形。 第一期:我不能让左上角圆润。 第二个问题:我希望我的应用程序能够绘制漂亮的全圆角矩形,而不是像我的图片中的第二个。 请问,我该如何解决这些问题? 在此先感谢您的帮助。
这是我的代码:
import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.BorderPane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;
public class Main extends Application {
public static void main(String[] args) {
launch(args);
}
public void start(Stage stage){
stage.setTitle("Test");
root = new BorderPane();
Scene scene = new Scene(root, 400, 400);
paintings = new Group();
stage.setScene(scene);
canvas = new Rectangle(0, 0, stage.getScene().getWidth(), stage.getScene().getHeight());
canvas.setFill(Color.WHITE);
addHandlers();
root.setCenter(canvas);
root.getChildren().add(paintings);
stage.show();
}
private void paintRectangle(){
roundedRect = new Rectangle(pressedX,
pressedY,
draggedX - pressedX,
draggedY - pressedY);
roundedRect.setFill(Color.RED);
roundedRect.setArcHeight(40);
roundedRect.setArcWidth(40);
paintings.getChildren().add(roundedRect);
}
private void addHandlers(){
canvas.setOnMousePressed(new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent me) {
pressedX = me.getX();
pressedY = me.getY();
}
});
canvas.setOnMouseReleased(new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent me) {
/* If I make the drawing in here, I get round corners, but it doesn't get drawn while dragging*/
//paintRectangle();
}
});
canvas.setOnMouseDragged(new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent me) {
draggedX = me.getX();
draggedY = me.getY();
paintRectangle();
}
});
}
double pressedX, pressedY;
double draggedX, draggedY;
Rectangle canvas;
Group paintings;
Rectangle roundedRect;
BorderPane root;
}
这是我的照片:
答案 0 :(得分:3)
我无法检查您的代码,但这是另一种方法。您可以将其与您的代码合并:
import javafx.application.Application;
import javafx.beans.property.SimpleDoubleProperty;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.BorderPane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;
public class Daer extends Application {
BorderPane pane;
Rectangle rect;
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);
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);
stage.show();
}
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());
} 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);
r.setArcHeight(40);
r.setArcWidth(40);
return r;
}
public static void main(String[] args) {
launch(args);
}
}