Question

For some reason when I compile my program it does not draw the map layers, however it does draw them if I debug or run from eclipse. However after it when I run it as a independent jar file it does draw the player, just not the maps.

Can anyone take a look and tell me what on earth is going on?

for those who have problems downloading

Here is my ground.java

package com.ChimpyGames.Tile;

import java.awt.Color;
import java.awt.Graphics;
import java.io.BufferedReader;
import java.io.FileReader;

public class Ground {

    private int x;
    private int y;

    private int tileSize;
    private int[][] map;
    public int mapWidth;
    public int mapHeight;

    public Ground(String s, int tileSize) {

        this.tileSize = tileSize;

        try {
            BufferedReader br = new BufferedReader(new FileReader("src/maps/" + s
                    + "/Ground.map"));

            mapWidth = Integer.parseInt(br.readLine());
            mapHeight = Integer.parseInt(br.readLine());
            map = new int[mapHeight][mapWidth];

            String delimiters = " ";
            for (int row = 0; row < mapHeight; row++) {
                String line = br.readLine();
                String[] tokens = line.split(delimiters);

                for (int col = 0; col < mapWidth; col++) {
                    map[row][col] = Integer.parseInt(tokens[col]);
                }
            }

        } catch (Exception e) {
        }

    }

    public static void update() {

    }

    public static void draw(Graphics g) {
        for (int row = 0; row < mapHeight; row++) {
            for (int col = 0; col < mapWidth; col++) {

                int rc = map[row][col];

                if (rc == 0) {

                }
                if (rc == 1) {
                    g.setColor(Color.WHITE);
                    g.fillRect(x + col * tileSize, y + row * tileSize,
                            tileSize, tileSize);
                }

            }
        }
    }
}

Here is my Play.java

package com.ChimpyGames.Menus;

import java.awt.Color;
import java.awt.Graphics2D;

import com.ChimpyGames.GamePanel;
import com.ChimpyGames.Entities.Player;
import com.ChimpyGames.Handlers.Keys;
import com.ChimpyGames.Tile.Ground;
import com.ChimpyGames.Tile.Mask1;
import com.ChimpyGames.Tile.Mask2;
import com.ChimpyGames.Tile.Overlay1;
import com.ChimpyGames.Tile.Overlay2;
import com.ChimpyGames.Tile.Scripts;

public class Play extends Menu {
    Player player;
    private String map;
    public int tileSize;
    public Ground ground;
    public Mask1 mask1;
    public Mask2 mask2;
    public OVerlay1 overlay1;
    public Overlay2 overlay2;
    public Scripts scripts;

    public Play(MenuManager gsm) {
        super(gsm);
        init();
    }

    @Override
    public void init() {
        // load the map by it's folder not by the .map
        map = "DefaultMap";
        tileSize = 32;

        // Load Individual layers
        ground = new Ground(map, tileSize);
        mask1 = new Mask1(map, tileSize);
        mask2 = new Mask2(map, tileSize);
        overlay1 = new Overlay1(map, tileSize);
        overlay2 = new Overlay2(map, tileSize);
        scripts = new Scripts(map, tileSize);

        player = new Player();


    }

    @Override
    public void update() {
        Ground.update();
        Mask1.update();
        Mask2.update();

        player.update();

        Overlay1.update();
        Overlay2.update();
        Scripts.update();
    }

    @Override
    public void draw(Graphics2D g) {
        handleInput();
        g.setColor(Color.BLACK);
        g.fillRect(0, 0, GamePanel.width, GamePanel.height);
        Ground.draw(g);
        Mask1.draw(g);
        Mask2.draw(g);
        // Keep above code at top

        player.draw(g);

        // Keep below code at bottom
        Overlay1.draw(g);
        Overlay2.draw(g);
        Scripts.draw(g);
    }

    @Override
    public void handleInput() {
        player.up(Keys.keyState\[Keys.UP\]);
        player.down(Keys.keyState\[Keys.DOWN\]);
        player.left(Keys.keyState\[Keys.LEFT\]);
        player.right(Keys.keyState\[Keys.RIGHT\]);

        if (Keys.keyState\[Keys.ENTER\]) {
        }
    }
}

Ran as executable Ran as executable

Ran from eclipse Ran from eclipse

Was it helpful?

Solution

  1. You need to create an images or a maps folder right under your project. Not in the source directory.

  2. You need to make the images or maps folder part of your Java build path.

  3. You need to read your maps like this. One method per map.


public Image getFrameImage() {
    Image image = null;
    try {
        image = ImageIO.read(getClass().getResource(
                "/package_games_board.png"));
    } catch (IOException e) {

    }
    return image;
}

OTHER TIPS

  1. Follow Java naming convention Ground Ground =, That doesn't confuse you? Variables begin with lower case letters Ground GroundGround ground

  2. Get rid of all the static in your Ground class. Those should all be instance methods and variable.

  3. Make Ground ground; a class member (global). Currently it's locally scoped in the constructor. You may think that doing Ground.draw(g); is using the instance Ground but it doesn't. You are calling the Ground class method staticly, in which case all the variable called in your draw method are defaulted to 0

  4. Same three points go for your other classes also that you call inside you init method.


public class Play {
    private Ground ground;

    public void inti() {
        ground = new Ground(...)
    }

    public void draw(...) {
        ground.draw(g);
    }
}

You really need to read some good tutorials on the difference between instance fields and methods and static fields and methods, and how to use them. You can start here

Problem was solved. it was trying to get files externally.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top