编译后的Java不会绘制地图图层

时间:2014-04-16 14:53:48

标签: java graphics awt

由于某种原因,当我编译我的程序时,它不会绘制地图图层,但是如果我调试或从eclipse运行它会绘制它们。然而,当它作为一个独立的jar文件运行它之后,它确实绘制了播放器,而不是地图。

任何人都可以看一看并告诉我究竟发生了什么事吗?

对于那些下载有问题的人

这是我的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);
                }

            }
        }
    }
}

这是我的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 from eclipse

3 个答案:

答案 0 :(得分:0)

  1. 遵循Java命名约定Ground Ground =没有让您感到困惑?变量以小写字母Ground GroundGround ground

  2. 开头
  3. 摆脱static课程中的所有Ground。这些都应该是实例方法和变量。

  4. 使Ground ground;成为一个类成员(全局)。目前它在构造函数中本地作用域。您可能认为执行Ground.draw(g);正在使用实例Ground,但它并没有。您静态调用Ground类方法,在这种情况下,draw方法中调用的所有变量默认为0

  5. 您也可以使用init方法调用其他课程相同的三点。


  6. public class Play {
        private Ground ground;
    
        public void inti() {
            ground = new Ground(...)
        }
    
        public void draw(...) {
            ground.draw(g);
        }
    }
    

    您真的需要阅读一些关于实例字段和方法与static字段和方法之间差异的好教程,以及如何使用它们。您可以启动here

答案 1 :(得分:0)

问题解决了。它试图从外部获取文件。

答案 2 :(得分:0)

  1. 您需要在项目下创建图像或地图文件夹。不在源目录中。

  2. 您需要将图像或地图文件夹作为Java构建路径的一部分。

  3. 您需要像这样阅读您的地图。每张地图一种方法。


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