Error con array
Publicado por Ana (35 intervenciones) el 05/03/2021 17:30:58
Hola buenas tardes, me sale el siguiente error:
Exception in thread "AWT-EventQueue-0" java.lang.IndexOutOfBoundsException: Index 2 out of bounds for length 1
Cuando ejecuto este codigo:
------------------------------------------------------------------------------------------------------------------------------------------
Si alguien me puede ayudar se lo agradeceria
Exception in thread "AWT-EventQueue-0" java.lang.IndexOutOfBoundsException: Index 2 out of bounds for length 1
Cuando ejecuto este codigo:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
public void menuJugador() {
JLabel cartaNum = new JLabel();
cartaNum.setBounds(420,250,120,120);
ImageIcon imagen = new ImageIcon(miControl.getJugador().cartasEnMano.get(indice)+".jpg");
JLabel btn = new JLabel();
btn.setBounds(270,350,120,180);
JLabel turno = new JLabel("Turno de: "+miControl.getJugador().getNombre());
turno.setBounds(0,0,200,20);
panel.add(turno);
JButton boton = new JButton("Siguiente");
boton.setBounds(420,360,100,30);
JButton boton3 = new JButton("Tirar");
boton3.setBounds(420,410,100,30);
JButton boton4 = new JButton("Agrupar");
boton4.setBounds(420,460,100,30);
btn.setIcon(new ImageIcon(imagen.getImage().getScaledInstance(btn.getWidth(),btn.getHeight(),Image.SCALE_SMOOTH)));
panel.add(btn);
panel.add(boton);
panel.add(boton3);
panel.add(boton4);
panel.add(cartaNum);
boton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (indice < miControl.getJugador().cartasEnMano.size()) {
r=indice;
ImageIcon imagen = new ImageIcon(miControl.getJugador().cartasEnMano.get(indice)+".jpg");
cartaNum.setText("Carta numero: "+ indice);
btn.setIcon(new ImageIcon(imagen.getImage().getScaledInstance(btn.getWidth(),btn.getHeight(),Image.SCALE_SMOOTH)));
indice++;
} else {
indice=0;
r=0;
}
}
} );
boton3.addActionListener(new ActionListener() { //al presionar este boton salta el error
public void actionPerformed(ActionEvent e) {
miControl.tirarCarta(r,miControl.getJugador()); ¿
indice=0;
r=0;
}
});
------------------------------------------------------------------------------------------------------------------------------------------
1
2
3
4
5
6
7
8
public void tirarCarta( int op, Jugador j) {
//op va a ser la carta a tirar y jugador va a ser el jugador que la tira
mesa.add(j.cartasEnMano.get(op)); //mesa es un arrayList y cartas en mano es un arraylist que
esta en la clase Jugador
j.cartasEnMano.remove(j.cartasEnMano.get(op));
}
Si alguien me puede ayudar se lo agradeceria
Valora esta pregunta


0