0

I am working on a Tetris program. I am reusing a perfectly functioning Tron program to make it. In the Tron program, the GUI worked perfectly, but in the Tetris program I cannot get any GUI window to pop up not even the grid. I also need another window to pop up to tell the set controls. Thanks a lot if you try. Below, I'll supply you with both sets of programs

Tetris

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);
    }}

Tron

    public class Snake extends JFrame{

private static final long serialVersionUID = 1L;
private Board board;
public static int a = 10;
public static int b = 10;
public static int c;
public static int d;
public static int flag = 0;
public static int headX = 3;
public static int headY = 20;
public static int speed = 100;
public static String snakeDirection = "east";
public static int segmentCount;
public static int segmentCount2;
public static boolean gameOver = false;
public static boolean gameOver2 = false;
public static boolean start = false;
public static boolean start2 = false;
public static int[][] segments = new int[2][400];
public static int[][] segments2 = new int[2][400];
public static int headX2 = 37;
public static int headY2 = 20;
public static String snakeDirection2 = "west";


//Creates Snake
public static void main(String[] args) {
    Snake snake = new Snake();
    new Board(snake);
}




public Snake(){
    super("Snake");
    setLayout(new BorderLayout());
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    setResizable(false);

    this.board = new Board(this);

    add(board, BorderLayout.CENTER);

    addKeyListener(new KeyAdapter(){
        public void keyPressed(KeyEvent e){
            switch (e.getKeyCode()){

            // checks if keys are pressed

            case KeyEvent.VK_UP:
                if(snakeDirection != "south"){
                    snakeDirection = "north";
                    start = true;
                }
                break;
            case KeyEvent.VK_W:
                if(snakeDirection2 != "south"){
                    snakeDirection2 = "north";
                    start2 = true;
                }
                break;

            case KeyEvent.VK_DOWN:
                if(snakeDirection != "north") {
                    snakeDirection = "south";
                    start = true;
                }
                break;
            case KeyEvent.VK_S:
                if(snakeDirection2 != "north") {
                    snakeDirection2 = "south";
                    start2 = true;
                }
                break;

            case KeyEvent.VK_LEFT:
                if(snakeDirection != "east") {
                    snakeDirection = "west";
                    start = true;
                }
                break;
            case KeyEvent.VK_A:
                if(snakeDirection2 != "east") {
                    snakeDirection2 = "west";
                    start2 = true;
                }
                break;

            case KeyEvent.VK_RIGHT:
                if(snakeDirection != "west") {
                    snakeDirection = "east";
                    start = true;
                }
                break;
            case KeyEvent.VK_D:
                if(snakeDirection2 != "west") {
                    snakeDirection2 = "east";
                    start2 = true;
                }
                break;
            }
        }


    });



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

    runGame();


}


public void runGame(){

    while(gameOver == false) {

        segmentCount = Board.numberSegments;
            try {
                Thread.sleep(speed);
            } catch (Exception e) {
            }
            if(start) {
                //checks to see if the snake collided with itself
                if(Board.grid[headX][headY] == 1){
                    gameOver = true;
                    Board.death = true;
                    gameOver();
                }
                if(Board.grid[headX2][headY2] == 1){
                    gameOver = true;
                    Board.death = true;
                    gameOver();
                }

                // changes depending on which direction the snake is going
                if(snakeDirection.equals("north")) {
                    headY -= 1;
                    System.out.println("Player 111(Coord):" + headX + " " + headY);
                    if(checkCollision() == true){
                        while (segmentCount > 0){

                            segments[0][segmentCount] = segments[0][segmentCount - 1];
                            segments[1][segmentCount] = segments[1][segmentCount - 1];
                            segmentCount -= 1;
                        }

                        segments[0][0] = headX;
                        segments[1][0] = headY + 1;
                    }

                }

                if(snakeDirection2.equals("north")) {
                    System.out.println("Player 222(Coord):" + headX2 + " " + headY2);
                    headY2 -= 1;
                    if(checkCollision() == true){
                        while (segmentCount2 > 0){

                            segments2[0][segmentCount2] = segments2[0][segmentCount2 - 1];
                            segments2[1][segmentCount2] = segments2[1][segmentCount2 - 1];
                            segmentCount2 -= 1;
                        }

                        segments2[0][0] = headX2;
                        segments2[1][0] = headY2 + 1;
                    }

                }

                if(snakeDirection.equals("east")){
                    headX += 1;
                    System.out.println("Player 111(Coord):" + headX + " " + headY);
                    if(checkCollision() == true){
                        while(segmentCount > 0){
                            segments[0][segmentCount] = segments[0][segmentCount - 1];
                            segments[1][segmentCount] = segments[1][segmentCount - 1];
                            segmentCount -= 1;
                        }
                        segments[0][0] = headX - 1;
                        segments[1][0] = headY;
                    }
                }
                if(snakeDirection2.equals("east")){
                    headX2 += 1;
                    System.out.println("Player 222(Coord):" + headX2 + " " + headY2);
                    if(checkCollision() == true){
                        while(segmentCount2 > 0){
                            segments2[0][segmentCount2] = segments2[0][segmentCount2 - 1];
                            segments2[1][segmentCount2] = segments2[1][segmentCount2 - 1];
                            segmentCount2 -= 1;
                        }
                        segments2[0][0] = headX2 - 1;
                        segments2[1][0] = headY2;
                    }
                }

                if(snakeDirection.equals("south")){
                    headY += 1;
                    System.out.println("Player 111(Coord):" + headX + " " + headY);
                    if(checkCollision() == true){
                        while(segmentCount > 0){
                            segments[0][segmentCount] = segments[0][segmentCount - 1];
                            segments[1][segmentCount] = segments[1][segmentCount - 1];
                            segmentCount -= 1;
                        }
                        segments[0][0] = headX;
                        segments[1][0] = headY - 1;
                    }
                }

                if(snakeDirection2.equals("south")){
                    headY2 += 1;
                    System.out.println("Player 222(Coord):" + headX2 + " " + headY2);
                    if(checkCollision() == true){
                        while(segmentCount2 > 0){
                            segments2[0][segmentCount2] = segments2[0][segmentCount2 - 1];
                            segments2[1][segmentCount2] = segments2[1][segmentCount2 - 1];
                            segmentCount2 -= 1;
                        }
                        segments2[0][0] = headX2;
                        segments2[1][0] = headY2 - 1;
                    }
                }

                if(snakeDirection.equals("west")){
                    headX -= 1;
                    System.out.println("Player 111(Coord):" + headX + " " + headY);
                    if(checkCollision() == true){
                        while(segmentCount > 0){
                            segments[0][segmentCount] = segments[0][segmentCount - 1];
                            segments[1][segmentCount] = segments[1][segmentCount - 1];
                            segmentCount -= 1;
                        }
                        segments[0][0] = headX + 1;
                        segments[1][0] = headY;
                    }
                }
                if(snakeDirection2.equals("west")){
                    headX2 -= 1;
                    System.out.println("Player 222(Coord):" + headX2 + " " + headY2);
                    if(checkCollision() == true){
                        while(segmentCount2 > 0){
                            segments2[0][segmentCount2] = segments2[0][segmentCount2 - 1];
                            segments2[1][segmentCount2] = segments2[1][segmentCount2 - 1];
                            segmentCount2 -= 1;
                        }
                        segments2[0][0] = headX2 + 1;
                        segments2[1][0] = headY2;
                    }
                }
                board.repaint();
                //sees i the game is still going
                if(!gameOver) {
                    segmentCount = Board.numberSegments;
                    if(Board.grid[headX][headY] != 0) {
                        gameOver = true;
                        Board.death = true;
                        gameOver();
                    }
                    Board.grid[headX][headY] = 2;
                    //Board.grid[segments[0][segmentCount]][segments[1][segmentCount]] = 0;
                    //while(segmentCount > 0) {
                        //Board.grid[segments[0][segmentCount]][segments[1][segmentCount]] = 1;
                        //segmentCount -= 1;
                    //}
                    if(Board.grid[headX2][headY2] != 0) {
                        gameOver = true;
                        Board.death = true;
                        gameOver();
                    }
                    Board.grid[headX2][headY2] = 12;
                    Board.grid[segments2[0][segmentCount2]][segments2[1][segmentCount2]] = 10;
                    while(segmentCount2 > 0) {
                        Board.grid[segments2[0][segmentCount2]][segments2[1][segmentCount2]] = 11;
                        segmentCount2 -= 1;

                    }
                    board.repaint();

                }
            }
    }
}

//makes sure the game is over 
public void gameOver(){
    System.out.println("GGGGGAAAAAAAMMMMMMMEEEEEEE OOOOOOVVVVVEEEEERRRR");
    for(int i = 0; i < 20; i++){
        System.out.println(segments2[0][i] + " " + segments2[1][i]);
    }
}
//checks to see if the snake collided with the boarder
public boolean checkCollision(){
    if(headX == 41 || headY == 41 || headX == -1 || headY == -1){
        gameOver = true;
        Board.death = true;
        gameOver();
        return false;
    }
    return true;
}
public boolean checkCollision2(){
    if(headX2 == 41 || headY2 == 41 || headX2 == -1 || headY2 == -1){
        gameOver2 = true;
        Board.death = true;
        gameOver();
        return false;
    }
    return true;
}   

}

    public class Board extends JPanel{

private static final long serialVersionUID = 1l;
public static int x = 10;
public static int y = 10;
public static int a = 10;
public static int b = 10;
public static int n = 10;
public static int m = 10;
public static int s;
public static int sg;
public static int erase = 20;
public static int erase2 = 20;
public static boolean death = false;
public static boolean death2 = false;

public static int segmentAdd;
public static int score = 0;
public static boolean touchedFruit = false;
public static boolean isFruit = false;
public static String direction;
public static String direction2;
public static int numberSegments = 5;
public static int numberSegments2 = 5;
public static String bodyDirection;
public static String tailDirection;
public static String bodyDirection2;
public static String tailDirection2;

public static final int num_rows = 40;
public static final int num_cols = 40;
public static int size = 15;

public static int [][] grid = new int[40][40];

//makes board
public Board(Snake tempSnake){
    grid[10][10] = 0;
    setPreferredSize(new Dimension(num_cols * size, num_rows * size));
    setBackground(Color.WHITE);
}


public void paintComponent(Graphics g){
    super.paintComponent(g);

    deathTracker(g);
    for(int i = 0; i < 40; i++){
        for(int j = 0; j < 40; j++){
            if(grid[i][j] != 0){
                drawTile(i,j,grid[i][j],g);
            }
        }
    }
    //bodyTracker(g);
    //bodyTracker2(g);
    System.out.println("Player 111:" + (numberSegments + 2));
    System.out.println("Player 222:" + (numberSegments2 + 2));

    g.setColor(Color.WHITE);
    g.drawRect(0, 0, getWidth()-1, getHeight()-1);

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


private void drawTile(int x, int y, int type, Graphics g){
    x *= size;
    y *= size;

    switch(type){

    case 1:
        g.setColor(Color.GREEN);
        g.fillRect(x,  y,  size,  size);
        break;

    case 2:
        g.setColor(Color.RED);
        g.fillRect(x,  y,  size,  size);
        break;
    case 3:
        g.setColor(Color.GREEN);
        g.fillRect(x,  y,  size,  size);
        break;
    case 4:
        g.setColor(Color.RED);
        g.fillRect(x,  y,  size,  size);
        break;
    case 11:
        g.setColor(Color.PINK);
        g.fillRect(x,  y,  size,  size);
        break;
    case 12:
        g.setColor(Color.MAGENTA);
        g.fillRect(x,  y,  size,  size);
        break;
    case 10:
        g.setColor(Color.CYAN);
        g.fillRect(x,  y,  size,  size);
        break;

    }

    }

private void drawTile2(int x, int y, int type, Graphics g){
    x *= size;
    y *= size;

    switch(type){

    case 11:
        g.setColor(Color.RED);
        g.fillRect(x,  y,  size,  size);
        break;
    case 12:
        g.setColor(Color.RED);
        g.fillRect(x,  y,  size,  size);
        break;
    case 10:
        g.setColor(Color.RED);
        g.fillRect(x,  y,  size,  size);
        break;

    }
}


public void bodyTracker(Graphics g){
    if(!Snake.gameOver){

        s = numberSegments;
        numberSegments += 1;
        drawTile(Snake.headX, Snake.headY, grid[Snake.headX][Snake.headY], g);

        while(s >= 0){
            if(s > 0){
                drawTile(Snake.segments[0][s], Snake.segments[1][s], grid[Snake.segments[0][s]][Snake.segments[1][s]], g);
            }
            if(s == 0){
                drawTile(Snake.segments[0][s], Snake.segments[1][s], 4, g);
            }
            s -= 1;
        }

    }
}
public void bodyTracker2(Graphics g){
    if(!Snake.gameOver){


        sg = numberSegments2;

        numberSegments2 += 1;
        drawTile2(Snake.headX2, Snake.headY2, grid[Snake.headX2][Snake.headY2], g);


        while(sg >= 0){
            if(sg > 0){
                drawTile2(Snake.segments2[0][sg], Snake.segments2[1][sg], grid[Snake.segments2[0][sg]][Snake.segments2[1][sg]], g);
            }
            if(s == 0){
                drawTile2(Snake.segments2[0][sg], Snake.segments2[1][sg], 4, g);
            }
            sg -= 1;
        }

    }
}


public void deathTracker(Graphics g){
    if (death = true){
        erase = 20;
        while(erase > 0 && erase2 > 0){
            drawTile(erase, erase2, 0, g);
            erase -= 1;
            if(erase == 0){
            erase2 -= 1;
        }
    }
}

} }

GrimThor3
  • 171
  • 1
  • 10
  • 3
    **Relevant** code only. I can tell you right now though that this is a logic error: `tetrisName == "ZShape"`. [Compare strings using `.equals()`](http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java). – Jeroen Vannevel Feb 08 '14 at 00:43

3 Answers3

1

Your Tetris program has two JFrames, neither of which are created correctly. "Piece" should absolutely not be a JFrame. Go read the "swing tutorial" trail and try again once you know how to create a featureless GUI window.

1

You code is...frankly, scary.

The over use of static means you have absolutle no idea what is pointing to where and which instance is control.

When I (was finally able to) run your Tetris class, it failed with ...

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)

