For a computer science project I am tasked with taking a completed Java game and add some parts to it. I decided to do my work on the classic game Tetris, however I am having some issues. One issue is that I am unable to print a JLabel on top of the game board when the game hits a gameOver. I am doing this using three different programs all combined by the package, and only need two of the three programs (I believe) to achieve this. Also, I have searched everywhere for how to place sound into Java and I have not been able to find anything. I want to add the Tetris theme to the game, however I also don't know where to add it.
The first code extracted here is the Tetris.java program, which creates the JFrame and all of the JLabels:
JLabel gameTitle;
static JLabel gameOver;
static boolean gameIsOver = false;
JLabel gameStatus;
public static void main(String argsp[]) throws Exception
{
Tetris game = new Tetris();
game.setVisible(true);
game.setResizable(false);
game.setSize(400, 900);
game.setTitle("Tetris - myName");
game.setDefaultCloseOperation(EXIT_ON_CLOSE);
game.setLocationRelativeTo(null);
game.getContentPane().setBackground(Color.DARK_GRAY);
while(gameIsOver)
game.add(gameOver, BorderLayout.CENTER);
}
public Tetris() throws Exception
{
URL fontURL = new URL("http://www.WebpagePublicity.com/" + "free-fonts/f/Futura%20Black%20BT.ttf");
Font f = Font.createFont(Font.TRUETYPE_FONT, fontURL.openStream());
GraphicsEnvironment g = GraphicsEnvironment.getLocalGraphicsEnvironment();
g.registerFont(f);
gameTitle = new JLabel("TETRIS", SwingConstants.CENTER);
gameTitle.setForeground(Color.WHITE);
f = f.deriveFont(Font.PLAIN, 80);
gameTitle.setFont(f);
add(gameTitle, BorderLayout.NORTH);
gameOver = new JLabel("GAME OVER", SwingConstants.CENTER);
gameOver.setForeground(Color.RED);
gameOver.setOpaque(true);
f = f.deriveFont(Font.PLAIN, 50);
gameOver.setFont(f);
gameStatus = new JLabel("LINES: 0 - SCORE: 0", SwingConstants.CENTER);
gameStatus.setForeground(Color.WHITE);
f = f.deriveFont(Font.PLAIN, 30);
gameStatus.setFont(f);
add(gameStatus, BorderLayout.SOUTH);
Board b = new Board(this);
add(b);
b.start();
}
The second program is the Board.java program, and I will extract the method that I believe must be used to set the JLabel to appear:
private void nextTetromino()
{
cP.setRandomTetromino();
cX = bW / 2 + 1;
cY = bH - 1 + cP.minY();
if(!moveTetromino(cP, cX, cY))
{
cP.setTetromino(Tetrominoes.NoShape);
t.stop();
playing = false;
/////////////////////////////////////////////////////
gameOver.add(gameOver, BorderLayout.CENTER);
//Tetris.gameOver.add(gameOver, BorderLayout.CENTER);
/////////////////////////////////////////////////////
}
}
You can see my two attempts to add the gameOver JLabel to the JFrame after the game ends. I have also tried to send a boolean back from the nextTetromino() method back to the Tetris programs' main() as well as the Tetris(), however I have had no luck.
The second part of this question is where I can add the Tetris theme so that when the game is being played, the theme will play as well. I have the MP3 file downloaded and its in the Tetris Source Folder that is home to all of these programs.
Thanks everyone!