从javafx start方法中分离代码

时间:2018-03-13 19:29:07

标签: java user-interface javafx

我想将两个FOR循环分成 generateBricks() addBricks()可能在一个单独的类中,但我不知道如何进行这种分离因为循环使用矩形类的形状,并尝试添加砖块的意外..任何想法家伙?

public class Main extends Application {

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

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

    //Just setting up the graphics for the game including the bar and ball
    Pane root = new Pane();
    Scene scene = new Scene(root , 400 , 500);

    Rectangle[][] bricks = new Rectangle[10][3];

    //Generating individual rectangle objects to add to brick array
    for(int i = 0; i < bricks.length; i++)
    {
        for(int j = 0; j < bricks[0].length; j++)
        {
            bricks[i][j] = new Rectangle(10 + (40 * i), 70 + (20 * j), 30, 10);
        }
    }   

    //Iterating through the array of bricks and adding them to the graphics
    for(int i = 0; i < bricks.length; i++)
    {
        for(int j = 0; j < bricks[0].length; j++)
        {
            root.getChildren().add(bricks[i][j]);
        }   
    }   

2 个答案:

答案 0 :(得分:0)

public class Main extends Application {

    Pane root;

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

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

        //Just setting up the graphics for the game including the bar and ball
        root = new Pane();
        Scene scene = new Scene(root, 400, 500);

        Rectangle[][] bricks = new Rectangle[10][3];

        bricks = generateBricks(bricks);
        addBricks(bricks);
    }

    public Rectangle[][] generateBricks(Rectangle[][] bricks) {
        //Generating individual rectangle objects to add to brick array
        for (int i = 0; i < bricks.length; i++) {
            for (int j = 0; j < bricks[0].length; j++) {
                bricks[i][j] = new Rectangle(10 + (40 * i), 70 + (20 * j), 30, 10);
            }
        }
        return bricks;
    }

    public void generateBricks(Rectangle[][] bricks) {
        //Iterating through the array of bricks and adding them to the graphics
        for (int i = 0; i < bricks.length; i++) {
            for (int j = 0; j < bricks[0].length; j++) {
                root.getChildren().add(bricks[i][j]);
            }
        }
    }
}
  

注意:
  全球Pane root;   将for放入不同的方法

答案 1 :(得分:0)

如果您只想分离方法,请考虑每种方法应该做什么,需要做什么信息,以及应该向谁调用它提供哪些信息。

您的generateBricks()方法应该以其名称负责创建砖块,并且应该将这些砖块(作为数组)返回给调用者。您可能希望该方法被告知要创建多少块(而不是硬编码或依赖全局变量)。所以方法的签名应该是

private Rectangle[][] generateBricks(int numCols, int numRows) ;

并且实现看起来像

private Rectangle[][] generateBricks(int numCols, int numRows) {
    Rectangle[][] bricks = new Rectangle[numCols][numRows];
    for (int i = 0 ; i < numCols ; i++) {
        for (int j = 0 ; j < numRows ; j++) {
            bricks[i][j] = new Rectangle(10 + (40 * i), 70 + (20 * j), 30, 10);
        }
    }
    return bricks ;
}

您的addBricks()方法需要将砖块添加到某种容器中;所以它需要的信息是砖块和容器。它只是去做它的工作,并且不需要向任何人调用它提供任何信息。所以它的签名将是

private void addBricks(Rectangle[][] bricks, Pane container) ;

它看起来像:

private void addBricks(Rectangle[][] bricks, Pane container) {
    for (int i = 0 ; i < bricks.length; i++) {
        for (int j = 0 ; i < bricks[i].length ; j++) {
            container.getChildren().add(bricks[i][j]);
        }
    }
}

使用for-each类型循环可以使它更优雅:

private void addBricks(Rectangle[][] bricks, Pane container) {
    for (Rectangle[] column : bricks) {
        for (Rectangle brick : column) {
            container.getChildren().add(brick);
        }
    }
}

甚至利用addAll(...)方法(采用一维数组):

private void addBricks(Rectangle[][] bricks, Pane container) {
    for (Rectangle[] column : bricks) {
        container.getChildren().addAll(column);
    }
}

所以这一切看起来像

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.Pane;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;

public class Main extends Application {

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

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

        //Just setting up the graphics for the game including the bar and ball
        Pane root = new Pane();
        Scene scene = new Scene(root , 400 , 500);

        Rectangle[][] bricks = generateBricks(10, 3);
        addBricks(bricks, root);

        primaryStage.setScene(scene);
        primaryStage.show();
    }

    private Rectangle[][] generateBricks(int numCols, int numRows) {
        Rectangle[][] bricks = new Rectangle[numCols][numRows];
        for (int i = 0 ; i < numCols ; i++) {
            for (int j = 0 ; j < numRows ; j++) {
                bricks[i][j] = new Rectangle(10 + (40 * i), 70 + (20 * j), 30, 10);
            }
        }
        return bricks ;
    }

    private void addBricks(Rectangle[][] bricks, Pane container) {
        for (Rectangle[] column : bricks) {
            container.getChildren().addAll(column);
        }
    }

}

如果你想更进一步,把事情分成不同的类,我认为首先要考虑的是"single responsibility principle"。这基本上说每个类应该负责应用程序功能的单个方面。您已经违反了这个原则,因为Application子类负责应用程序的生命周期(这就是为什么它有一个名为start()的方法,它在应用程序启动时调用,以及其他方法您可能不需要,例如init()stop())。现在,您的Main类正在处理应用程序的生命周期,并负责设计显示(或#34;视图&#34;)。所以你可以将第二个责任提取到自己的类中:

import javafx.scene.layout.Pane;
import javafx.scene.shape.Rectangle;

public class BrickView extends Pane {

    public BrickView() {
        Rectangle[][] bricks = generateBricks(10, 3);
        addBricks(bricks);
    }


    private Rectangle[][] generateBricks(int numCols, int numRows) {
        Rectangle[][] bricks = new Rectangle[numCols][numRows];
        for (int i = 0 ; i < numCols ; i++) {
            for (int j = 0 ; j < numRows ; j++) {
                bricks[i][j] = new Rectangle(10 + (40 * i), 70 + (20 * j), 30, 10);
            }
        }
        return bricks ;
    }

    private void addBricks(Rectangle[][] bricks) {
        for (Rectangle[] column : bricks) {
            this.getChildren().addAll(column);
        }
    }
}

现在你的Main课程可以专注于它应该做的事情:启动应用程序:

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.stage.Stage;

public class Main extends Application {

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

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

        //Just setting up the graphics for the game including the bar and ball
        BrickView brickView = new BrickView();
        Scene scene = new Scene(brickView , 400 , 500);

        primaryStage.setScene(scene);
        primaryStage.show();
    }

}

您甚至可以更进一步,创建一个BrickFactory类,负责创建砖块,并将该功能与决定如何显示它们的视图分开。这可能有点过头了(虽然它确实有一些优势)。