Sign in to follow this  
Swattkidd

[java] Java tile maps

Recommended Posts

Ok well im trying to create a tile map and im following this step by step process which i find VERY useful, ne way, i drew the picture, then i drew it for the top line...but i cant figure out how to get it to draw for the whole applet. Like the way i have it now it goes and then stops when it reaches the end. But thats it, i dont know how make it go to the next line and so on. heres what i have import java.awt.*; import javax.swing.*; import java.applet.*; public class Mymap extends Applet { int x = 15; int y = 5; Image pic = Toolkit.getDefaultToolkit().getImage("Game.GIF"); Image pic1 = Toolkit.getDefaultToolkit().getImage("Game1.GIF"); public void init() { } public void paint(Graphics g) { for(int x = 15;x < 300; x=x+15) { if(pic != null) { g.drawImage(pic,x,y,this); } } } }

Share this post


Link to post
Share on other sites
You draw one line of tiles with a fixed y=5 (set after the redundant x=15 line); you should add a nested loop varying y, like the x one.

It might be smart to introduce auxiliary constants or variables: the width and height of a tile, the offset (currently x=15 and y=5) where the first tile should be drawn, the number of displayed tiles in each direction (which is more meaningful than the currently used pixel coordinate).
Numbers of unclear origin in random positions (e.g. "for(int x = 15;x < 300; x=x+15)") are hard to read and modify. For example in your code the two uses of 15 are completely different; "for(x=startX;x<maxX;x+=tileWidth)" would be more meaningful.

Share this post


Link to post
Share on other sites
Ok well my code is updated and now i need to figure out how to create a 2 dimensional array in order to place each tile.
Heres my code

import java.awt.*;
import javax.swing.*;
import java.applet.*;

public class Mymap extends Applet
{


int across;
int vert;

Image pic = Toolkit.getDefaultToolkit().getImage("Game.GIF");
Image pic1 = Toolkit.getDefaultToolkit().getImage("Game1.GIF");
public void init()
{

}
public void paint(Graphics g)
{


for(int across = 0;across <getWidth() ; across+=15){
for(int vert = 0 ; vert <getHeight();vert+=15){

g.drawImage(pic,across,vert,this);
}
}

}


}

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this