我有一个问题。 我正在创建一个弧长发生变化的应用程序,我希望该弧位于边框内,但是当我更改弧的长度时,它会居中,因此它看起来不像一个正在填充的圆。 实际上我需要的是一个弧并通过它的最大长度(360)计算它的位置(边框的中心)。 有人可以帮我解决这个问题吗? 非常感谢。
最佳答案
创建一个组。在组中放置一个矩形,其大小为一个完整的圆(例如矩形的高度和宽度设置为圆的直径),然后将圆弧添加到组中,圆弧布局位置设置为圆的半径。将组放在 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;
}
}
关于JavaFX 在边框 Pane 中居中一个可调整大小的圆弧,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27418189/