学校プロジェクトの「マップ」用にグリッドを作成するために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();
}
}
、それは、着色及び検出するためのものですあなたが立っているブロック。しかし、私はすべてが右に押し出されてから、下に押し出される理由について混乱しています。私はブロック作成の数学とインデックスの紹介を何千回も行ってきました。
描画/四角形を塗る前に色を設定しようとしましたか? – MadProgrammer
はい、これは機能しました。とても有難い! –