Skip to main content
gamedev.net
🔒 Locked

[java] Java tile maps

Started by Swattkidd Oct 3, 2006 at 12:21 AM 1 replies 3.8k views
Original Post
Swattkidd
Swattkidd
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); } } } }
LorenzoGatti
LorenzoGatti
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
Omae Wa Mou Shindeiru
Swattkidd
Swattkidd
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 for(int vert = 0 ; vert
g.drawImage(pic,across,vert,this);
}
}

}


}

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.