2017-05-16 8 views
1

学校プロジェクトの「マップ」用にグリッドを作成するために2D配列でJFrameの色を付けようとしています。 問題は、フレームの(0,0)の位置に、ランダムな灰色のボックスが表示され、すべてを右に押すことです。それは最後のブロックをNEXT行の最初の位置にプッシュさえします。これは私を非常に混乱させます。JFrameでの矩形の作成がわからないために右に移動

配列はインデックス「0」から始まり、なぜ(0,0)の位置に黒いボックスを描画していないのか不思議です。ここ

は、自分のコードで2つの関連のクラスである:2次元アレイである残虐行為のために謝罪

public class Floor1Components extends JComponent { 
    public void paintComponent(Graphics g) { 
    Graphics2D g2 = (Graphics2D) g; 

    for (int y = 0; y < Map.floor1Schematics.length; y++) { //for every row 

     for(int x = 0; x < Map.floor1Schematics[0].length; x++) { //do something for every column 
      Rectangle block = new Rectangle((x*50), (y*50), 50, 50); 
      g2.draw(block); 
      g2.fill(block); 
      if(Map.floor1Schematics[y][x].equals("wl")) { 
       g2.setColor(Color.BLACK); 
      } 
      if(Map.floor1Schematics[y][x].equals("gr")) { 
       g2.setColor(Color.GREEN); 
      } 
      if(Map.floor1Schematics[y][x].equals("end")) { 
       g2.setColor(Color.RED); 
      } 
      if(Map.floor1Schematics[y][x].equals("ch")) { 
       g2.setColor(Color.YELLOW); 
      } 
      if(Map.floor1Schematics[y][x].equals("pv")) { 
       g2.setColor(Color.GRAY); 
      } 
     } 

    } 
} 

}

