Sign in to follow this  
DevlinHealy

Java Tileset Help!

Recommended Posts

Okay so, I'm kind lost here. Java has a really funky way of displaying graphics, and I've hit a wall trying to get this code to work so, here's the source:

//Frame Class handles the window

[code]
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;

import javax.swing.JFrame;

public class Frame extends JFrame {

public Frame() {
setTitle("Tile Test");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setResizable(false);
setSize(600, 400);
setVisible(true);

}
public static void main(String[] args) {
new Frame();
}
public void paint(Graphics g) {
}
public void paintComponent(Graphics g) {
}
}

// ^^^ Pretty fundamental stuff

[/code]/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

//Tile Class should do all the work.

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////[code]
import java.awt.Graphics;
import java.awt.Image;
import javax.swing.ImageIcon;

public class Tile {
int mapArray[][] = {{1, 2, 3},{3, 2, 1}, //this array is the "map" I am using. Each value in the array translates to the t1, t2, t3,
{2, 1, 3},{3, 1, 2}}; //images below

public int mapHeight = 4; //Number of tiles per map (keeping it small for now)
public int mapWidth = 4; // "
public final int tileWidth = 32; //Tile dimensions
public final int tileHeight = 32; // "
public int x; //x location of tile on screen
public int y; //"
int tileType = 0; //Which tile is it?
Image img;
ImageIcon t0 = new ImageIcon("C:\\test\\tile1.png"); //Tile graphics 1
ImageIcon t1 = new ImageIcon("C:\\test\\tile2.png"); //etc
ImageIcon t2 = new ImageIcon("C:\\test\\tile3.png");

public static void display(int x[][]) {
for(int row = 0; row < x.length; row++) {//loops through rows
for(int column = 0; column < x[row].length; column++) {
}}}}

[/code]

So I have the images configured-ish, I have the basic "loop through array and figure out what each tile is", but I dunno how to finish this... no tiling tutorials in Java online, I spent a long time looking. Tried to look at ones from other languages, all I could find was C# and XNA (and it was a really complex and functional tileset, very hard to follow he had error checking and such in it), and SDL (on Lazyfoo's SDL site), which I couldn't translate from SDL to Java.

So basically, I need to get the for statements to read the values at the index in the array, I figured I'd use some nested if statements, maybe a switch. Once that works I need to send the result of the for's to paintComponent in the frame class, to paint it on the screen.

Hoping for a miracle. Really appreciate your help. I've made a ton of games like platformers, and asteroids, but none of them directly get into tilesets.

Devlin

Share this post


Link to post
Share on other sites
Why don't you have an 2d array that has Image objects?
Then you can just loop through your 2d array, get the Image object then draw it using the java.awt.Graphics2D.

This book really help me: [url="http://www.brackeen.com/javagamebook/"]http://www.brackeen.com/javagamebook/[/url]





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