0

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!

  • 1
    Focus on ONE question at a time, otherwise it makes the question too broad to be answered correctly or sincintly – MadProgrammer Jun 09 '17 at 22:42
  • 1) For better help sooner, post a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). 2) @MadProgrammer *"..too broad to be answered correctly or sincintly "* I guess you mean 'succinctly' but fully agree. SO is a Q&A site, not a help desk or tutoring service. Each thread should focus on **one** specific question. This not only helps encourage answers from people that know how to solve one question but not the other, but also helps people searching for answers later. These things are also easier for the programmer to debug if broken down into smaller parts. – Andrew Thompson Jun 10 '17 at 02:07
  • 1
    @AndrewThompson I guess I mean, some one got me up way too early in the morning and I shouldn't trying to human today :P – MadProgrammer Jun 10 '17 at 02:08

2 Answers2

1

You can't just add a label to the BorderLayout.CENTER. This will replace the existing component in the CENTER.

If you want the label "on top" of the other components then you have a couple of options:

  1. Use a JLayeredPane. Your game will be played on one layer. Then you can add the JLabel on a different layer

  2. Display the label on the glass pane of the frame. This is the easier of the two approaches.

Read the Swing Tutorial for examples of both of these approaches. There are sections on:

  1. How to Use Root Panes

  2. How to Use LayeredPanes

camickr
  • 321,443
  • 19
  • 166
  • 288
0

Maybe create the label within your main as if it was apart of your game regardless of whether you lose or not. Just make it invisible. label1.setVisible(false);.

Then when you lose, make the label visible by setting the label's visibility to true. (When you lose)

With regards to the sound aspect. Go have a look at this thread here or even at the java post here

I believe that the code above shall aid you in your need for sound.

Hope this helps :D

With regards to setting the visibility of the label from a different class try:

if(loseGame){
    className.getLabelName().setVisible(true);
    myPanel.repaint();
} 
fluffy
  • 223
  • 1
  • 14
  • Either I don't know how to find the pathway to the sound file, or simply it doesn't work for me. And the JLabel problem I kind of understand, I am at the point where I can set the visibility to true in the second program, but I don't know how to send it back to the first program to display the Game Over JLabel. – Dylpyckle13 Jun 09 '17 at 17:55
  • Updated, give that a try. – fluffy Jun 09 '17 at 18:12