0

Hi I'm following an online tutorial to make a rhythm game in Java and I have a problem with displaying the menubar. When I execute the program, the menubar seems to cover the background image and all I see is the menu bar on the top of the screen and a black screen below it. It seems like the code works on windows but it somehow doesn't work on a mac. Anybody know how to fix this problem?

package dynamic_beat_4;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;

public class DynamicBeat extends JFrame {

    private Image screenImage;
    private Graphics screenGraphic;

    private Image introBackground = new ImageIcon(Main.class.getResource("../images/introBackground.jpg")).getImage(); 
    private JLabel menuBar = new JLabel(new ImageIcon(Main.class.getResource("../images/menuBar.png"))); 

    public DynamicBeat() {
        setUndecorated(true); 
        setTitle("Dynamic Beat"); 
        setSize(Main.SCREEN_WIDTH, Main.SCREEN_HEIGHT);
        setResizable(false);
        setLocationRelativeTo(null); 
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true); 
        setBackground(new Color(0, 0, 0, 0)); 
        setLayout(null); 

        menuBar.setBounds(0, 0, 1280, 30);
        add(menuBar); 

        Music introMusic = new Music("intromusic.mp3", true);
        introMusic.start();

    }

    public void paint(Graphics g) {
        screenImage = createImage(Main.SCREEN_WIDTH, Main.SCREEN_HEIGHT); 
        screenGraphic = screenImage.getGraphics();
        screenDraw(screenGraphic); 
        g.drawImage(screenImage, 0, 0, null);
    }


    public void screenDraw(Graphics g) {
        g.drawImage(introBackground, 0, 0, null);
        paintComponents(g); 
        this.repaint(); 
    }

}
Jiwoo Lee
  • 23
  • 6
  • [This is an expected behavior](https://stackoverflow.com/a/8701830/11198711) with java swing, but there are some decent [solutions](https://stackoverflow.com/questions/13439595/do-java-guis-display-the-same-on-all-operating-systems) – Andrew May 07 '19 at 18:22
  • 1
    Possible duplicate of [How to remove title bar in JFrame](https://stackoverflow.com/questions/8701716/how-to-remove-title-bar-in-jframe) – Andrew May 07 '19 at 18:23

1 Answers1

0

Why do you call it a "menubar"? It is a JLabel. Swing has a specific component called a JMenuBar, so your terminology is very confusing trying to understand what you are really doing.

I see many inconsistencies:

  1. You should NOT be overriding paint().
  2. You should NOT be using a null layout, or setBounds().
  3. You should NOT be invoking paintComponents() directly.
  4. You should NOT be invoking repaint() in a method called from a painting method. Basically you are creating an infinite loop.
  5. You should NOT be using getGraphics(). If you ever need to do custom painting then you just use the Graphics object passed to the painting method.

Not really sure what you are attempting to do but I would suggest that all you need to to use the defrault BorderLayout of the frame. Then the basic code is:

add(menuBar, BorderLayout.PAGE_START);
add(introBackground, BorderLayout.CENTER)
pack();
setVisible( true );

Read the section from the Swing tutorial on How to Use BorderLayout for more information and working examples to get you started.

camickr
  • 321,443
  • 19
  • 166
  • 288