public class Map { 

public static boolean floor1 = true; 
public static boolean floor2 = false; 
public static boolean floor3 = false; 

public static JFrame frame = new JFrame(); 

public static String floor1Schematics[][] = 
    { 
{"wl","wl","wl","wl","wl","wl","wl","wl","wl","wl","wl","wl","wl","wl","wl","wl","wl","wl","wl","wl","wl","wl","wl","wl","wl","wl","wl","wl","wl","wl"}, 
{"wl","gr","gr","gr","wl","gr","gr","gr","gr","gr","gr","gr","gr","gr","pv","pv","gr","gr","gr","gr","gr","gr","gr","gr","gr","wl","gr","gr","gr","wl"}, 
{"wl","gr","ch","gr","wl","gr","gr","gr","gr","gr","gr","gr","gr","gr","pv","pv","gr","gr","gr","gr","gr","gr","gr","gr","gr","wl","gr","ch","gr","wl"}, 
{"wl","gr","gr","gr","gr","gr","gr","gr","gr","gr","gr","gr","gr","gr","pv","pv","gr","gr","gr","gr","gr","gr","gr","gr","gr","gr","gr","gr","gr","wl"}, 
{"wl","wl","wl","gr","gr","gr","gr","gr","gr","gr","gr","gr","gr","gr","pv","pv","gr","gr","gr","gr","gr","gr","gr","gr","gr","gr","gr","wl","wl","wl"}, 
{"wl","gr","gr","gr","gr","gr","gr","gr","gr","gr","gr","gr","gr","gr","pv","pv","gr","gr","gr","gr","gr","gr","gr","gr","gr","gr","gr","gr","gr","wl"}, 
{"wl","gr","gr","gr","gr","gr","gr","gr","gr","gr","gr","gr","gr","gr","pv","pv","gr","gr","gr","gr","gr","gr","gr","gr","gr","gr","gr","gr","gr","wl"}, 
{"wl","gr","gr","gr","gr","gr","gr","gr","gr","gr","gr","gr","gr","gr","pv","pv","gr","gr","gr","gr","gr","gr","gr","gr","gr","gr","gr","gr","gr","wl"}, 
{"wl","gr","gr","gr","gr","gr","gr","gr","gr","gr","gr","gr","gr","gr","pv","pv","gr","gr","gr","gr","gr","gr","gr","gr","gr","gr","gr","gr","gr","wl"}, 
{"wl","gr","gr","gr","gr","gr","gr","gr","gr","gr","gr","gr","gr","gr","pv","pv","gr","gr","gr","gr","gr","gr","gr","gr","gr","gr","gr","gr","gr","wl"}, 
{"wl","gr","gr","gr","gr","gr","gr","gr","gr","gr","gr","gr","gr","gr","pv","pv","gr","gr","gr","gr","gr","gr","gr","gr","gr","gr","gr","gr","gr","wl"}, 
{"wl","gr","gr","gr","gr","gr","gr","gr","gr","gr","gr","gr","gr","gr","pv","pv","gr","gr","gr","gr","gr","gr","gr","gr","gr","gr","gr","gr","gr","wl"}, 
{"wl","gr","gr","gr","gr","gr","gr","gr","gr","gr","gr","gr","pv","pv","pv","pv","pv","pv","gr","gr","gr","gr","gr","gr","gr","gr","gr","gr","gr","wl"}, 
{"wl","gr","gr","gr","gr","gr","gr","gr","gr","gr","gr","gr","pv","pv","pv","pv","pv","pv","gr","gr","gr","gr","gr","gr","gr","gr","gr","gr","gr","wl"}, 
{"wl","pv","pv","pv","pv","pv","pv","pv","pv","pv","pv","pv","pv","pv","pv","pv","pv","pv","pv","pv","pv","pv","pv","pv","pv","pv","pv","pv","pv","wl"}, 
{"wl","pv","pv","pv","pv","pv","pv","pv","pv","pv","pv","pv","pv","pv","pv","pv","pv","pv","pv","pv","pv","pv","pv","pv","pv","pv","pv","pv","pv","wl"}, 
{"wl","gr","gr","gr","gr","gr","gr","gr","gr","gr","gr","gr","pv","pv","pv","pv","pv","pv","gr","gr","gr","gr","gr","gr","gr","gr","gr","gr","gr","wl"}, 
{"wl","gr","gr","gr","gr","gr","gr","gr","gr","gr","gr","gr","pv","pv","pv","pv","pv","pv","gr","gr","gr","gr","gr","gr","gr","gr","gr","gr","gr","wl"}, 
{"wl","gr","gr","gr","gr","gr","gr","gr","gr","gr","gr","gr","gr","gr","pv","pv","gr","gr","gr","gr","gr","gr","gr","gr","gr","gr","gr","gr","gr","wl"}, 
{"wl","gr","gr","gr","gr","gr","gr","gr","gr","gr","gr","gr","gr","gr","pv","pv","gr","gr","gr","gr","gr","gr","gr","gr","gr","gr","gr","gr","gr","wl"}, 
{"wl","gr","gr","gr","gr","gr","gr","gr","gr","gr","gr","gr","gr","gr","pv","pv","gr","gr","gr","gr","gr","gr","gr","gr","gr","gr","gr","gr","gr","wl"}, 
{"wl","gr","gr","gr","gr","gr","gr","gr","gr","gr","gr","gr","gr","gr","pv","pv","gr","gr","gr","gr","gr","gr","gr","gr","gr","gr","gr","gr","gr","wl"}, 
{"wl","gr","gr","gr","gr","gr","gr","gr","gr","gr","gr","gr","gr","gr","pv","pv","gr","gr","gr","gr","gr","gr","gr","gr","gr","gr","gr","gr","gr","wl"}, 
{"wl","gr","gr","gr","gr","gr","gr","gr","gr","gr","gr","gr","gr","gr","pv","pv","gr","gr","gr","gr","gr","gr","gr","gr","gr","gr","gr","gr","gr","wl"}, 
{"wl","gr","gr","gr","gr","gr","gr","gr","gr","gr","gr","gr","gr","gr","pv","pv","gr","gr","gr","gr","gr","gr","gr","gr","gr","gr","gr","gr","gr","wl"}, 
{"wl","wl","wl","gr","gr","gr","gr","gr","gr","gr","gr","gr","gr","gr","pv","pv","gr","gr","gr","gr","gr","gr","gr","gr","gr","gr","gr","wl","wl","wl"}, 
{"wl","gr","gr","gr","gr","gr","gr","gr","gr","gr","gr","gr","gr","gr","pv","pv","gr","gr","gr","gr","gr","gr","gr","gr","gr","gr","gr","gr","gr","wl"}, 
{"wl","gr","ch","gr","wl","gr","gr","gr","gr","gr","gr","gr","gr","gr","pv","pv","gr","gr","gr","gr","gr","gr","gr","gr","gr","wl","gr","ch","gr","wl"}, 
{"wl","gr","gr","gr","wl","gr","gr","gr","gr","gr","gr","gr","gr","gr","pv","pv","gr","gr","gr","gr","gr","gr","gr","gr","gr","wl","gr","gr","gr","wl"}, 
{"wl","wl","wl","wl","wl","wl","wl","wl","wl","wl","wl","wl","wl","wl","wl","wl","wl","wl","wl","wl","wl","wl","wl","wl","wl","wl","wl","wl","wl","wl"}, 

    }; 

public static void createFrame() { 

    frame.setSize(1600,1600); 
    frame.setTitle("Frame"); 
    frame.setResizable(false); 
    //frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE); 
    //PlayerModel player = new PlayerModel(); 
    //frame.add(player); 
    //frame.setVisible(true); 
    if (floor1 && !floor2 && !floor3) { 
     Floor1Components floor1 = new Floor1Components(); 
     frame.add(floor1); 
     frame.setVisible(true); 
    } 
} 

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

}

、それは、着色及び検出するためのものですあなたが立っているブロック。しかし、私はすべてが右に押し出されてから、下に押し出される理由について混乱しています。私はブロック作成の数学とインデックスの紹介を何千回も行ってきました。

+0

描画/四角形を塗る前に色を設定しようとしましたか? – MadProgrammer

+0

はい、これは機能しました。とても有難い! –

答えて

1

私は私が間違っていない場合は、あなたのsetcolors後

 g2.draw(block); 
     g2.fill(block); 

を置くべきだと思います。

最初の要素では、ボックスを描画してから、後で色を設定します。それが役に立ったら教えてください。メインのメソッドコードをお持ちの場合、私はさらに助けてくれるでしょう。

+0

ああ私の良さ。ありがとうございました。私はこのバグを1週間作業していました。それを中心にハードコーディングされていても、私にはより多くの問題が発生しました。なぜこの作品が私に説明できますか?私はこの同じ種類の間違いをもう一度したくありません。どうもありがとうございます。 –

+0

私が言うことができる限り、そのようなもの(現実世界のアイテムを使って、exのために):あなたはデフォルトで灰色の鉛筆を拾う。あなたが何かを描きたいなら、それは灰色で描かれます。あなたは描画したい右の色の鉛筆を選択し、必要なものを描画する必要があります。それがなぜ相殺されたかはあまり確かではありませんが、実際のコードでもう少しデバッグする必要があります。 また、おそらくg2.drawは必要なく、g2.fillだけです。 – user2980932

+0

forループが私が見ることからどのように働いていたかとちょうど相殺されたように見えます。 – user2980932

関連する問題