i'm trying to write a game that show a background and a player on screen simultaneously using 2 different classes.

here are the relevant parts of the program, or at least i think

public WorkingTitle2(){
		super("Working Title");
		setSize(550,429);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		loadPictures();
		loadSounds();
		
		
		pp = new PaintP();
		pb = new PaintB();
		
		add(pb);
		add(pp);
			
		setVisible(true);
		addKeyListener(new ButtonListener());
		
	}

private class PaintB extends JPanel{
		public void paintComponent(Graphics g){
			super.paintComponent(g);
			g.drawImage(background,0,0, this);
		}
	}
	private class PaintP extends JPanel{
		public void paintComponent(Graphics g){	
		
			super.paintComponent(g);	
			g.drawImage(player2,playerX,playerY, this);
			
		}
}//close paintComponent

public static void main (String[ ] args)
	{
		WorkingTitle2 wt = new WorkingTitle2();
	}

can someone tell me why doesn't this work? only 1 of the image shows.

Use println to see if all of your code is executing. also you have super.paintComponent(g); in both of your codes. make sure that they are not interfering with each other. maybe try calling one paint from the other.

pp is not executing because i added that to the frame first. how do i make sure that they're not interfering?

like I said try calling one paint from the other, and remove the super.paintComponent from the second one and then that may fix out issue of the second one erasing the first one.

i'm not exactly show if this is right, but it doesn't work. the background is not showing.

private class PaintB extends JPanel{
		public void paintComponent(Graphics g){
			super.paintComponent(g);
			g.drawImage(background,0,0, this);
			ps.repaint();
		}
	}
	private class PaintScreen extends JPanel{
		public void paintComponent(Graphics g){	
			g.drawImage(player2,playerX,playerY, this);
			
		}

Why do you want to separate the background and the character into separate paint methods anyway? If that didn't work, then I have no other ideas.

You add two panels to your window, which appears to have the default (ie Border) layout. You don't specify where to place them so you get the default value, and they are both placed in the same place, so you only see the second one.
Two better ways to do this:
1. Have 1 panel with a paintComponent that paints the background and the player (which could be delegated to the player class)
2. Have the background as a panel, and make the player a JLabel placed in the panel. You could either override paintComponent for the label, or maybe make your player graphic an ImageIcon and use JLabel's normal painting. This solution is also easier if you want to do things like clicking on the player, or dragging it.

pp = new PaintPlayer();//extends jlabel
pb = new PaintB();//extends jpanel
		
pb.add(pp);
add(pb);

is the above code correct?

That looks reasonable. You'll need to set a null LayoutManager for the panel so you can use absolute positioning to put the label exactly where you want it.
ps
You can ask here if your code is correct and you will get one or two opinions.
In the same time or less you can run it through the compiler and test it and get a definitive answer.

i believe this is what you're talking about with nulling out the layout manager, but it isn't working. is there something else i need to do?

private class PaintB extends JPanel{
		public PaintB(){
			setLayout(null);
		}
		public void paintComponent(Graphics g){
			super.paintComponent(g);
			g.drawImage(background,0,0, this);
		}
	}

When you add the label you need to set the label's bounds (position & size) explicitly.

ok i'll give that a try tomorrow as i have a ton of homework to do right now. thanks for the suggestion.

so add this...

pp.setHorizontalAlignment(JLabel.LEFT);
pp.setPreferredSize(new Dimension(175, 100));

...in the constructor?

With a null layout, use setLocation() or setBounds().

public PaintPlayer(){
    setBounds(0,50, 200,200);
    setLocation(0,50);
}

i don't know if this is right, but it's not working either.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.