Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/user-interface/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 俄罗斯方块程序的GUI出现故障_Java_User Interface_Tetris - Fatal编程技术网

Java 俄罗斯方块程序的GUI出现故障

Java 俄罗斯方块程序的GUI出现故障,java,user-interface,tetris,Java,User Interface,Tetris,我正在做一个俄罗斯方块项目。我正在重复使用一个功能完善的Tron程序来实现它。在Tron程序中,GUI工作得很好,但在俄罗斯方块程序中,我无法弹出任何GUI窗口,即使是网格。我还需要另一个窗口弹出,告诉设置控件。如果你尝试的话,非常感谢。下面,我将为您提供两套程序 俄罗斯方块 public class Board extends JPanel { private static final long serialVersionUID = 1l; public static boolean dea

我正在做一个俄罗斯方块项目。我正在重复使用一个功能完善的Tron程序来实现它。在Tron程序中,GUI工作得很好,但在俄罗斯方块程序中,我无法弹出任何GUI窗口,即使是网格。我还需要另一个窗口弹出,告诉设置控件。如果你尝试的话,非常感谢。下面,我将为您提供两套程序

俄罗斯方块

public class Board extends JPanel {

private static final long serialVersionUID = 1l;
public static boolean death = false;
public static int score = 0;
public static String direction;
public static int numberTetris = 1;

public static final int numRows = 10;
public static final int numCols = 18;
public static final int size = 20;

public static int[][] grid = new int[10][218];

public Board(Tetris tempTetris){
    grid[10][18] = 8;
    setPreferredSize(new Dimension(numCols * size, numRows * size));
    setBackground(Color.DARK_GRAY);
}

//Erases the grid to be filled with zeros
public static void restart(int[][] gr){
    for (int i = 0; i <= numCols; i++){
        for (int j = 1; j <= numRows; j++){
            grid[i][j] = 0;
        }
    }
    Piece.nextTetris();
}

//Swts the graphics component
public void paintComponent(Graphics g){
    super.paintComponent(g);
    g.drawString("score: " + score, 10, 10);
    g.setColor(Color.DARK_GRAY);
    g.drawRect(0, 0 , getWidth() - 1, getHeight() - 1);

    for (int x = 0; x < numCols; x++){
        for (int y = 0; y < numRows; y++){
            g.drawLine(x * size, 0, x * size, getHeight());
            g.drawLine(0, y * size, getWidth(), y * size);
        }
    }
}

//Fills in the spaces
private void drawTile(int x, int y, int type, Graphics g){
    x *= size;
    y *= size;

    switch (type){

    case 1:
        if (Piece.type == 1){
            //ZShape
            g.setColor(Color.ORANGE);
            if (Piece.pieceDirection == 270 || Piece.pieceDirection == 90){
                g.fillRect(Piece.centerX - 1, Piece.centerY, size, size);
                g.fillRect(Piece.centerX, Piece.centerY, size, size);
                g.fillRect(Piece.centerX, Piece.centerY - 1, size, size);
                g.fillRect(Piece.centerX + 1, Piece.centerY - 1, size, size);
            }
            if (Piece.pieceDirection == 180 || Piece.pieceDirection == 0){
                g.fillRect(Piece.centerX + 1, Piece.centerY + 1, size, size);
                g.fillRect(Piece.centerX + 1, Piece.centerY, size, size);
                g.fillRect(Piece.centerX, Piece.centerY, size, size);
                g.fillRect(Piece.centerX, Piece.centerY - 1, size, size);
            }
        }else if (Piece.type == 2){
            //"SShape;
            g.setColor(Color.BLUE);
            if (Piece.pieceDirection == 270 || Piece.pieceDirection == 90){
                g.fillRect(Piece.centerX + 1, Piece.centerY, size, size);
                g.fillRect(Piece.centerX, Piece.centerY, size, size);
                g.fillRect(Piece.centerX, Piece.centerY - 1, size, size);
                g.fillRect(Piece.centerX - 1, Piece.centerY - 1, size, size);
            }
            if (Piece.pieceDirection == 180 || Piece.pieceDirection == 0){
                g.fillRect(Piece.centerX, Piece.centerY + 1, size, size);
                g.fillRect(Piece.centerX, Piece.centerY, size, size);
                g.fillRect(Piece.centerX + 1, Piece.centerY, size, size);
                g.fillRect(Piece.centerX + 1, Piece.centerY - 1, size, size);
            }
        }else if (Piece.type == 3){
            //LineShape;
            g.setColor(Color.RED);
            if (Piece.pieceDirection == 270 || Piece.pieceDirection == 90){
                g.fillRect(Piece.centerX - 2, Piece.centerY, size, size);
                g.fillRect(Piece.centerX - 1, Piece.centerY, size, size);
                g.fillRect(Piece.centerX, Piece.centerY, size, size);
                g.fillRect(Piece.centerX + 1, Piece.centerY, size, size);
            }
            if (Piece.pieceDirection == 180 || Piece.pieceDirection == 0){
                g.fillRect(Piece.centerX, Piece.centerY + 1, size, size);
                g.fillRect(Piece.centerX, Piece.centerY, size, size);
                g.fillRect(Piece.centerX, Piece.centerY - 1, size, size);
                g.fillRect(Piece.centerX, Piece.centerY - 2, size, size);
            }
        }else if (Piece.type == 4){
            //TShape;
            g.setColor(Color.GREEN);
            if (Piece.pieceDirection == 270){
                g.fillRect(Piece.centerX - 1, Piece.centerY, size, size);
                g.fillRect(Piece.centerX, Piece.centerY, size, size);
                g.fillRect(Piece.centerX + 1, Piece.centerY, size, size);
                g.fillRect(Piece.centerX, Piece.centerY - 1, size, size);
            }
            if (Piece.pieceDirection == 90){
                g.fillRect(Piece.centerX - 1, Piece.centerY, size, size);
                g.fillRect(Piece.centerX, Piece.centerY, size, size);
                g.fillRect(Piece.centerX + 1, Piece.centerY, size, size);
                g.fillRect(Piece.centerX, Piece.centerY + 1, size, size);
            }
            if (Piece.pieceDirection == 180){
                g.fillRect(Piece.centerX, Piece.centerY + 1, size, size);
                g.fillRect(Piece.centerX, Piece.centerY, size, size);
                g.fillRect(Piece.centerX, Piece.centerY - 1, size, size);
                g.fillRect(Piece.centerX - 1, Piece.centerY, size, size);
            }
            if (Piece.pieceDirection == 0){
                g.fillRect(Piece.centerX, Piece.centerY + 1, size, size);
                g.fillRect(Piece.centerX, Piece.centerY, size, size);
                g.fillRect(Piece.centerX, Piece.centerY - 1, size, size);
                g.fillRect(Piece.centerX + 1, Piece.centerY, size, size);
            }
        }else if (Piece.type == 5){
            //SqaureShape;
            g.setColor(Color.YELLOW);
            if (Piece.pieceDirection == 270 || Piece.pieceDirection == 180 || Piece.pieceDirection == 90 || Piece.pieceDirection == 0){
                g.fillRect(Piece.centerX - 1, Piece.centerY, size, size);
                g.fillRect(Piece.centerX, Piece.centerY, size, size);
                g.fillRect(Piece.centerX - 1, Piece.centerY - 1, size, size);
                g.fillRect(Piece.centerX, Piece.centerY - 1, size, size);
            }
        }else if (Piece.type == 6){
            //LShape;
            g.setColor(Color.DARK_GRAY);
            if (Piece.pieceDirection == 270){
                g.fillRect(Piece.centerX - 1, Piece.centerY - 1, size, size);
                g.fillRect(Piece.centerX - 1, Piece.centerY, size, size);
                g.fillRect(Piece.centerX, Piece.centerY, size, size);
                g.fillRect(Piece.centerX + 1, Piece.centerY, size, size);
            }
            if (Piece.pieceDirection == 90){
                g.fillRect(Piece.centerX - 1, Piece.centerY, size, size);
                g.fillRect(Piece.centerX, Piece.centerY, size, size);
                g.fillRect(Piece.centerX + 1, Piece.centerY, size, size);
                g.fillRect(Piece.centerX + 1, Piece.centerY + 1, size, size);
            }
            if (Piece.pieceDirection == 180){
                g.fillRect(Piece.centerX - 1, Piece.centerY + 1, size, size);
                g.fillRect(Piece.centerX, Piece.centerY + 1, size, size);
                g.fillRect(Piece.centerX, Piece.centerY, size, size);
                g.fillRect(Piece.centerX, Piece.centerY - 1, size, size);
            }
            if (Piece.pieceDirection == 0){
                g.fillRect(Piece.centerX, Piece.centerY + 1, size, size);
                g.fillRect(Piece.centerX, Piece.centerY, size, size);
                g.fillRect(Piece.centerX, Piece.centerY - 1, size, size);
                g.fillRect(Piece.centerX + 1, Piece.centerY - 1, size, size);
            }
        }else if (Piece.type == 7){
            //MirroredLShape;
            g.setColor(Color.MAGENTA);
            if (Piece.pieceDirection == 270){
                g.fillRect(Piece.centerX - 1, Piece.centerY, size, size);
                g.fillRect(Piece.centerX, Piece.centerY, size, size);
                g.fillRect(Piece.centerX + 1, Piece.centerY, size, size);
                g.fillRect(Piece.centerX + 1, Piece.centerY - 1, size, size);
            }
            if (Piece.pieceDirection == 90){
                g.fillRect(Piece.centerX - 1, Piece.centerY  + 1, size, size);
                g.fillRect(Piece.centerX - 1, Piece.centerY, size, size);
                g.fillRect(Piece.centerX, Piece.centerY, size, size);
                g.fillRect(Piece.centerX + 1, Piece.centerY, size, size);
            }
            if (Piece.pieceDirection == 180){
                g.fillRect(Piece.centerX, Piece.centerY + 1, size, size);
                g.fillRect(Piece.centerX, Piece.centerY, size, size);
                g.fillRect(Piece.centerX, Piece.centerY - 1, size, size);
                g.fillRect(Piece.centerX - 1, Piece.centerY - 1, size, size);
            }
            if (Piece.pieceDirection == 0){
                g.fillRect(Piece.centerX + 1, Piece.centerY + 1, size, size);
                g.fillRect(Piece.centerX, Piece.centerY + 1, size, size);
                g.fillRect(Piece.centerX, Piece.centerY, size, size);
                g.fillRect(Piece.centerX, Piece.centerY - 1, size, size);
            }
        }

        break;

    //Used pieces   
    case 2:
        g.setColor(Color.BLACK);
        g.fillRect(x, y, size, size);

        break;
    //The grid
    case 3:
        g.setColor(Color.WHITE);

        break;
    }

    //Pretty much refreshes the board
    public static void repaint(){
        for (int i = 0; i <= numCols; i++){
            for (int j = -1; j >= numRows; j--){
                if (grid[i][j] == 2){
                    drawTile(i, j, 2, g);
                }else if (grid[i][j] == 0){
                    drawTile(i, j, 3, g);
                }
            }
        }
    }
}


    public class Piece extends JFrame {

public static final long serialVersionUID = 1L;
public Board board;
public static final int x = 1;
public static final int y = 1;
public static int centerX = 10;
public static int centerY = 10;
public static final int north = 90;
public static final int east = 0;
public static final int south = 270;
public static final int west = 180;
public static int pieceDirection = south;
public static int pieceCount;
public static Piece[][] piece1 = new Piece[x][y];
public static Piece[][] piece2 = new Piece[x][y];
public static Piece[][] piece3 = new Piece[x][y];
public static Piece[][] piece4 = new Piece[x][y];
public static int a;
public static String pieceName;
public static int[][] usedPieceCoords = new int[Board.numRows][Board.numCols];
public static int type;
/*
 * type = 1 == ZShape
   type = 2 == SShape
   type = 3 == LineShape
   type = 4 == TShape
   type = 5 == SquareShape
   type = 6 == LShape
   type = 7 == MirroredLShape
 */
public static final int firstCenterX = 4;
public static final int firstCenterY = -1;

//Constructs a piece obeject at a certain pair of coordinates 
public Piece(int X, int Y, int num){
    centerX = X;
    centerY = Y;
    type = num;
}

//Checks if the piece has gone out of set bounds, I still haven't done anything to it, but advice would be helpful
public static boolean checkCollision(){
    if (centerX == 20 || centerY == 20 || centerX == -1 || centerY == -1){
        Tetris.gameOver = true;
        Board.death = true;
        Tetris.gameOver();

    }
    return false;
}

//Makes a new tetris and puts it in the game
public static void nextTetris(){
    a = (int)(Math.random());
    Piece centerPiece = new Piece(firstCenterX, firstCenterY, a);
}

//I intend to increase speed until it hits the piece under it
public static void drop() {

}

//Moves the piece one space to the right
public static void moveRight() {
    Board.grid[centerX][centerY] = 0;

    centerX =+ 1;
}

//Moves the piece one space to the left
public static void moveLeft() {
    Board.grid[centerX][centerY] = 0;
    Board.grid[centerX - 1][centerY] = 1;
}

//Rotates the piece 90 degrees clockwise
public static void turnPiece(){
    pieceDirection -= 90;
    if (Tetris.tetrisName == ""){

    }
}
}


    public class Tetris extends JFrame {

private static final long serialVersionUID = 1L;
private Board board;
public static int speed = 250;
public static int segmentCount = 4;
public static boolean gameOver = false;
public static boolean start = false;
public static String tetrisName;

//Makes a tetris object, still to define it more as creating a certain object based on Piece.type
public Tetris(){

    super("Tetris");
    setLayout(new BorderLayout());
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    setResizable(false);
    this.board = new Board(this);
    add(board, BorderLayout.CENTER);

    //the key listeners
    addKeyListener(new KeyAdapter() {
        public void keyPressed(KeyEvent e) {
            switch(e.getKeyCode()){

            case KeyEvent.VK_W:
            case KeyEvent.VK_UP:
                Piece.turnPiece();
                break;

            case KeyEvent.VK_S:
            case KeyEvent.VK_DOWN:
                speed =+ 10;
                break;

            case KeyEvent.VK_A:
            case KeyEvent.VK_LEFT:
                Piece.moveLeft();
                break;

            case KeyEvent.VK_D:
            case KeyEvent.VK_RIGHT:
                Piece.moveRight();
                break;

            case KeyEvent.VK_SPACE:
                Piece.drop();
            }
        }

    });

    pack();
    setLocationRelativeTo(null);
    setVisible(true);

    runGame();
}

    //The delay portion and the starting of the game
        public void runGame(){
            while (gameOver == false){
                try{
                    Thread.sleep(150);
                    }
                catch(Exception e){}
                if (start){
                    //self-collision check
                    if (Board.grid[Piece.centerX][Piece.centerY] >= -1){
                        gameOver = true;
                        Board.death = true;
                        gameOver();
                    }

                board.repaint();

            }
        }
    }

    //This just outprints "GAME OVER" to the consule when it is called
    public static void gameOver(){
        System.out.println("GAME OVER");
    }

    //This outputs the number that the certain name corresponds to
    public int getType(String tetrisName){
        if (tetrisName == "ZShape"){
             Piece.type = 1;
        }else if (tetrisName == "SShape"){
            Piece.type = 2;
        }else if (tetrisName == "LineShape"){
            Piece.type = 3;
        }else if (tetrisName == "TShape"){
            Piece.type = 4;
        }else if (tetrisName == "SquareShape"){
            Piece.type = 5;
        }else if (tetrisName == "LShape"){
            Piece.type = 6;
        }else if (tetrisName == "MirroredLShape"){
            Piece.type = 7;
        }
        return Piece.type;
    }

    //Making a new piece, tetris, and board
    public static void main(String[] args){
        int a = (int)(Math.random() * 7);
        Piece centerPiece = new Piece(Piece.firstCenterX, Piece.firstCenterY, a);
        Tetris tetris = new Tetris();
        new Board(tetris);
    }}
公共类板扩展JPanel{
私有静态最终长serialVersionUID=1l;
公共静态布尔死亡=false;
公共静态积分=0;
公共静态字符串方向;
公共静态整数rteris=1;
公共静态最终整数=10;
公共静态最终整数=18;
公共静态最终整数大小=20;
公共静态int[]grid=newint[10][218];
公共委员会(俄罗斯方块){
网格[10][18]=8;
setPreferredSize(新维度(numCols*size,numRows*size));
挫折背景(颜色:深灰色);
}
//删除要用零填充的网格
公共静态无效重新启动(int[]gr){

对于(int i=0;i您的俄罗斯方块程序有两个JFrame,两个JFrame都没有正确创建。“Piece”绝对不应该是JFrame。阅读“swing tutorial”教程,一旦您知道如何创建一个无特色的GUI窗口,请再试一次。

您的代码……坦白说,很可怕

过度使用
static
意味着你完全不知道什么指向哪里,哪个实例是控件

当我(终于能够)运行你的
俄罗斯方块
课程时,它失败了,因为

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 10
    at tetris.Tetris$Board.<init>(Tetris.java:146)
    at tetris.Tetris.<init>(Tetris.java:35)
    at tetris.Tetris.main(Tetris.java:127)
当你看一看下降,你就会明白为什么

public static int[][] grid = new int[10][218];
grid
在第一维度中只有十个元素。请记住,数组在Java中是
0
索引的

  • 不要过度使用
    static
    ,它只会在你面前爆炸,使程序无法正常运行,更不用说很难知道实际指向的是什么
  • 依靠模型(虚拟视图)来维护游戏状态,并允许UI组件呈现这些模型
  • 使用观察者模式,甚至生产者/消费者模式来通知不同的元素模型已经更改
  • 对游戏的每个部分都有一个明确的责任界限。例如,游戏循环不应该直接修改模型,而应该向它提供信息,它可以用来决定如何最好地更新自己
  • 在键侦听器上使用

我认为,通过查看一个运行中的俄罗斯方块(尽管它是用Java编写的),您将获得很多价值

例如:
图形用户界面-“我决定不把图形部分搞得太乱,所以我使用了“LED”模型。我的意思是,我有一个2-D的JPanel数组(20/10),通过着色,我可以显示图形。”

模型-“方程中最基本的部分是具有int值x和y的块类”
-“Figure类有一个包含4个Block对象的ArrayList。不选择标准数组的原因是,随着游戏的进行,这些Block将被消除。”

-“Board是一个类,它实际上包含屏幕上显示的所有图形。它有一个单独的图形字段,名为FreshFigure,表示“下降”的图形,由用户控制。”

仅相关代码。我现在可以告诉您,尽管这是一个逻辑错误:
tetrisName==“ZShape”
。嘿,Mad,我需要绘制分数和控件。我的朋友向我展示了一个drawString(String)方法。但我输入了它,它生成了g.drawString(迭代器,x,y)。你能帮我吗?,而该教程的其余部分也不会有任何伤害,可能不会有任何伤害。你应该在答案中包含链接的相关部分,以便在链接内容更改或无法访问时,你的答案仍然有用。
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 10
    at tetris.Tetris$Board.<init>(Tetris.java:146)
    at tetris.Tetris.<init>(Tetris.java:35)
    at tetris.Tetris.main(Tetris.java:127)
public Board(Tetris tempTetris) {
    grid[10][18] = 8;
    ^---- Index out of bounds...
public static int[][] grid = new int[10][218];