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.

Hey can someone help me out, I've been trying to fix this for about 2 hours now, and I'm a noob when it comes to game programming and Java(only been reading and programming with it for a week now, this is my first non-tutorial driven attempt at anything).

Basically from my understanding my tile map class should be done correctly, here's the code in case I am mistaken: (file name is Map.java and its part of the tilemap package)

package tilemap;

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

public class Map{

    private final int TILE_SIZE = 40;
    private Image wall;
    private Image floor;

    private int[][] map = { {1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
                            {1, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
                            {1, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
                            {1, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
                            {1, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
                            {1, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
                            {1, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
                            {1, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
                            {1, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
                            {1, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
                            {1, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
                            {1, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
                            {1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }};
    public Map() {
        wall = new ImageIcon("wall.gif").getImage();
        floor = new ImageIcon("floor.gif").getImage();
    }

    public void paint(Graphics2D g) {
        // loop through all the tiles in the map rendering them
        // based on whether they block or not
        for (int across=0;across<10;across++) {
            for (int down=0;down<13;down++) {

                if (map[across][down] == 1) {
                    g.drawImage(wall, across*TILE_SIZE, down*TILE_SIZE, null);
                } else {
                    g.drawImage(floor, across*TILE_SIZE, down*TILE_SIZE, null);
                }

            }
        }
    }
}

Now I think the issue is in this section here in my GameCanvas.java:

public void gameLoop() {
        boolean gameRunning = true;
        long last = System.nanoTime();

        // keep looking while the game is running
        while (gameRunning) {
            Graphics2D g = (Graphics2D) strategy.getDrawGraphics();

            // clear the screen
            g.setColor(Color.black);
            g.fillRect(0,0,1000,1000);

            // render our game objects
            map.paint(g);

            // flip the buffer so we can see the rendering
            g.dispose();
            strategy.show();

            // pause a bit so that we don't choke the system
            try { Thread.sleep(4); } catch (Exception e) {};
        }
    }

And the full GameCanvas.java in case its else where:

package tilemap;

import java.awt.Canvas;
import java.awt.Color;
import java.awt.Frame;
import java.awt.Graphics2D;
import java.awt.image.BufferStrategy;

import tilemap.Map;

public class GameCanvas extends Canvas {

    /** The buffered strategy used for accelerated rendering */
    private BufferStrategy strategy;

    /** The map our player will wander round */
    private Map map;

    public static void main(String[] args) {
        new GameCanvas();
    }

    public GameCanvas() {
        Frame frame = new Frame("Tile Map Example");
        frame.setLayout(null);
        frame.add(this);
        frame.setSize(500,500);
        frame.setResizable(true);
        frame.setVisible(true);

        createBufferStrategy(2);
        strategy = getBufferStrategy();

        map = new Map();

        // start the game loop
        gameLoop();
    }

    public void gameLoop() {
        boolean gameRunning = true;
        long last = System.nanoTime();

        // keep looking while the game is running
        while (gameRunning) {
            Graphics2D g = (Graphics2D) strategy.getDrawGraphics();

            // clear the screen
            g.setColor(Color.black);
            g.fillRect(0,0,1000,1000);

            // render our game objects
            map.paint(g);

            // flip the buffer so we can see the rendering
            g.dispose();
            strategy.show();

            // pause a bit so that we don't choke the system
            try { Thread.sleep(4); } catch (Exception e) {};
        }
    }
}

I think the issue is when I go to draw/paint the screen there's something I'm not taking care of correctly, or maybe I'm not calling the right method (I've seen like 3 ways to draw to the screen, Canvas (which I think I'm using), JFrames/Jpanels, and OpenGL(which I have no idea about))

Please let me know what the issue is if you can spot it, cause I really have no idea without an compilation error poping up.

Thank you!

share|improve this question
add comment

3 Answers

up vote 1 down vote accepted

In your constructor, try this.setSize(500,500);, and you might also need this.setVisible(true);. If that still doesn't work, instead of giving the frame a null layout, try giving it a border layout and add this in the CENTER.

I wonder whether ImageIcon is not returning a proper image; try following the trail on Reading/Loading an Image.

Looks like you're using BufferStrategy correctly. You could try commenting out g.dispose();, but I think that's correct.

share|improve this answer
    
Ive changed the loading and drawing of images according to the link you posted but the only thing that changed anything is when I changed this line frame.setLayout(null); to this frame.setLayout(new BorderLayout()); now when it starts I get a black screen instead of a white one. –  Latency Jul 10 '11 at 17:29
1  
Well a black screen is much better than a white one, seeing as you're filling the screen with black. In your clear screen piece, change black to some other color and see if the window is filled with that color; if so, then you at least know the screen is being drawn, and then I would look into why the images aren't being drawn; perhaps they aren't being loaded (i.e. the path is wrong) and I would definitely debug or println to figure that out for sure. –  Ricket Jul 10 '11 at 18:04
    
The screen gets filled with the color that I define whether its blue, red, black, ect. so it looks like the draw is working, I'll take another look at the image loading, load from C: instead of from the project(just to test it), then again maybe the way I'm doing it doesn't work with Canvas or maybe I'm forgetting a step at some point. Thanks for the help I'll check those out options out. –  Latency Jul 10 '11 at 18:30
    
It was an issue with the image loading, thanks for all the help, I'll accept this as the answer :) –  Latency Jul 11 '11 at 11:03
add comment

You're not setting the size of the Canvas before you create the buffer strategy.

this.setSize(500,500);

Also, you have an out of bounds exception, which d33j has already pointed out.

share|improve this answer
    
do I not set this size in the GameCanvas with this line: frame.setSize(500,500); which I create a new instance of in the main? –  Latency Jul 10 '11 at 18:24
1  
You do set it after you create the buffer strategy, but I meant that you need to set it before. –  skyuzo Sep 12 '11 at 20:11
add comment

I see an out of bounds exception. Try changing 2 lines to this:

        for (int across=0; across<13; across++) {
            for (int down=0; down<10; down++) {
share|improve this answer
    
I still get the same result, a blank white window, whether its that way or the way I had it originally, Although I didn't know I was doing the nested loop backwards, thank you for pointing that out. –  Latency Jul 10 '11 at 5:30
add comment

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.