Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I recently watched a video on how to do tilemaps in java. He has the tiles as colored rectangles. I want them to be images.

My code:

public void draw(Graphics g){
    for(int row = 0; row < mapheight; row++){
        for(int col = 0; col < mapwidth; col++){

             int rc = map[row][col];

             if(rc == 0){
                 g.setColor(Color.green);
             }

             if(rc == 1){
                 g.setColor(Color.yellow);
             }


             g.fillRect(x + col * tilesize, y + row * tilesize,tilesize,tilesize);


        }
    }
}

This is the tilemap class. It says if rc is = to 0 it sets the color to green. Is there any way I can change that to an image?

share|improve this question

1 Answer 1

Without a fairly substantial change in your code, your options are limited.

If you take a look at the functions provided for you in the Graphics class, you'll find a drawImage function that will allow you to draw an image instead of using the existing fillRect function.

Even this simpler change is going to require more changes to your code than just one line. You'll need to add image loading, storage and selection.

I recommend you find a tutorial for tile maps using images, instead of attempting to modify this code to work for you.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.