【问题标题】:Store objects in my 2D array and show it on a JFrame将对象存储在我的二维数组中并在 JFrame 上显示
【发布时间】:2016-09-19 12:31:21
【问题描述】:

嗨 ppeps(英文破译),

我正在开发一款 2D 迷宫游戏。我需要一些帮助来将对象存储在我的二维数组中。我在这个上得到了一个 NullPointer:this.currentMap[colsCount][rowsCount] = objects.get(col);。主程序调用 LevelGenerator 构造函数。

 public final class LevelGenerator extends JFrame {

private HashMap<String, ItemObject> objects = new HashMap<>();
private ItemObject[][] currentMap;
private int HEIGHT = 320;
private int WIDHT = 480;

public JFrame frame = null;

public Level currentLevel = null;

private final List<Level> levels
        = new ArrayList<Level>() {
            {
                add(new Level001());
                add(new Level002());
                add(new Level003());
            }
        };

public LevelGenerator() {
    // Vul de frame
    //this.frame = frame;
    // Vul de objecten lijst
    //objects.put("B", new Bazooka());
    objects.put("", new EmptyTile());
    objects.put("W", new Wall());
    this.currentLevel = levels.get(0);

    this.Load();
}

/// Laad de map in
public void Load() {
    int rowsCount = 0;
    int colsCount = 0;

    for (String[] row : this.currentLevel.map) {
        for (String col : row) {
            this.currentMap[colsCount][rowsCount] = objects.get(col);
            colsCount += 1;
        }

        rowsCount += 1;
    }

    this.Start();
}

public void Start() {

    this.frame.setSize(this.HEIGHT, this.WIDHT);
    this.frame.setLayout(new GridLayout(15, 10));
    this.frame.add(this.currentLevel);
    this.frame.setResizable(false);
    this.frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.frame.setVisible(true);

}
}

itemObject的代码:

public class ItemObject {

public int x = 0;
public int y = 0;

public String image = "";

public void setImage(String image) {
    this.image = image;
}

}

【问题讨论】:

  • this.currentMap 是否在某处初始化?

标签: java arrays


【解决方案1】:

你还没有初始化ItemObject[][] currentMapanywhere。在 start() 方法中可以添加这行代码:

currentMap = new ItemObject[*# of rows*][*# of columns*];

在向数组添加任何值之前,必须先初始化对象。

【讨论】:

  • 好的,谢谢我补充说。你能帮我在 JFrame 上展示这个吗?什么东西最好用?
  • @Tony ItemObject 只是一张图片吗?如果是这样,我会看看这个:stackoverflow.com/questions/18871150/…
【解决方案2】:

好吧,伙计们,我修好了。现在我想要它在 JFrame 上。我想不通... W = Wall 和 "" = emptytile。现在的代码如下所示:

等级:

public class Level extends JComponent {

final int ROWS = 10;
final int COLUMNS = 15;

public String[][] map = new String[ROWS][COLUMNS];
private ItemObject[][] loadedMap;

public void Load(HashMap<String, ItemObject> objects) {
    int rowsCount = 0;

    for (String[] row : this.map) {
        int colsCount = 0;

        for (String col : row) {
            this.loadedMap[rowsCount][colsCount] = objects.get(col);
            colsCount += 1;
        }

        rowsCount += 1;
    }
}

关卡生成器:

 public final class LevelGenerator extends JFrame {

private HashMap<String, ItemObject> objects = new HashMap<>();
private int HEIGHT = 500;
private int WIDTH = 750;

public JFrame frame = null;
public Level currentLevel = null;

private final List<Level> levels
        = new ArrayList<Level>() {
            {
                add(new Level001());
                add(new Level002());
                add(new Level003());
            }
        };

public LevelGenerator(JFrame frame) {
    // Vul de frame
    this.frame = frame;
    // Vul de objecten lijst
    objects.put("B", new Bazooka());
    objects.put("", new EmptyTile());
    objects.put("W", new Wall());
    this.currentLevel = levels.get(0);

    this.Load();
}

/// Laad de map in
public void Load() {
    this.currentLevel.Load(objects);

    this.Start();
}

public void Start() {
    if (this.frame != null) {
        this.frame.setSize(this.WIDTH, this.HEIGHT);
        this.frame.setLayout(new GridLayout(15, 10));
        this.frame.add(this.currentLevel);
        this.frame.setResizable(false);
        this.frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.frame.setVisible(true);
    }
}

level001:

public class Level001 extends Level {

public Level001() {
    String[][] tiles = {    { "W", "W", "W", "W", "W", "W", "W", "W", "W", "W", "W", "W", "W", "W", "W" },
                                { "W", "", "", "", "", "", "", "", "", "", "", "", "", "", "W" },
                                { "W", "", "", "", "", "", "", "", "", "", "", "", "", "", "W" },
                                { "W", "", "", "", "", "", "", "", "", "", "", "", "", "", "W" },
                                { "W", "", "", "", "", "", "", "", "", "", "", "", "", "", "W" },
                                { "W", "", "", "", "", "", "", "", "", "", "", "", "", "", "W" },
                                { "W", "", "", "", "", "", "", "", "", "", "", "", "", "", "W" },
                                { "W", "", "", "", "", "", "", "", "", "", "", "", "", "", "W" },
                                { "W", "", "", "", "", "", "", "", "", "", "", "", "", "", "W" },
                                { "W", "W", "W", "W", "W", "W", "W", "W", "W", "W", "W", "W", "W", "W", "W" }
                            };

    super.map = tiles;
}

项目对象:

public class ItemObject {

public int x = 0;
public int y = 0;

private String image = "";
private ImageIcon imageIcon;

public ImageIcon getImageIcon() {
    return new ImageIcon(this.image);
}

public void setImage(String url) {
   this.image = "/com/maze/images/" + url;
}  

emptyTile 对象;

public class EmptyTile extends ItemObject {

public EmptyTile(){

    this.setImage("Empty.png");

}

墙壁对象:

public class Wall extends ItemObject {

public Wall() {

    this.setImage("Wall.png");

}

【讨论】:

  • 在我的回答中查看我评论的链接。这应该会有所帮助
  • 你不能把它当成一个聊天板来给这个家伙一个分数——他指出了你麻烦的开始;我们不应该做你的整个软件生产
猜你喜欢
  • 1970-01-01
  • 2017-05-30
  • 1970-01-01
  • 2015-05-27
  • 2019-12-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多