presario
Legacy Member
Hi,
Zit met een vraagske. Voor men project moet ik doolhof maken met daarin een "held" die naar een "wezen" moet bewegen en verslaan. Het tekenen van de doolhof doe ik op deze manier
Nu, dat zorgt voor geen problemen, het is nl. het bewegen. Ik laat mijn "held" bewegen dmv buttons in men frame. Ik gebruik van elke button zijn actionlistener om methode aan te roepen.
Dus ik controleer of er al dan niet een muur staat, staat er geen dan mag hij bewegen. Ik roep dus telkens de methode "beweeg.." maar het loopt fout bij het bewegen. Naar boven en naar links gaat zonder probleem maar als ik naar onder of naar rechts klik krijg ik een "array out of bounds exeption". Iemand die hier ziet waar de fout zit? Zou er mij een serieus plezier mee doen
!
mvg
Pres.
Zit met een vraagske. Voor men project moet ik doolhof maken met daarin een "held" die naar een "wezen" moet bewegen en verslaan. Het tekenen van de doolhof doe ik op deze manier
Code:
private static final long serialVersionUID = 1L;
private JLabel[][] label = new JLabel[10][10];
public int[][] doolhof = { { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 3, 1, 1, 1, 1, 1, 1, 1, 0 },
{ 0, 0, 0, 0, 0, 0, 1, 3, 1, 0 },
{ 0, 1, 1, 1, 1, 1, 1, 2, 1, 0 },
{ 0, 1, 1, 1, 3, 1, 1, 1, 1, 0 },
{ 0, 1, 1, 1, 0, 0, 0, 0, 0, 0 },
{ 0, 1, 3, 1, 1, 1, 1, 1, 1, 0 },
{ 0, 0, 0, 0, 0, 0, 1, 3, 1, 0 },
{ 0, 1, 1, 1, 1, 1, 1, 1, 1, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 } };
public Doolhofveld() {
GridLayout thisLayout = new GridLayout(10, 10);
thisLayout.setHgap(0);
thisLayout.setVgap(0);
thisLayout.setColumns(10);
thisLayout.setRows(10);
setLayout(thisLayout);
dool(doolhof);
}
public void dool(int b[][]) {
for (int i = 0; i < b.length; i++)
for (int j = 0; j < b[i].length; j++) {
if (b[i][j] == 0) {
label[i][j] = new JLabel();
label[i][j].setIcon(new ImageIcon(getClass()
.getClassLoader().getResource("W.png")));
label[i][j].setBounds(i * 100, j * 100, 250, 500);
add(label[i][j]);
}
if (b[i][j] == 1) {
label[i][j] = new JLabel();
label[i][j].setIcon(new ImageIcon(getClass()
.getClassLoader().getResource("G.png")));
label[i][j].setBounds(i * 100, j * 100, 250, 500);
add(label[i][j]);
}
if (b[i][j] == 2) {
label[i][j] = new JLabel();
label[i][j].setIcon(new ImageIcon(getClass()
.getClassLoader().getResource("P.png")));
label[i][j].setBounds(i * 100, j * 100, 250, 500);
add(label[i][j]);
}
if (b[i][j] == 3) {
label[i][j] = new JLabel();
label[i][j].setIcon(new ImageIcon(getClass()
.getClassLoader().getResource("D.png")));
label[i][j].setBounds(i * 100, j * 100, 250, 500);
add(label[i][j]);
}
}
}
Code:
public boolean boven() {
boolean controle = false;
for (int i = 0; i < doolhof.length; i++)
for (int j = 0; j < doolhof[i].length; j++) {
if (doolhof[i][j] == 2) {
if (doolhof[i - 1][j] == 0) {
return controle = false;
} else {
return controle = true;
}
}
}
return controle;
}
public void beweegBoven() {
if (boven() == true) {
for (int i = 0; i < doolhof.length; i++)
for (int j = 0; j < doolhof[i].length; j++) {
if (doolhof[i][j] == 2) {
if(doolhof[i-1][j]==3)
{
Gevecht g=new Gevecht();
g.setVisible(true);
}
doolhof[i][j] = doolhof[i - 1][j];
doolhof[i - 1][j] = 2;
label[i][j].setIcon(new ImageIcon(getClass()
.getClassLoader().getResource("G.png")));
label[i - 1][j].setIcon(new ImageIcon(getClass()
.getClassLoader().getResource("P.png")));
}
}
}
}
public boolean onder() {
boolean controle = false;
for (int i = 0; i < doolhof.length; i++)
for (int j = 0; j < doolhof[i].length; j++) {
if (doolhof[i][j] == 2) {
if (doolhof[i + 1][j] == 0)
controle = false;
else
controle = true;
}
}
return controle;
}
public void beweegOnder() {
if (onder() == true) {
for (int i = 0; i < doolhof.length; i++)
for (int j = 0; j < doolhof[i].length; j++) {
if (doolhof[i][j] == 2) {
/*if(doolhof[i+1][j]==3)
{
Gevecht g=new Gevecht();
g.setVisible(true);
}*/
doolhof[i][j] = doolhof[i + 1][j];
doolhof[i + 1][j] = 2;
label[i][j].setIcon(new ImageIcon(getClass()
.getClassLoader().getResource("G.png")));
label[i + 1][j].setIcon(new ImageIcon(getClass()
.getClassLoader().getResource("P.png")));
}
}
}
}
public boolean links() {
boolean controle = false;
for (int i = 0; i < doolhof.length; i++)
for (int j = 0; j < doolhof[i].length; j++) {
if (doolhof[i][j] == 2) {
if (doolhof[i][j-1] == 0)
controle = false;
else
controle = true;
}
}
return controle;
}
public void beweegLinks() {
if (links() == true) {
for (int i = 0; i < doolhof.length; i++)
for (int j = 0; j < doolhof[i].length; j++) {
if (doolhof[i][j] == 2) {
if(doolhof[i][j-1]==3)
{
Gevecht g=new Gevecht();
g.setVisible(true);
}
doolhof[i][j] = doolhof[i][j - 1];
doolhof[i][j - 1] = 2;
label[i][j].setIcon(new ImageIcon(getClass()
.getClassLoader().getResource("G.png")));
label[i][j - 1].setIcon(new ImageIcon(getClass()
.getClassLoader().getResource("P.png")));
}
}
}
}
public boolean rechts() {
boolean controle = false;
for (int i = 1; i < doolhof.length; i++)
for (int j = 1; j < doolhof[i].length; j++) {
if (doolhof[i][j] == 2)
{
if (doolhof[i][j + 1] == 0)
controle = false;
else
controle = true;
}
}
return controle;
}
public void beweegRechts() {
if (rechts() == true) {
try
{
for (int i = 1; i <= doolhof.length; i++)
for (int j = 1; j <= doolhof[i].length; j++) {
if (doolhof[i][j] == 2)
{
if(doolhof[i][j+1]==3)
{
Gevecht g=new Gevecht();
g.setVisible(true);
}
doolhof[i][j] = doolhof[i][j + 1];
doolhof[i][j + 1] = 2;
label[i][j].setIcon(new ImageIcon(getClass()
.getClassLoader().getResource("G.png")));
label[i][j + 1].setIcon(new ImageIcon(getClass()
.getClassLoader().getResource("P.png")));
}
}
}
catch(ArrayIndexOutOfBoundsException e)
{
JOptionPane.showMessageDialog(null,e.getMessage());
}
}
}
!mvg
Pres.



.