Sempre que saio do alcance da matriz ele dá esse erro, ja tentei criar condições para quando tentar sair o programa parar, porém não funcionou
Segue código:
´´´
/*Dupla: Giovanni Ota
*Ra: 819148633
*SIN1BN-MCA
*Vitor Vendramini
*
*SIN1BN-MCA
*/
package labirinto;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class Lab {
public static void main(String[] args) {
int mapa[][] = {
{1,1,1,1,1,1,1,1,1},
{1,1,1,0,0,0,0,1,1},
{1,1,1,0,1,1,1,0,1},
{1,0,0,2,0,0,0,0,1},
{1,0,1,0,1,1,1,1,1},
{0,0,1,0,0,0,0,0,1},
{1,1,1,1,1,1,1,1,1}
};
JFrame tela = new JFrame();
tela.setTitle("Labirinto - Giovanni Ota, Vitor Vendramini - SIN1BN-MCA");
tela.setSize(500,500);
tela.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel painel = new JPanel();
painel.setLayout(new FlowLayout());
painel.setBackground(new Color(254,200,60));
JLabel rotulos [][] = new JLabel [7][9];
for(int i = 0; i < 7; i++) {
for (int j = 0; j < 9; j++) {
rotulos[i][j] = new JLabel();
if (mapa[i][j] == 1) {
rotulos[i][j].setText("X ");
}
if (rotulos[i][j].getText() == "X ") {
rotulos[i][j].setForeground(new Color(211, 25, 28));
}
if (mapa[i][j] == 0) {
rotulos[i][j].setText(" ");
}
if (mapa[i][j] == 2) {
rotulos[i][j].setText("H ");
}
if (rotulos[i][j].getText() == "H ") {
rotulos[i][j].setForeground(new Color(51, 51, 51));
}
}
}
for(int i = 0; i < 7; i++) {
for (int j = 0; j < 9; j++) {
rotulos[i][j].setFont(new Font("Arial", Font.BOLD, 50));
painel.add(rotulos[i][j]);
}
}
JTextField caixa = new JTextField();
painel.add(caixa);
caixa.setSize(10,20);
caixa.addKeyListener(new KeyListener() {
@Override
public void keyTyped(KeyEvent e) {
// TODO Auto-generated method stub
}
@Override
public void keyReleased(KeyEvent e) {
// TODO Auto-generated method stub
}
@Override
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_LEFT) {
for(int i = 0; i < 7; i++) {
for(int j = 0; j < 9; j++) {
if (rotulos[i][j].getText() == "H " && rotulos[i][j - 1].getText() != "X ") {
rotulos[i][j].setText(" ");
rotulos[i][j - 1].setText("H ");
}
}
}
}
if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
for(int i = 0; i < 7; i++) {
for(int j = 0; j < 9; j++) {
if (rotulos[i][j].getText() == "H " && rotulos[i][j + 1].getText() != "X ") {
rotulos[i][j].setText(" ");
rotulos[i][j + 1].setText("H ");
break;
}
}
}
}
if (e.getKeyCode() == KeyEvent.VK_UP) {
for(int i = 0; i < 7; i++) {
for(int j = 0; j < 9; j++) {
if (rotulos[i][j].getText() == "H " && rotulos[i - 1][j].getText() != "X ") {
rotulos[i][j].setText(" ");
rotulos[i - 1][j].setText("H ");
}
}
}
}
if (e.getKeyCode() == KeyEvent.VK_DOWN) {
for(int i = 0; i < 7; i++) {
for(int j = 0; j < 9; j++) {
if (rotulos[i][j].getText() == "H " && rotulos[i + 1][j].getText() != "X ") {
rotulos[i][j].setText(" ");
rotulos[i += 1][j].setText("H ");
}
}
}
}
if (rotulos[5][7].getText() == "H " || rotulos[2][7].getText() == "H " || rotulos[1][6].getText() == "H ") {
Erro error = new Erro();
error.setVisible(true);
}
if (rotulos[5][0].getText() == "H ") {
Certo out = new Certo();
out.setVisible(true);
}
}
});
tela.setContentPane(painel);
tela.setVisible(true);
}
}
´´´