1

我有个问题。我正在创建一个弧长改变的应用程序,我希望该弧在边框内,但是当我改变弧的长度时,它会居中,所以它看起来不像一个被填充的圆圈。实际上我需要的是一个弧并通过它的最大长度(360)计算它的位置(边框的中心)。有人可以帮我解决这个问题吗?非常感谢。

4

2 回答 2

3

在 BorderPane(或 StackPane)内,对齐是根据形状的边界框(左上角、中心、底部等)完成的。因此圆弧的中心点会移动,不会给你想要的效果。

相反,将弧线放在 AnchorPane 内(如果要使用 BorderPane,请将 AnchorPane 放在 BorderPane 的区域(左、右、中心等)内)。即使您更改弧长,这也将固定弧的中心点,并为您提供一个圆被增加的弧长填充的效果。

于 2014-12-11T20:40:15.660 回答
3

创建一个组。在组中放置一个整圆大小的矩形(例如,将矩形的高度和宽度设置为圆的直径),然后将圆弧添加到组中,并将圆弧布局位置设置为圆的半径。将组放在 StackPane 中,以便固定大小的组将在可调整大小的区域内居中。将 StackPane 放在 BorderPane 的中心。将 StackPane 的最小大小设置为零,以便它的大小可以小于 Group,保持 Group 居中并在其可见范围内裁剪。在 BorderPane 的底部添加一些控件,以便您可以动态控制圆弧的长度。

弧形图像

import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.*;
import javafx.scene.control.Slider;
import javafx.scene.layout.*;
import javafx.scene.paint.Color;
import javafx.scene.shape.*;
import javafx.stage.Stage;

public class ArcControl extends Application {
    @Override
    public void start(Stage stage) throws Exception {
        CenteredArc centeredArc = new CenteredArc();
        ArcControls arcControls = new ArcControls(centeredArc.getArc());

        BorderPane layout = new BorderPane();
        layout.setCenter(centeredArc.getArcPane());
        layout.setBottom(arcControls.getControlPane());

        stage.setScene(new Scene(layout));
        stage.show();
    }

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

class CenteredArc {
    private static final double INSET = 10;

    private static final double ARC_RADIUS = 100;
    private static final double INITIAL_ARC_LENGTH  = 60;
    private static final double ARC_STROKE_WIDTH = 10;
    private static final double ARC_REGION_SIZE =
            ARC_RADIUS * 2 + ARC_STROKE_WIDTH + INSET * 2;

    private final Arc arc;
    private final Pane arcPane;

    public CenteredArc() {
        // Create the arc.
        arc = new Arc(
                ARC_REGION_SIZE / 2, ARC_REGION_SIZE / 2,
                ARC_RADIUS, ARC_RADIUS,
                0,
                INITIAL_ARC_LENGTH
        );
        arc.setStrokeWidth(10);
        arc.setStrokeLineCap(StrokeLineCap.ROUND);
        arc.setStroke(Color.FORESTGREEN);
        arc.setFill(Color.POWDERBLUE);

        // Create a background fill on which the arc will be centered.
        // The paint of the background fill can be set to Color.TRANSPARENT
        // if you don't want the fill to be seen.
        final double fillSize = ARC_RADIUS * 2 + arc.getStrokeWidth() + INSET * 2;
        Rectangle fill = new Rectangle(fillSize, fillSize, Color.PINK);

        // Place the fill and the arc in the group.
        // The Group will be a fixed sized matching the fill size.
        Group centeredArcGroup = new Group(fill, arc);

        // place the arc group in a StackPane so it is centered in a resizable region.
        arcPane = new StackPane(centeredArcGroup);
        arcPane.setPadding(new Insets(INSET));
        arcPane.setMinSize(0, 0);
        arcPane.setStyle("-fx-background-color: papayawhip;");
    }

    public Arc getArc() {
        return arc;
    }

    public Pane getArcPane() {
        return arcPane;
    }
}

// helper class which can use a slider to control an arc.
class ArcControls {
    private static final double INSET = 10;

    private final Slider slider;
    private final VBox controlPane;

    public ArcControls(Arc arc) {
        slider = new Slider(0, 360, arc.getLength());
        controlPane = new VBox(
                slider
        );
        controlPane.setPadding(
                new Insets(INSET)
        );
        controlPane.setStyle(
                "-fx-background-color: palegreen;"
        );

        arc.lengthProperty().bind(slider.valueProperty());
    }

    public Slider getSlider() {
        return slider;
    }

    public VBox getControlPane() {
        return controlPane;
    }
}
于 2014-12-11T23:05:21.000 回答