RIZY101
RIZY101

Reputation: 29

Adding a jpanel over a jlabel background

I know this question has been asked before but i cant seem to implement any of the other answers to my project. So i have my paint method in my player class here.

public void paintComponent(Graphics g)
{
   //makes player(placeholder for real art)
   super.paintComponent(g);
   g.setColor(Color.GREEN);
   g.fillRect(x,y,50,30);
}

Then I have my main class here.

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
/**
* Write a description of class Main here.
* 
* @author Richard Zins 
* @V01
*/
public class Main extends JFrame
{
    public static void main(String[]args)
    {
     Player p1 = new Player();
     Main m = new Main(p1);


     }

     public Main(Player p1)
     {
      JFrame ar = new JFrame();
      JLabel background = new JLabel(new ImageIcon("/Users/rizins/Desktop/PacManTestBackGround.jpg"));
      ar.setTitle("Runner Maze");
      ar.setSize(800,600); 
      ar.add(background);
      ar.setVisible(true);
      ar.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      ar.add(p1);
   }
}

Now I cant seem to get my player object to paint over my background any help would be appreciated!

Upvotes: 1

Views: 1678

Answers (3)

MadProgrammer
MadProgrammer

Reputation: 347244

There are a couple of mistakes...

  1. JPanel by default is opaque, so you need to change it to be transparent
  2. JFrame uses a BorderLayout by default, so only one component will be shown at the (default) center position, in this case, the last thing you add.
  3. You should call setVisible last

Instead, set a layout manager for the JLabel and add your player class to it. Instead of adding the JLabel to the frame, you should make the label the contentPane for the frame, for example...

p1.setOpaque(false);
JFrame ar = new JFrame();
ar.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JLabel background = new JLabel(new ImageIcon("/Users/rizins/Desktop/PacManTestBackGround.jpg"));
ar.setTitle("Runner Maze");
ar.setContentPane(background);
ar.setLayout(new BorderLayout());
ar.add(p1);
ar.pack(); 
ar.setVisible(true);

I should point out that using a JLabel to display a background image like this could cause you problems, as the JLabel only uses the text and icon properties to calculate its preferred size, this could cause some child components to be laid out beyond its visible range.

See How to set a background picture in JPanel for more details and a possible solution

Upvotes: 2

S.Klumpers
S.Klumpers

Reputation: 420

Use a JLayeredPane and add your background at the index 0 (indexed with an Integer not an int). Then you add another JPanel that is not opaque (like in @Bahramdun Adil 's answer) and add your player to that.

This way you can have a background and display your player at the same time.

Upvotes: 0

Bahramdun Adil
Bahramdun Adil

Reputation: 6079

You can make the JPanel transparent by setting the opaque to false. e.g:

panel.setOpaque(false)

Try is if this work for you.

Upvotes: 0

Related Questions