1

I am messing with java graphics for the first time, after a lot of searching I still can not seem to successfully add in a background, I Have my background image in a seperate file, pre-drawn and i'm trying to import it into my project.... Any help would be VERY appreciated!

    public static void startMenu(){
        ImagePanel panel = new ImagePanel(new ImageIcon("C:/Background.jpg").getImage());

        // Loads the background image and stores in img object
        Window game = new Window();
        JFrame frame = new JFrame();

        JButton startButton = new JButton("Start"); //makes a button with the text Start
        startButton.setBounds(300, 400, 89, 23);
        startButton.setVisible(true);

        frame.getContentPane().add(startButton);
        frame.getContentPane().add(panel);

        frame.add(game);
        frame.pack();
        frame.setTitle(title);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setResizable(false);
        frame.setSize(WIDTH, HEIGHT);
        frame.setLocationRelativeTo(null); // starts it in the center of the
                                            // screen
        frame.setVisible(true);


        game.start(); // Initialize start method
    }
    public static void startButton(){

        }

}


class ImagePanel extends JPanel {

      private Image backgroundImg;

      public ImagePanel(String backgroundImg) {
        this(new ImageIcon(backgroundImg).getImage());
      }

      public ImagePanel(Image backgroundImg) {
        Dimension size = new Dimension(backgroundImg.getWidth(null), backgroundImg.getHeight(null));
        setPreferredSize(size);
        setMinimumSize(size);
        setMaximumSize(size);
        setSize(size);
        setLayout(null);
      }

      public void paintComponent(Graphics g) {
        g.drawImage(backgroundImg, 0, 0, null);
      }

    }
2

1 Answer 1

1

A few things

  1. Look at the constructor you're calling:

    new ImagePanel(new ImageIcon("C:/Background.jpg").getImage());
    

    Then look at the constructor and ImagePanel class

    class ImagePanel extends JPanel {
    
        private Image backgroundImg;
    
        public ImagePanel(Image backgroundImg) {
            Dimension size = new Dimension(
                                 backgroundImg.getWidth(null),
                                 backgroundImg.getHeight(null));
            ...
        }
    
        public void paintComponent(Graphics g) {
            g.drawImage(backgroundImg, 0, 0, null);
        }
    }
    

    So you're not actually doing anything with the Image that you pass to the construtor, besides using its size. You should probably use it to initialize the backgroundImg. Like so:

    private Image backgroundImg;
    
    public ImagePanel(Image backgroundImg) {
        this.backgroundImg = backgroundImg;
    }
    
  2. Look at these lines

    frame.getContentPane().add(startButton);
    frame.getContentPane().add(panel);
    frame.add(game);
    

    FYI, frame.add() does the same thing as frame.getContentPane().add(). And in your case, since you haven't changed the layout of the frame/contentpane, you are left with the default BorderLayout. What this means for you is that if you don't specify the position when adding a component, (like frame.add(component, BorderLayout.PAGE_END)), then implicitly, you are trying to add to the center, (like frame.add(component, BorderLayout.CENTER)). Problem with this is that each BorderLayout position can only hold one component - the last one you add wins. So in your case game wins.

    See Laying out Components Withing a Container for some other ideas for layout management. Also more directly, see How to use BorderLayout

  3. Window is an AWT component. You shouldn't try and add AWT components to Swing Components. May be better to just use JPanel or JComponent, though not really sure the purpose of the window

  4. g.drawImage(backgroundImg, 0, 0, null);null should be this instead.

  5. Get rid of all your get[Xxx]Size() in your ImagePanel constructor. You don't need them, better to just override getPreferredSize().

    class ImagePanel extends JPanel {
    
        private Image backgroundImg;
    
        public ImagePanel(Image backgroundImg) {
            this.backgroundImg = backgroundImg;
        }
    
        @Override
        public Dimension getPreferredSize() {
            return new Dimension(backgroundImage.getWidth(this),
                                 backgroundImage.getHeight(this));
        }
    }
    

    Also have a look at Should I avoid the use of set(Preferred|Maximum|Minimum)Size methods in Java Swing?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.