2016-05-22 12 views
1

こんにちはpeps(壊れた英語)、オブジェクトを2D配列に格納してJFrameに表示します

私は2D迷路ゲームで作業しています。 2D配列にオブジェクトを格納するには、いくつかの助けが必要です。私はこの1つでNullPointerを取得します:this.currentMap [colsCount] [rowsCount] = objects.get(col);. mainはLevelGeneratorコンストラクタを呼び出します。 itemObjectの

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); 

} 
} 

コード:

public class ItemObject { 

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

public String image = ""; 

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

} 
+0

'this.currentMap'はどこかに初期化されますか? –

答えて

1

あなたはどこにでもItemObject[][] currentMapを初期化していません。あなたはこのコード行を追加することがありstart()方法で:

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

あなたは配列に任意の値を追加する前に、あなたは最初のオブジェクトを初期化する必要があります。

+0

よろしくお願いいたします。これをJFrameに表示する手助けはできますか?使用するのに最適なものは何ですか? – Tony

+0

@Tony ItemObjectは単なる絵ですか?もしそうなら、私はこれを見ます:http://stackoverflow.com/questions/18871150/how-do-i-load-images-in-jframe-javaeclipse – Arman

0

私はそれを修正しました。今私はJFrameでそれをしたい。私はそれを理解することはできません... W =壁と ""空のタイル。コードは次のようになります。

レベル:

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; 
    } 
} 

Levelgenerator:

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; 
} 

ItemObject:

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"); 

} 
+0

私の答えでコメントしたリンクを見てください。それは助けてください – Arman

+0

あなたはチャットボードとしてこれを実行することはできません男はポイントの移動を与える - 彼はあなたのトラブルの初めを指摘した。私たちはあなたのソフトウェア生産全体をすることは想定されていません – gpasch

関連する問題