我尝试使用2D数组为JFrame着色以创建网格,以及#34; map"为学校项目。 问题是,在框架的(0,0)位置,出现一个随机的灰色框,将所有内容推向右侧。它甚至将最后一个块推到了NEXT行的第一个位置,这让我非常困惑。
我考虑到数组以索引" 0"开头的事实,并且我想知道为什么它没有在(0,0)位置绘制黑盒子。
以下是我的代码中的两个相关类:
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();
}
}
为2D阵列的暴行道歉,即用于着色和检测你站在哪个块上。但是我很困惑为什么一切都被推到了右边,然后再被推倒了。我已经对块创建和索引引用的数学进行了数千次的研究。
答案 0 :(得分:1)
我认为你应该把
g2.draw(block);
g2.fill(block);
在你的setcolors之后,如果我没弄错的话。
对于第一个元素,您正在绘制一个框,然后设置颜色。如果有帮助,请告诉我。如果你有主要的方法代码,我愿意进一步帮助。