我试图在拖动鼠标时在 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;
}
这是我的照片: