1

I'm currently making a game but for some reason when some Graphics2D Components are not moving and the program ends up crashing. Is there some interference between the KeyListener and the moving? Or maybe because I've repainted all the the panel on move?

I've commented what each part of the code does.

Here is the code: `

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;

public class Interface extends JPanel implements ActionListener, KeyListener{

Timer timer;

Player p;
Alien[] a; 

ImageIcon img, msl, al;
Image i, ms, ali;
Graphics2D g2d;

boolean allowUp = true, allowDown = true, allowAlienMove = false, firstDraw = true;
boolean[] allowAlien;
int mx, my = 0;
static int[] ay, ax, olday, oldax;

public Interface(){

    setFocusable(true);
    setBackground(Color.BLACK);
    setDoubleBuffered(true);

    addKeyListener(this);

    img = new ImageIcon(this.getClass().getResource("player.jpg"));
    al = new ImageIcon(this.getClass().getResource("alien.jpg"));

    p = new Player(this.getWidth()/6, this.getHeight()/6,10,this.getHeight()/2,img);
    timer = new Timer(1000, this);

    allowAlien = new boolean[5];
    a = new Alien[5];
    ay = new int[5];
    ax = new int[5];
    olday = new int[5];
    oldax = new int[5];

    for(int i = 0; i < 5; i++){

        ay[i] = -1;
        olday[i] = -1;

        ax[i] = -1;
        oldax[i] = -1;

    }

    for(int i = 0; i < allowAlien.length; i++){

        allowAlien[i] = false;

    }

    spawnAliens();          


}

//SPAWN ALIENS

public void spawnAliens(){       

    int[] area = new int[5];
    area[0] = 70;
    area[1] = 170;
    area[2] = 270;
    area[3] = 370;
    area[4] = 445;

    for(int j = 0; j < ax.length; j++){

        ax[j] = 700;
        oldax[j] = ax[j];

    }

    for(int i =0; i < a.length; i++){

        if(i > 0){
            ay[i] = ((int)(Math.random()*area[i] + area[i-1]));
        }else{
            ay[i] = ((int)(Math.random()*area[i] + 5));
        }

        olday[i] = ay[i];


        //THIS COMPONENT NOT MOVING
        a[i] = new Alien(this.getWidth()/6, this.getHeight()/6,ax[i],ay[i],img);
        allowAlien[i] = true;

       repaint();

    }

    allowAlienMove = true;



}

//THIS IS THE DRAWING PART 

public void paint(Graphics g){
    super.paint(g);

    i = img.getImage();  
    ms = msl.getImage();
    ali = al.getImage();

    g2d = (Graphics2D)g;
    g2d.drawImage(i, p.getX(), p.getY(), null);
    if(allowMissle){    
        g2d.drawImage(ms, m.getX(), m.getY(), null);
    }

    for(int i = 0; i < a.length; i++){
        if(allowAlien[i]){        
            g2d.drawImage(ali, ax[i], ay[i], null);


        }
    }


    if(firstDraw){
        timer.start();
    }

    firstDraw = false;
}

//THIS IS THE MOVEMENT PART

public void actionPerformed(ActionEvent e){


    while(ax[0] > 10){

        ax[0] += 10;
        repaint();
    }


}

//KEY LISTENER INTERFACE METHODS

public void keyReleased(KeyEvent e){



}
public void keyPressed(KeyEvent e){

    int key = e.getKeyCode();

    if (key == KeyEvent.VK_W) {

        if(p.getY() <= 5){

            allowUp = false;

        }else{

            allowUp = true;

        }

        if(allowUp){        
            p.move(0, -5);
            my = p.getY();

            repaint();
        }
    }else if (key == KeyEvent.VK_S) {

        if(p.getY() >= 410 - 5){

            allowDown = false;

        }else{

            allowDown = true;

        }
        if(allowDown){
            p.move(0, 5);
            my = p.getY();
            repaint();
        }
    }

}
public void keyTyped(KeyEvent e){


}
}

Thanks in advance :)

`

mKorbel
  • 109,525
  • 20
  • 134
  • 319
beur_x
  • 169
  • 3
  • 11
  • Please post the Alien and Player classes – Aubin Apr 13 '13 at 16:02
  • Custom painting should be done by overriding the `paintComponent()` method, not the paint() method. Don't use a KeyListener. Instead use `Key Bindings`. Search the forum this recommendation is made daily. – camickr Apr 13 '13 at 16:04

1 Answers1

1

Because you have no pauses in there that means everything will happen instantaneously which will cause it to crash. Also you started a timer but never used it is there something you were trying to achieve with this?

phcoding
  • 608
  • 4
  • 16
  • I started the Timer in the paint method. – beur_x Apr 13 '13 at 16:01
  • Yes but it doesn't do anything if you just start it. You need to use it in some way otherwise it does nothing. – phcoding Apr 13 '13 at 16:05
  • Don't ever start a Timer in the paint() method. – camickr Apr 13 '13 at 16:05
  • What @camickr said is absolutely correct. You need to use a timer with a Listener. Here is a good example of how to implement this: http://stackoverflow.com/questions/6168179/how-do-i-make-this-java-for-loop-pause-for-1-2-a-second-between-each-iteration – phcoding Apr 13 '13 at 16:09