-2

I have a main class that creates the instance of the JFrame and JPanel and I set the JPanel as public so I can access it from another class to add more things into it (I don't know if this is how it works, that's just my logic).

public class Main {

   private JFrame obj; 
   public Gameplay gamePlay; 

   public Main() {
      obj = new JFrame();
      gamePlay = new Gameplay();
   }

   public void execute() {
      //properties of the jframe
      obj.setBounds(10, 10, 700, 600);
      obj.setTitle("Brick breaker");
      obj.setResizable(false);
      obj.setVisible(true);
      obj.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

      //add the jpanel inside the jframe
      obj.add(gamePlay);
   }

   public static void main(String[] args) {
      Main main = new Main();
      main.execute();
   }
}

Then, I have another class named Gameplay which extends JPanel. One of the methods inside has to draw things inside the JPanel and add a label when a condition is true.

Note that the commented region at the end where "GAME OVER" is drawn by with graphics g it does work correctly, but I want to put a label instead because I want to use custom font. The label never shows up. All the other things that the method pait() has to paint also work fine.

public void paint(Graphics g) {
    //background
    g.setColor(Color.black);
    g.fillRect(1, 1, 692, 592);
    
    //more code of drawing things
    //...
    
    if(ballposY > 570) {
        play = false;
        ballXdir = 0;
        ballYdir = 0;
        
        lblGameOver = new JLabel("GAME OVER");
        lblGameOver.setForeground(Color.green);
        lblGameOver.setFont(pixels);//custom font
        Main main = new Main();
        main.gamePlay.add(lblGameOver);

        //g.setColor(Color.green);
        //g.setFont(new Font("serif", Font.BOLD, 30));
        //g.drawString("GAME OVER", 250, 300);
    }
    g.dispose();
}
Sumit Singh
  • 487
  • 5
  • 19
winter
  • 207
  • 1
  • 4
  • 13
  • You are creating a new `Main` object in your `paint()` method and setting the label there. This is never shown though. That's probably not what you intended to do. Assuming that you actually wanted to do is show the color in the actual gameplay panel, it would just be `this.add(lblGameOver)` instead of the two lines where you create the new `Main` and add the label. – maloomeister Jul 10 '20 at 09:24
  • @maloomeister thank you for the feedback, but the label is still not showing. – winter Jul 10 '20 at 09:30
  • The info at https://stackoverflow.com/questions/21309627/how-to-add-jlabel-at-run-time-in-swing should help you go further. Regards – Horia Muntean Jul 10 '20 at 09:47
  • 1
    Don't add components in paint, because then you'll have to repaint which will add the component again. Also, you're overriding paint, and nowhere in your paint method are you painting the components. Override `paintComponent` not `paint`. – matt Jul 10 '20 at 09:57
  • @matt is right, hence why I removed my response. Adding components should not be done in `paintComponents`. – maloomeister Jul 10 '20 at 10:05
  • 3
    Post a proper [mre] demonstrating the proper after implementing all the suggestions if you still have a problem. – camickr Jul 10 '20 at 13:30
  • 2
    Another note: `obj.setVisible(true); .. obj.add(gamePlay);` should be in the opposite order. Only set a GUI visible once all components have been added & `pack()` is called. Components *can* be added after the GUI is visible, but special measures must be taken & that approach is usually the 'right answer to the wrong question'. – Andrew Thompson Jul 10 '20 at 14:17
  • There is too much code displayed, that is not strictly necessary. Reducing the code to the smallest possible size that still shows the error is a very important debugging skill. – NoDataDumpNoContribution Jul 16 '20 at 09:10
  • @Trilarion *"the smallest possible size that still shows the error"* Yes. We generally call that a [mre] & a pro-tip `[mre]` in a comment auto expands to that link & words. – Andrew Thompson Jul 17 '20 at 01:54
  • @AndrewThompson Thanks. I already knew that, having registered 8 years ago. I sometimes vary a bit and if I use the auto expansion, I like `[example]` more. – NoDataDumpNoContribution Jul 17 '20 at 06:03

1 Answers1

0

You are need to add this line to your code:

lblGameOver.setOpaque(true); It's will add your BackGroud and make that methode works well lblGameOver.setForeground(Color.green);

Programmer
  • 803
  • 1
  • 6
  • 13