Search code examples
javagraphic

Java Graphic trouble when shooting multiple bullet


So i'm trying to create this java game about aircraft shooting aliens and stuff. The aircraft shoot a bullet every time mouse click. That mean the aircraft can shoot 10 or 20 or more bullets at a time. To demonstrate the bullet movement i tried Thread and Timer but the real problem is if i 1 bullet shot out that mean i created a new Thread(or Timer) and that make the game run very slow. Is there any way i can fix this problem? Here a my code for bullet moving

public class Bullet extends JComponent implements Runnable {

int x;//coordinates
int y;
BufferedImage img = null;
Thread thr;
public Bullet(int a, int b) {
        x = a;
        y = b;
        thr = new Thread(this);
        thr.start();

    }
protected void paintComponent(Graphics g) {
        // TODO Auto-generated method stub
        try {
            img = ImageIO.read(new File("bullet.png"));
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        // g.drawImage(resizeImage(img, 2), x, y, this);

        g.drawImage(Plane.scale(img, 2, img.getWidth(), img.getHeight(), 0.125, 0.125), x, y, this);
        width = img.getWidth() / 8;
        height = img.getHeight() / 8;

        super.paintComponent(g);

    }
public void run() {


        while(true)
        {
            if(y<-50)break;//if the bullet isnt out of the frame yet
            y-=5;//move up
            repaint();
            try {
                Thread.sleep(10);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

    }

}

Solution

  • A bullet should NOT be on its own thread. There are several reasons for this, one of which is the one you mentioned - it is going to make your game very slow.

    Try using one master thread which updates all bullets. You will need an update function in your bullet:

    public class Bullet extends JComponent {
     public void update() {
      if(y<-50)return; //if the bullet isnt out of the frame yet
      y-=5;           //move up
     }
    
     //all your other code for your bullet
    }
    

    Then in your master thread have a list of bullets:

    LinkedList<Bullet> bullets = new LinkedList<>();
    

    In the run method of that thread, you can continuously update ALL bullets:

    public void run() {
        while(true)
        {
            for (Bullet b : bullets) {
                b.update();
            }
            repaint();
            try {
                Thread.sleep(10);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
    

    You will need to have a method in your master thread that lets you add a new bullet:

    public void addBullet(Bullet b) {
        bullets.add(b);
    }
    

    Then you can call that to add a new bullet and the master thread will update that bullet along with all the others.