1

This is one section of a Yahtzee game I'm working with. I am trying to set the background to be a yahtzee.png file that is in the project folder. I commented out my attempt to do so, because it's not working out for me. Is there a better way to set this up? This is how it looks now. There should be a custom image instead of the grey background.

     ExFrame(int numPlayers)
  { 
      frame = new JFrame();
      frame.setSize(450+150*numPlayers,700);

        frame.setTitle("YAHTZEE!");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        this.numPlayers = numPlayers;
        this.numGridRows = 20;
        this.buttonWidth = 140;
        this.numCreateButLabCalls = 0;
        this.component = new DiceComponent(buttonWidth*2);
        this.cButtons = new JButton[numGridRows];
        this.cButtonsText = new String[numGridRows];
        this.cLabels = new JLabel[numPlayers][numGridRows];
        this.statusLabel = new JLabel("<html>New game has been started!<br>Please select the dice that you wish to hold or click on a scoring button</html>");
        this.score = new YahtzeeScore[numPlayers];

        //populate score array
        for(int k = 0; k < numPlayers; k++)
        {
            score[k] = new YahtzeeScore(cButtons,cLabels, statusLabel, component.getDieArray(), cButtonsText, numGridRows, k);
        }

        statusLabel.setPreferredSize(new Dimension(buttonWidth*2, 100));
        centerPanel = new JPanel(new GridLayout(numGridRows,numPlayers+1)); //columns based on numPlayers

        component.rollDice(true);
        popCenterPanel();
        for(int k = 0; k < numPlayers; k++)
            score[k].reset();
        addListeners();

        frame.setLayout(null);
        frame.add(component);
        frame.add(statusLabel);
        frame.add(centerPanel);
    //  frame.add(new JLabel(new ImageIcon("/YahtzeeAgain/yahtzee.png")));
        Insets insets = frame.getInsets();
        Dimension size = statusLabel.getPreferredSize();
        statusLabel.setBounds(100+ insets.left,insets.top,size.width,size.height);
        size = component.getPreferredSize();
        component.setBounds(insets.left, 150 + insets.top,
                 size.width, size.height);
        size = centerPanel.getPreferredSize();
        centerPanel.setBounds(290 + insets.left, 140 + insets.top,
                 size.width, size.height);
    centerPanel.setBackground(Color.gray);
    frame.add(this);
    frame.setVisible(true); 


    }
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
Terik Brunson
  • 187
  • 1
  • 13
  • can you just check with absolute path and share the result? – marmeladze Apr 14 '15 at 20:33
  • If you can, avoid using a JLabel as a background component, it does not calculate its preferred size based on its child components, but the icon and text properties. Instead create a custom component and override its paintComponent method, for [example](http://stackoverflow.com/questions/24176008/background-image-for-a-jpanel-not-working/24176183#24176183), while a little more complicated, it is more flexible – MadProgrammer Apr 14 '15 at 21:32

2 Answers2

2

Best way:

Add your background JLabel to the Frame's contentpane (as you are doing), then do setLayout(null) on it, and add all other components to your background JLabel

ControlAltDel
  • 33,923
  • 10
  • 53
  • 80
  • Beware, that the JLabel will NOT calculate its preferred size based on the needs of it child components, but will use the icon and text properties of the label, which could present issues on different platforms... – MadProgrammer Apr 14 '15 at 21:29
0

How about

frame.setContentPane(new JLabel(new ImageIcon("/YahtzeeAgain/yahtzee.png")));
Undo
  • 25,519
  • 37
  • 106
  • 129
Ace McCloud
  • 798
  • 9
  • 27