Sign in to follow this  
Swattkidd

Java tile based map

Recommended Posts

Ok heres the problem, i am going through a "tutorial" with someone on another thread, but he is using a different way to paste the tiles and all that, this is what i thought it would be like( the way i want it) map[[1,1,1,1,1] 1,0,0,0,1 1,0,0,0,1 1,1,1,1,1]] with 1's being solid land and 0's being water.... so if someone can give me an example, that would be great HERE IS THE WAY IM DOING IT RIGHT NOW, WHICH HAS MANY EXCEPTION ERRORS **********************************************************************************
[SOURCE]

import java.awt.*;

import javax.swing.*;

import java.applet.*;

import java.awt.*;

import java.util.*;

 

public class Mymap extends Applet {

    int across;

   int vert;

   int graph[][] = new int[20][20];

    Image img = Toolkit.getDefaultToolkit().getImage("Game.GIF");

    Image pic = Toolkit.getDefaultToolkit().getImage("Game1.GIF");

    MediaTracker track = new MediaTracker(this);

 

 

    public void init() {

        // Register images with media tracker
        setSize(300,300);
        track.addImage(img, 0);

        track.addImage(pic,1);

 

        // populate ints

        populateGraph();

    } 

 

    public void paint(Graphics g) {

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

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

                if (graph[across][vert] == 0) {

                    // we draw img
                    g.drawImage(pic,across*15,vert*15,this);

                } else {

                    // we draw pic
                    g.drawImage(img,across*15,vert*15,this);
                    
                    

                }

            }

        }

    }

 

    public void populateGraph() {

         Random r = new Random();

        for (int x = 0; x <10; x++) {

            for (int y=0; y<10;y++) {

                graph[x][y] = r.nextInt(2);

            }
        }
    }
}[/SOURCE]
[/source] Exceptions***
Exception in thread "AWT-EventQueue-1" java.lang.ArrayIndexOutOfBoundsException: 15
        at Mymap.paint(Mymap.java:57)
        at java.awt.Container.update(Container.java:1730)
        at sun.awt.RepaintArea.updateComponent(RepaintArea.java:239)
        at sun.awt.RepaintArea.paint(RepaintArea.java:216)
        at sun.awt.windows.WComponentPeer.handleEvent(WComponentPeer.java:254)
        at java.awt.Component.dispatchEventImpl(Component.java:4031)
        at java.awt.Container.dispatchEventImpl(Container.java:2024)
        at java.awt.Component.dispatchEvent(Component.java:3803)
        at java.awt.EventQueue.dispatchEvent(EventQueue.java:463)
        at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.java:242)
        at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:163)
        at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:157)
        at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:149)
        at java.awt.EventDispatchThread.run(EventDispatchThread.java:110)
Exception in thread "AWT-EventQueue-1" java.lang.ArrayIndexOutOfBoundsException: 15
        at Mymap.paint(Mymap.java:57)
        at sun.awt.RepaintArea.paintComponent(RepaintArea.java:248)
        at sun.awt.RepaintArea.paint(RepaintArea.java:224)
        at sun.awt.windows.WComponentPeer.handleEvent(WComponentPeer.java:254)
        at java.awt.Component.dispatchEventImpl(Component.java:4031)
        at java.awt.Container.dispatchEventImpl(Container.java:2024)
        at java.awt.Component.dispatchEvent(Component.java:3803)
        at java.awt.EventQueue.dispatchEvent(EventQueue.java:463)
        at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.java:242)
        at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:163)
        at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:157)
        at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:149)
        at java.awt.EventDispatchThread.run(EventDispatchThread.java:110)
[Edited by - Swattkidd on October 4, 2006 6:56:38 PM]

Share this post


Link to post
Share on other sites
Here is the updated code that only prints tiles going down the side

import java.awt.*;

import javax.swing.*;

import java.applet.*;

import java.awt.*;

import java.util.*;



public class Mymap extends Applet {

int across;

int vert;

int width = 20;

int height = 20;

int graph[][] = new int[15][15];

Image img = Toolkit.getDefaultToolkit().getImage("Game.GIF");

Image pic = Toolkit.getDefaultToolkit().getImage("Game1.GIF");

MediaTracker track = new MediaTracker(this);





public void init() {

// Register images with media tracker

track.addImage(img, 0);

track.addImage(pic,1);



// populate ints

populateGraph();

}



public void paint(Graphics g) {

for(int across = 0; across < width ; across++) {

for(int vert = 0; vert < height ; vert++) {

if (graph[across][vert] == 0) {

// we draw img
g.drawImage(pic,across*15,vert*15,this);

} else {

// we draw pic
g.drawImage(img,across*15,vert*15,this);



}

}

}

}



public void populateGraph() {

Random r = new Random();

for (int x = 0; x <10; x++) {

for (int y=0; y<10;y++) {

graph[x][y] = r.nextInt(2);

}
}
}
}

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Your problem is in the different sizes you are using for your map array

int graph[][] = new int[15][15];

....
width and height is set to 20!
for(int across = 0; across <

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
for(int across = 0; across

getWidth() and getHeight() are the width and height of the applet in pixels. You need the need the size of the array. Something like this:

for(int across = 0; across

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