如何在Java-fx中创建这样的视图?

时间:2019-07-15 11:14:53

标签: javafx javafx-8

我对此观点有些疑问。我需要在JavaFX中添加此“边框”并将圆圈分为2部分。

public void start(Stage primaryStage){

Circle mycircle = new Circle(200,200,200);
mycircle.setFill(Color.GREEN);

BorderPane root = new BorderPane();
root.setCenter(mycircle);

}

我没有错误,但这不是我想要的视图。所以有人可以帮我吗

Image

顺便说一句,对图像质量不好感到遗憾

2 个答案:

答案 0 :(得分:1)

这能满足您的需求吗?

import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.layout.BorderPane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;

public class HalfCircleDemo extends Application {

    @Override
    public void start(final Stage stage) {
        BorderPane root = new BorderPane();

        Group circleGroup = new Group();

        Circle greenCircle = new Circle(200,200,200);
        greenCircle.setFill(Color.GREEN);

        Circle blueCircle = new Circle(200,200,200);
        blueCircle.setFill(Color.BLUE);

        Rectangle clip = new Rectangle(400, 200);
        greenCircle.setClip(clip);

        circleGroup.getChildren().setAll(blueCircle, greenCircle);

        root.setCenter(circleGroup);

        Scene scene = new Scene(root);

        stage.setScene(scene);

        stage.show();

    }

    public static void main(String[] args) {
        launch(args);
    }

}

答案 1 :(得分:1)

您可以将PathsArcTo元素一起使用:

private static Group createHalfCircles(double radius) {
    Path upperHalf = new Path(
            new MoveTo(0, radius),
            new ArcTo(radius, radius, 0, 2*radius, radius, true, false),
            new ClosePath());
    upperHalf.setFill(Color.PURPLE);
    upperHalf.setStroke(null);

    Path lowerHalf = new Path(
            new MoveTo(2 * radius, radius),
            new ArcTo(radius, radius, 0, 0, radius, true, false),
            new ClosePath());
    lowerHalf.setFill(Color.GREEN);
    lowerHalf.setStroke(null);

    return new Group(upperHalf, lowerHalf);
}

@Override
public void start(Stage primaryStage) throws Exception {

    Scene scene = new Scene(createHalfCircles(50));
    primaryStage.setScene(scene);
    primaryStage.show();
}