0

Whenever I add a background (Image img) to my JFrame I am unable to see my menu bar .... Any help would be greatly appreciated ... I'm just learning JFrames and am probably overlooking something stupid.

class GameFrame extends JFrame {

private JLabel statusbar;
Image img = new ImageIcon("splash.png").getImage();

public GameFrame() {
    initUI();
    menuUI();
    BackgroundLoader bg = new BackgroundLoader();
}

@Override
    public void paint(Graphics g) {
    try {
        Image img = ImageIO.read(new File("splash.png"));             
        g.drawImage(img, 0, 0, this.getWidth(), this.getHeight(), null);
    } catch (IOException e) {
        e.printStackTrace();
    }

}

public final void initUI() {
    setTitle("Super RPG Hero: The Quest for Fame and Fortune");
    setSize(800, 480);
    setLocationRelativeTo(null);
    setDefaultCloseOperation(EXIT_ON_CLOSE);

    //JLabel background = new JLabel(splash);
    //background.setBounds(0, 0, splash.getIconWidth(), splash.getIconHeight());
    //getLayeredPane().add(background, new Integer(Integer.MIN_VALUE));

}

public final void menuUI() {


    JMenuBar menubar = new JMenuBar();

    //Creates file menu item
    JMenu file = new JMenu("File");
    file.setMnemonic(KeyEvent.VK_F);

    //Creates Object for New Game toolbar
    JMenuItem newItem = new JMenuItem("New Game");
    newItem.setMnemonic(KeyEvent.VK_C);
    newItem.setToolTipText("New Game");
    newItem.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent event) {
            String playerName = "Peter";
            CharacterCreator characterOne = new CharacterCreator(playerName);
            characterOne.statBuilder();
        }
    });

    //Creates Object for Save Game toolbar
    JMenuItem saveItem = new JMenuItem("Save");
    saveItem.setMnemonic(KeyEvent.VK_C);
    saveItem.setToolTipText("Save Game");

    //Creates Object for Load Game toolbar
    JMenuItem loadItem = new JMenuItem("Load");
    loadItem.setMnemonic(KeyEvent.VK_C);
    loadItem.setToolTipText("Load Game");

    //Creates Object for Exit Game toolbar
    //And creates method for the game to exit
    JMenuItem exitItem = new JMenuItem("Exit");
    exitItem.setMnemonic(KeyEvent.VK_C);
    exitItem.setToolTipText("Exit Game");
    exitItem.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent event) {
            System.exit(0);
        }
    });

    //Adds created objects to GUI
    file.add(newItem);
    file.add(saveItem);
    file.add(loadItem);
    file.add(exitItem);
    menubar.add(file);

    setJMenuBar(menubar);

}

}

mKorbel
  • 109,525
  • 20
  • 134
  • 319
Peter Chappy
  • 1,165
  • 4
  • 22
  • 41
  • 1) What is a `BackgroundLoader`? 2) Don't load images in `paint()` or `paintComponent()` 3) Don't extend frame. 4) When drawing an image that is loaded asynchronously, use an `ImageObserver` (or use `ImageIO` to load it). 5) Set the preferred size of the custom component, rather than the size of the frame itself. Then call `pack()`. 6) For better help sooner, post an [SSCCE](http://sscce.org/). – Andrew Thompson Feb 27 '12 at 23:11

1 Answers1

4

You should implement paintComponent() and not paint().

By overriding paint and not delegating up, you're not letting the JFrame paint what it needs to paint.

Also, look at this answer.

Community
  • 1
  • 1
Reverend Gonzo
  • 39,701
  • 6
  • 59
  • 77
  • 1
    More specifically -- he should override `paintComponent(...)` for a JPanel or other component that derives from JComponent (including JComponent itself) and then either add this to the JFrame's contentPane or make it the contentPane. – Hovercraft Full Of Eels Feb 27 '12 at 22:33