Y. Markov
Y. Markov

Reputation: 11

Images in JFrame don't display

I am trying to add images to my program and I have a problem with 2 of them. Ball.png and ground.png don't display.

Where did I make a mistake?

When I add images of ball and ground as a background, there is no problem.

    public class Game extends JPanel implements ActionListener {

Image ball;
Image ground;
Image arrow;

public Game(){
    try {
        ball = ImageIO.read(new File("ball.png"));
    } catch (IOException e) { 
        System.out.println("pliku brak");
    }
    try {
        ground = ImageIO.read(new File("ground.png"));
    } catch (IOException ex) {
        System.out.println("pliku brak");
    } 
}

@Override
public void paintComponent(Graphics g) {
    super.paintComponent(g);
    g.drawImage(ball, 100, 100, null);
    g.drawImage(ground, 300, 300, null);
}
@Override
public void actionPerformed(ActionEvent e) {
    throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}


public class Start {
public static void main(String[] args) {
    SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            okienko();
        }
    });
}

public static void okienko()
{    

    JFrame frame = new JFrame("Hold The Ball");

    try
    {
        frame.setContentPane(new JLabel(new ImageIcon(ImageIO.read(new File("background.png")))));
    } catch (IOException e)
        {
            System.out.println("pliku brak");
        }
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.add(new Game());
    frame.setSize(1000, 600);
    frame.setResizable(false);
    frame.setVisible(true);       
}
}

Upvotes: 1

Views: 125

Answers (3)

Andrew Thompson
Andrew Thompson

Reputation: 168845

Don't try to layer components like this.

The best approach here would be to paint the background at the same time as painting the other game elements. To do that:

  1. Change

    Image arrow; 
    

    to

    Image arrow; 
    Image bg; // declare the BG as a class attribute of the Game class
    
  2. Load the BG at the same time as the other two images.
  3. Paint the BG first (at 0x0) in the paintComponent(Graphics) method.

Upvotes: 2

ArcticLord
ArcticLord

Reputation: 4039

You cannot see your Game JPanel because your JFrame is filled with the frame.setContentPane(...) Background JLabel. You need to use a LayoutManager to be able to lay your Panel over the other.

 frame.setLayout(new BorderLayout());
 frame.add(new Game(), BorderLayout.CENTER);

Will fix that.
But then you will not see the Background since your Game JPanel is on top of all. To fix that you should make this Panel have an invisible background.
So add

setOpaque(false);

to your Game Constructor.

Upvotes: 1

Erkan
Erkan

Reputation: 41

You should use image path with ImageIcon then setIcon to JLabel.

ImageIcon icon = new ImageIcon("images/background.png"); JLabel label = new JLabel(icon);

You can read this : https://docs.oracle.com/javase/tutorial/uiswing/components/icon.html

Upvotes: -1

Related Questions