Which was here...

public Board(Tetris tempTetris) {
    grid[10][18] = 8;
    ^---- Index out of bounds...

When you take a look at the declration, you can see why...

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

grid only has ten elements in the first dimension. Remember, arrays are 0 indexed in Java.

  • Drop the over use of static, it will simply explode in you face and makes the program impossibly ridget, not to mention difficult to know what's actually point to what...
  • Rely on models (vitrual views) to maintain the game state and allow the UI components to render those models.
  • Use observer pattern and maybe even producer/consumer pattern to notify distinct elements that the models have changed
  • Have a clear boundary of responsibility for each section of the game. For example, the game loop should not be modifying the model directly, but should be providing it with information it can use to make decisions about how best to update itself.
  • Use Key bindings over KeyListener
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • hey Mad, I need to draw the score and the controls. My friend showed my a drawString(String) method. But I type it in and it comes up with g.drawString(iterator, x, y). Could you help me? – GrimThor3 Feb 08 '14 at 22:05
  • [Graphics#drawString](http://docs.oracle.com/javase/7/docs/api/java/awt/Graphics.html#drawString(java.lang.String,%20int,%20int)), [Lesson: Getting Started with Graphics](http://docs.oracle.com/javase/tutorial/2d/basic2d/) and the rest of that tutorial wouldn't hurt either, [Performing Custom Painting](http://docs.oracle.com/javase/tutorial/uiswing/painting/) probably wouldn't hurt either – MadProgrammer Feb 08 '14 at 22:17
0

I think you will get a lot of value by looking at a working Tetris (although it's in Java); http://bordiani.wordpress.com/2014/10/20/tetris-in-java-part-i-overview/;

For example:
GUI - "I decided to not mess around too much with the graphical part, so I used the “LED” model. What I mean is that I have a 2-D array of JPanels (20/10) and by coloring them – I display the figures."

MODEL - "the most basic piece in the equation is the Block class which has the int values x and y"
- "The Figure class has an ArrayList of 4 Block objects. The reason for not choosing a standard array is because the blocks will be eliminated as the game progresses."
- "Board is the class which actually contains all the figures that appear on the screen. It has a separate Figure field which is called FreshFigure and represents the figure that "falls down" and is controlled by the user."

Johny
  • 315
  • 4
  • 9
  • You should include the relevant parts of your link in your answer so that your answer is still useful if the linked content changes or becomes unreachable. – skrrgwasme Oct 20 '14 at 21:39