在Java中创建多个圆

在Java中创建多个圆,java,javafx,Java,Javafx,我在课堂上有一个项目,我需要展示一个只有三个圆环的交通灯。我从黄色的开始,然后尝试在一些随机的地方添加一个红色的,只是为了看看我是否能做到,但是黄色的是唯一显示的。我不知道红色圆圈是否在黄色圆圈的下方,但无论如何,对于我来说,为什么红色圆圈没有显示出来,这没有多大意义 package tryingGraphicsStuff; import javafx.application.Application; import javafx.stage.Stage; import javafx.scene

我在课堂上有一个项目,我需要展示一个只有三个圆环的交通灯。我从黄色的开始,然后尝试在一些随机的地方添加一个红色的,只是为了看看我是否能做到,但是黄色的是唯一显示的。我不知道红色圆圈是否在黄色圆圈的下方,但无论如何,对于我来说,为什么红色圆圈没有显示出来,这没有多大意义

package tryingGraphicsStuff;
import javafx.application.Application; 
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.layout.StackPane;
import javafx.scene.shape.Circle;
import javafx.scene.paint.*;
import javafx.scene.text.*;
import javafx.scene.control.*;

public class TryingGraphicsStuff extends Application{
    @Override
    public void start(Stage stage) throws Exception {
        // create circle
                Circle circle = new Circle(); 
                circle.setCenterX(150);
                circle.setCenterY(150);
                circle.setRadius(50);
                circle.setFill(Color.RED);
                // place on pane
                StackPane p = new StackPane();
                p.getChildren().add(circle);
                // ensure it stays centered if window resized
                //circle.centerXProperty().bind(p.widthProperty().divide(2)); 
                //circle.centerYProperty().bind(p.heightProperty().divide(2));

                Circle circleTwo = new Circle();
                circleTwo.setCenterX(400);
                circleTwo.setCenterY(400);
                circleTwo.setRadius(50);
                circleTwo.setFill(Color.YELLOW);
                // place on pane
                p.getChildren().add(circleTwo);


                // create scene from pane
                Scene scene = new Scene(p, 300, 1000);

                // place scene on stage
                stage.setTitle("Circle");
                stage.setScene(scene);
                stage.show();
    }
    public static void main (String [] args)
    {
        Application.launch(args);
    }

}

是的,你的两个圆圈重叠了。 您只需使用
VBox
而不是
StackPane
。它会解决你的问题

VBox p = new VBox();

实际上,我对上面的代码有点困惑。根据你的数字,红色的应该是显示的,而不是黄色的。您的场景只有300px宽,黄色圆圈的中心位置为400,这将使它看不见(半径仅为50)

增加场景大小或在视图中移动圆。

A“将其子对象从后向前堆叠”。(这里的堆栈以z坐标表示)。它是一个“布局窗格”,实际上为您管理子节点的放置。因此,圆的
centerX
centerY
属性将被忽略,并且它们按添加顺序依次出现(因此红色的在黄色的下面,您看到的唯一一个是黄色的)。默认情况下,“堆栈”窗格将它们居中

所有“布局窗格”为您定位节点。例如,
VBox
将在垂直堆栈中定位节点,第一个在顶部,第二个在下面,依此类推。因此,如果使用
VBox
而不是
StackPane
,则圆圈将一个接一个地出现(在y方向),但请注意,它们仍然不尊重
centerX
centerY
属性

窗格
类本身不管理其子节点的布局;因此,如果要使用形状对象的坐标,
窗格
可能是最好的选择<代码>组的行为类似,但采用其子边界并集的边界,因此其行为类似于窗格,但其局部坐标系不同

下面的演示显示了所有这些选项。同样,
窗格
将以直观的方式运行

import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Tab;
import javafx.scene.control.TabPane;
import javafx.scene.layout.Pane;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.stage.Stage;

public class CircleLayoutExample extends Application {

    @Override
    public void start(Stage primaryStage) {

        TabPane tabs = new TabPane();

        tabs.getTabs().add(createTab(new StackPane()));
        tabs.getTabs().add(createTab(new VBox()));
        tabs.getTabs().add(createTab(new Pane()));
        tabs.getTabs().add(createTab(new Group()));

        Scene scene = new Scene(tabs, 600, 600);
        primaryStage.setScene(scene);

        primaryStage.show();
    }

    private Tab createTab(Pane pane) {
        Circle c1 = new Circle(150, 150, 50, Color.RED);
        Circle c2 = new Circle(400, 400, 50, Color.YELLOW);

        pane.getChildren().addAll(c1, c2);

        Tab tab = new Tab(pane.getClass().getSimpleName());
        tab.setContent(pane);
        return tab ;
    }

    // annoyingly, Pane and Group do not have a common superclass with a getChildren()
    // method, so just reproduce the code...
    private Tab createTab(Group pane) {
        Circle c1 = new Circle(150, 150, 50, Color.RED);
        Circle c2 = new Circle(400, 400, 50, Color.YELLOW);

        pane.getChildren().addAll(c1, c2);

        Tab tab = new Tab(pane.getClass().getSimpleName());
        tab.setContent(pane);
        return tab ;
    }
    public static void main(String[] args) {
        launch(args);
    }
}


正如其他答案所建议的那样,在这里使用a将对您的帮助最大,因为它会自动将其子项放入垂直行中。下面是一个使用数组的简短片段(因此您可以创建任意多个圆)

导入javafx.application.application;
导入javafx.geometry.Pos;
导入javafx.stage.stage;
导入javafx.scene.scene;
导入javafx.scene.layout.VBox;
导入javafx.scene.shape.Circle;
导入javafx.scene.paint.*;
公共类尝试GraphicsStuff扩展应用程序{
@凌驾
public void start(Stage)引发异常{
圆圈[]圆圈=新圆圈[3];//创建3个圆圈
VBox VBox=新VBox();//VBox将在垂直行中放置圆
vBox.setAlignment(Pos.CENTER);//中心圆
对于(int i=0;i

和往常一样,请理解代码,不要盲目地复制和粘贴。干杯

不,两个
圆圈
都在屏幕上,但黄色圆圈被画在红色圆圈上。您可以使用
Color.YELLOW.deriveColor(0,1,1,0.5)
(alpha=0.5的黄色)而不是
Color.YELLOW
来轻松验证这一点,从而显示一个橙色圆圈。
import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.layout.VBox;
import javafx.scene.shape.Circle;
import javafx.scene.paint.*;

public class TryingGraphicsStuff extends Application{
    @Override
    public void start(Stage stage) throws Exception {

            Circle[] circle = new Circle[3];          // create 3 circles
            VBox vBox = new VBox();                   // vbox will put circles in vertical row
            vBox.setAlignment(Pos.CENTER);            // center circles

            for(int i = 0; i < circle.length; i++){
                circle[i] = new Circle(50);           // initialize circles with radius of 50
                vBox.getChildren().add(circle[i]);
            }

            circle[0].setFill(Color.RED);
            circle[1].setFill(Color.YELLOW);
            circle[2].setFill(Color.GREEN);

            // add vbox to scene

            Scene scene = new Scene(vBox, 300, 800);
            stage.setTitle("Circle");
            stage.setScene(scene);
            stage.show();
    }
    public static void main (String [] args){
        Application.launch(args);
    }

}