所以我试图用java制作一个迷宫(没有生成器),但遇到了一个障碍。我现在的代码会做一个迷宫,也会做一个jframe,但是它不会给它上色。。。有没有办法使着色工作??
import java.awt.*;
import javax.swing.*;
import java.lang.*;
public class ayy{
public static void main(String [] args){
JFrame frame = new JFrame("Maze");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setSize(1000,1000);
frame.setVisible(true);
int width = 1;
int height = 1;
int [][] map= {
{0,0,0,0,0,0,0,0,0,0,},
{0,0,0,0,0,0,0,0,0,0,},
{2,1,1,1,0,0,0,0,0,0,},
{0,0,0,1,0,0,0,1,1,2,},
{0,0,0,1,0,0,0,1,0,0,},
{0,0,0,1,0,0,0,1,0,0,},
{0,0,0,1,1,1,1,1,0,0,},
{0,0,0,0,0,0,0,0,0,0,},
{0,0,0,0,0,0,0,0,0,0,},
{0,0,0,0,0,0,0,0,0,0,}
};
for(int i=0;i<map.length;i++){
for(int j=0;j<map.length;j++){
switch(map[i][j]){
case 0:
class rectangle{
public void paint(Graphics g){
g.drawRect(1,1,1,1);
g.setColor(Color.red);
}
}
break;
case 1:
class rectangle2{
public void paint(Graphics g){
g.drawRect(1,1,1,1);
g.setColor(Color.yellow);
}
} break;
case 2:
class rectangle3{
public void paint(Graphics g){
g.drawRect(1,1,1,1);
g.setColor(Color.blue);
}
} break;
}
}
}
}
}
任何帮助都可以,谢谢!
1条答案
按热度按时间ee7vknir1#
1-)不要在switch case上创建类,这不是一个好的做法。
2-)如果类没有继承jcomponent,那么它将无法重写paint或paintcomponent方法,因为它没有这些方法。
3-)大写类名称的第一个字母,并使用有意义的名称。
4-)按以下方式修改代码: