Background images

I’m trying to setup the background for my game. i’m not sure however if what i’m doing is right. i haven’t been able to get it to work.

here’s an example: -

g.images[0] = Toolkit.getDefaultToolkit().getImage(“grass tile.jpg”);

g.images[1] = Toolkit.getDefaultToolkit().getImage(“boulder1.gif”);

g.images…

i’m doing this in the constructor of my class. is that the right place to do it??

cheers

Whats g? Is it the graphics context? Not quite sure what’re you’re trying to achieve… looks a little like javascript trying to manipulate the page.

You’ll have to override the paint() method of what ever component you’re using to draw to the screen. Inside the paint() method you can use g.drawImage() to draw the image you want…

Hope this helps,

Kev.

yeah g is the graphic context. Basically I’m trying to setup a background that has different images. i.e one part grass one part stone, so on and so on. I’m also doing this as a application not an applet.
how do i set-up the paint method within an application to implement this??

Check the share code section…

This thread could help…

http://www.java-gaming.org/cgi-bin/JGOForums/YaBB.cgi?board=share;action=display;num=1036791657

Thanks but thats not really what i want :frowning:

ok… some pseducode



public class MyFrame extends JFrame{

   Image[] images = new Image[2];

   public MyFrame(){
       images[0] = Toolkit.getDefaultToolkit().getImage("grass tile.jpg"); 
       images[1] = Toolkit.getDefaultToolkit().getImage("boulder1.jpg"); 
  }

  public void paint(Graphics g){
     //clear background
     g.setColor(Color.black);
     g.fillrect(0,0,width,height);

     //draw background
     g.drawImage(images[0], 0, 0, this);
 }

  public static void main(String[] args){
      MyFrame f = new MyFrame();
      f.setSize(400,200);
      f.show();
  }

}

is that what u r after?

yeah that looks more like it ;D thanks

just one question though.
How does it know the size of the images. Will it just take those two images and fill the whole screen, or will it just get the image size from the image file and place in the corner of the screen?

cheers

I have just made the frame 400x200 as an example. If you would like to have your frame changed after the sice of the images you should use following methods on your image objects to get their size…


image.getWidth(this); //this = Jframe, or use null
image.getHeight(this); //this = Jframe, or use null

And the image is being placed in the upper left cornor (i.e. the 0,0 in the abow code)