This is my first question, so I'm still a little confused.
(这是我的第一个问题,所以我仍然有些困惑。)
I am doing an activity for University and I have to make a JFrame Labyrinth using matrix. (我正在为大学做一项活动,我必须使用矩阵制作一个JFrame迷宫。)
I have most of it done, but I just can't implement some "visual movements". (我已经完成了大部分工作,但是我无法执行一些“视觉动作”。)
Sorry for the Portuguese comments, but basically you start at position (x, y) and control the movements using the keyboard arrows. (很抱歉葡萄牙语注释,但基本上您是从(x,y)位置开始并使用键盘箭头控制移动的。)
You can only move through the 0s (X), if you touch a wall (1 / #), it says "parede!", which means wall. (您只能穿过0(X),如果您碰触墙壁(1 /#),则说“ parede!”,即墙壁。)
All I want to do is, when you're in a position, it changes the " X" to " ", but I have no idea how to do it. (我要做的就是,当您处于某个位置时,它将“ X”更改为“”,但是我不知道该怎么做。)
Also, for some reason, when you hit the 2 (or " !"), which is the end, it's not saying the "Você venceu!" (另外,由于某种原因,当您按下2(或“!”)时,也就结束了,这并不是说“Vocêvenceu!”。)
quote, it just says "Parede!", but that's not that important and I can probably make that work, what I need the most are the visual changes when you're in a certain position. (引用,它只是说“ Parede!”,但这并不是那么重要,我可能可以完成这项工作,我最需要的是当您处于特定位置时的视觉变化。)
If you guys could give me the code and explain how it works, I'll really appreciate it.
(如果你们能给我代码并解释其工作原理,我将非常感谢。)
Thank you very much!
(非常感谢你!)
This is the code: (这是代码:)
static int x = 4; // Pontos de
static int y = 0; // Partida
public static void main (String[] args) {
// Criando uma matriz para representar o mapa:
int mapa[][] = {
{1, 1, 1, 1, 0, 1, 1, 1},
{1, 0, 0, 0, 0, 0, 0, 1},
{1, 0, 1, 1, 0, 1, 0, 1},
{1, 0, 1, 1, 1, 0, 0, 1},
{1, 0, 0, 1, 0, 0, 1, 1},
{1, 1, 1, 1, 2, 1, 1, 1}
};
// Matriz para representar os elementos gráficos:
JLabel layout[][] = new JLabel[6][8];
for (int i = 0; i < 6; i++) {
for (int j = 0; j < 8; j++) {
layout[i][j] = new JLabel();
if (mapa[i][j] == 0) {
layout[i][j].setText(" X");
} else if (mapa [i][j] == 1) {
layout[i][j].setText(" #");
} else if (mapa [i][j] == 2) {
layout[i][j].setText(" !");
}
}
}
// Preparando a tela:
JFrame tela = new JFrame();
tela.setLocationRelativeTo(null);
tela.setSize(300, 400);
tela.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Criando um painel:
JPanel painel = new JPanel();
painel.setLayout(new GridLayout(0,8));
for (int i=0; i<6; i++) {
for (int j=0; j<8; j++) {
painel.add(layout[i][j]);
}
}
tela.setContentPane(painel);
tela.setVisible(true);
// Criando uma caixa de texto para o usuário manipular:
JTextField texto = new JTextField();
painel.add(texto);
texto.addKeyListener(new KeyListener() {
@Override
public void keyTyped(KeyEvent e) {
// TODO Auto-generated method stub
}
@Override
public void keyPressed(KeyEvent e) {
// TODO Auto-generated method stub
if (e.getKeyCode() == e.VK_LEFT) {
if (mapa[x - 1][y] != 0) {
System.out.println("Parede!");
} else {
x = x - 1;
}
}
if (e.getKeyCode() == e.VK_RIGHT) {
if (mapa[x + 1][y] != 0) {
System.out.println("Parede!");
} else {
x = x + 1;
}
}
if (e.getKeyCode() == e.VK_UP) {
if (mapa[x][y - 1] != 0) {
System.out.println("Parede!");
} else {
y = y - 1;
}
}
if (e.getKeyCode() == e.VK_DOWN) {
}
if (mapa[x][y + 1] == 1) {
System.out.println("Parede!");
} else if (mapa[x][y + 1] == 2) {
System.out.println("Você venceu!");
} else {
y = y + 1;
}
}
@Override
public void keyReleased(KeyEvent e) {
// TODO Auto-generated method stub
}
});
}
ask by Fábio Saraiva translate from so