有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java实现类

这些天我在努力学习java。现在我刚刚学会了如何使用多个类,我正在构建“砖块破碎机”游戏,但我不明白为什么这不起作用

 public void run() {
    BricksandBox world = new BricksandBox();
    world.createWorld();
}

下面是“BricksandBox”中的代码部分:

public void createWorld(){
        buildCanvas();
        int i = 0;
        while(i < NBRICK_ROWS){
        i++;
        createARow();
        setCollumNumber();

        }
    }
    /*builds the gamecenter */
    private void buildCanvas(){
        GRect can = new GRect(APPLICATION_WIDTH, APPLICATION_HEIGHT);
        add(can);

    }

    private void createARow(){


        for (int i = 1; i <= NBRICKS_PER_ROW; i++){
            //int y = the brickYoffset + BrickHeight+BrickSep this moves
            // the brick one down. 
            int y = BRICK_Y_OFFSET+((BRICK_SEP+BRICK_HEIGHT)*collumNumber);
            /* 
             * i-1 because it can't be set to zero. else there will be 11 bricks.
             * so you do i-1.
             */
            int x = BRICK_SEP/2+(BRICK_SEP*(i-1))+(BRICK_WIDTH*(i-1));
            GRect brick = new GRect(x,y,BRICK_WIDTH,BRICK_HEIGHT);
            brick.setFilled(true);

            //setting the color
            if(collumNumber <= 1){
                brick.setColor(Color.RED);
            }
            else if(collumNumber <= 3){
                brick.setColor(Color.ORANGE);
            }
            else if(collumNumber <= 5){
                brick.setColor(Color.YELLOW);
            }
            else if(collumNumber <= 7){
                brick.setColor(Color.GREEN);
            }
            else if(collumNumber <= 9){
                brick.setColor(Color.CYAN);
            }
            add(brick);
            }

    }   

    private void setCollumNumber(){
        //MeeBezig. create a value that counts up from 1 to 10.
        //1 is the first row number. 2 is the second row number etc...
        int x = BRICK_SEP/2+(BRICK_SEP*collumNumber)+(BRICK_WIDTH*collumNumber/2);
        int y = BRICK_Y_OFFSET+((BRICK_HEIGHT)*collumNumber);


        if(contains(x,y)){

            collumNumber++;

            }

    }


    /** 
     * a number that counts from one to ten
     */
    private int collumNumber;'

首先,该部分只是在主文件中。但对于分解,我试着把它放在不同的类中。但现在当我运行程序时,它只显示一个空白画布

提前谢谢


共 (0) 个答案