painting image background separately with the player image

i want to paint the image background on a JFrame once only. So i have a the following codes..

publicvoid paint(Graphics g)

{

g.drawImage(sea,0,0,this);

for(int across = 0; across < width ; across++)

{

for(int vert = 0; vert < height ; vert++)

{

if (map.A[vert][across] == 3)

{

g.drawImage(treasure,across*TILE_SIZE,vert*TILE_SIZE,this);

}

elseif (map.A[vert][across] == 4)

{

g.drawImage(bottomLeft,across*TILE_SIZE,vert*TILE_SIZE,this);

}

elseif (map.A[vert][across] == 5)

{

g.drawImage(midLeft,across*TILE_SIZE,vert*TILE_SIZE,this);

}

elseif (map.A[vert][across] == 6)

{

g.drawImage(topLeft,across*TILE_SIZE,vert*TILE_SIZE,this);

}

elseif (map.A[vert][across] == 7)

{

g.drawImage(topCenter,across*TILE_SIZE,vert*TILE_SIZE,this);

}

elseif (map.A[vert][across] == 8)

{

g.drawImage(topRight,across*TILE_SIZE,vert*TILE_SIZE,this);

}

elseif (map.A[vert][across] == 9)

{

g.drawImage(midRight,across*TILE_SIZE,vert*TILE_SIZE,this);

}

elseif (map.A[vert][across] == 10)

{

g.drawImage(bottomRight,across*TILE_SIZE,vert*TILE_SIZE,this);

}

elseif (map.A[vert][across] == 11)

{

g.drawImage(bottomCenter,across*TILE_SIZE,vert*TILE_SIZE,this);

}

elseif (map.A[vert][across] == 12)

{

g.drawImage(center,across*TILE_SIZE,vert*TILE_SIZE,this);

}

elseif (map.A[vert][across] == 13)

{

g.drawImage(grass,across*TILE_SIZE,vert*TILE_SIZE,this);

}

}

}

}

This code works fine and showed the image.

However when i wanted to paint the player image, (using a while loop to keep checking and updating the player position and show on the JFrame)...

publicvoid gameLoop()

{

gameRunning =true;

// keep looking while the game is running

while (gameRunning)

{

Graphics2D g = (Graphics2D) strategy.getDrawGraphics();

render(g);

// flip the buffer so we can see the rendering

g.dispose();

strategy.show();

logic();// the player movement

//pause a bit so that we don't choke the system

try{ Thread.sleep(4);}catch (Exception e){};

}

}

The background image will not show but only the player. The background appears white in colour.

Can anyone help how to paint the 2 things separately?

[5090 byte] By [Hanz_05a] at [2007-11-27 10:52:59]
# 1

A couple of questions:

1) Why are you using paint() rather than paintComponent? The latter is usually more appropriate for Swing components.

2) What component are you overriding the graphics on? Hopefully a JPanel and not a JFrame, correct?

3) Are you supposed to be calling super.paintComponent(g); (this of course after you correct the paint to paintComponent error)

4) In your gameLoop method, you refer to various methods but are hiding the code for the methods from us. Do you want us to hide our solutions from you?

Recommendations:

1) Make sure that you are overriding the right component. Not the JFrame, correct? Probably a JPanel.

2) Override paintComponent and call the super.paintComponent.

3) Create a Short, Self Contained, Compilable and Executable, Example Program (SSCCE) that demonstrates the incorrect behaviour. You can find out more about this construct here:

http://homepage1.nifty.com/algafield/sscce.html

4) Post this where it belongs, in the Swing forum.

Good luck!

petes1234a at 2007-7-29 11:41:13 > top of Java-index,Java Essentials,Java Programming...