Problem with sprite

I’m trying to create an enemy every 10 seconds and make it appear on the screen with different moves.
The problem is that the enemy does not appear on the screen.
I tried to create an array list and add and then draw. But I do not know if I’m doing it right.
Here is the class which controls the game:



package projeto1;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.image.BufferedImage;
import java.util.ArrayList;

import javax.swing.JPanel;

public class PainelJogo extends JPanel implements Runnable {

    public static int TELA_LARGURA = 640;
    public static int TELA_ALTURA = 480;
    private Jogador jogador;    
    private ArrayList<Inimigo> inimigo = new ArrayList();
    long tempoAnterior = System.currentTimeMillis();
    float timer;
    int cont = 0;
    private BufferedImage buffer;
    private Graphics grafico;
    private double difTempo = 0; 
    
    public PainelJogo() {
        setPreferredSize(new Dimension(TELA_LARGURA, TELA_ALTURA));
        setFocusable(true);
        requestFocus();

        buffer = new BufferedImage(TELA_LARGURA, TELA_ALTURA,
                BufferedImage.TYPE_4BYTE_ABGR);
        grafico = buffer.getGraphics(); 
        
        jogador = new Jogador();// Create player
        
        //Set initial position to player
        jogador.setX(100);
        jogador.setY(200);
        
        
        

        addKeyListener(new KeyListener() {

            public void keyTyped(KeyEvent e) {}

            public void keyPressed(KeyEvent e) {
                if (e.getKeyCode() == KeyEvent.VK_LEFT)
                    jogador.setLeft(true);
                if (e.getKeyCode() == KeyEvent.VK_RIGHT)
                    jogador.setRight(true);
                if (e.getKeyCode() == KeyEvent.VK_UP)
                    jogador.setUp(true);
                if (e.getKeyCode() == KeyEvent.VK_DOWN)
                    jogador.setDown(true);
            }

            public void keyReleased(KeyEvent e) {
                if (e.getKeyCode() == KeyEvent.VK_LEFT)
                    jogador.setLeft(false);
                if (e.getKeyCode() == KeyEvent.VK_RIGHT)
                    jogador.setRight(false);
                if (e.getKeyCode() == KeyEvent.VK_UP)
                    jogador.setUp(false);
                if (e.getKeyCode() == KeyEvent.VK_DOWN)
                    jogador.setDown(false);
            }
        });

        Thread t = new Thread(this);          
        t.start();                


    }

    public void paint(Graphics g) {
        super.paint(g);
        g.drawImage(buffer, 0, 0, null);
    }
    
    public void run() {
         
        while(true) {
           
            difTempo = ((double) (System.currentTimeMillis() - tempoAnterior)) / 1000f;    
            tempoAnterior = System.currentTimeMillis();
            timer += difTempo;
            
            if(timer > 10000)
            {
                inimigo.add(new Inimigo());
                inimigo.get(cont).desenha(grafico);                            
                inimigo.get(cont).atualiza(difTempo);            
            }
            
            grafico.setColor(Color.WHITE);
            grafico.fillRect(0, 0, TELA_LARGURA, TELA_ALTURA);
            jogador.atualiza(0);
            jogador.desenha(grafico);
            
            cont++;
            this.paintImmediately(0, 0, TELA_LARGURA, TELA_ALTURA);
        }                    
        }                          
    }

Where to start… Ummmmmm.

Well, the good news is, if runnable is implemented properly you will be making new entities. The bad news is that the way you are doing it here, will not retrieve entities properly due to using the runnable class to display entities. I’ll just warp you ahead, creating entities is no different than creating particles. So I’ll share some code that’ll hopefully give you the idea.

Particle Example

Hopefully this code will help you with a lot of issues. Copy and paste the entire thing in your IDE. Best of luck :).

There are two problems in your code.

First: you are not storing the new enemies created in your run method.
Second: You are not iterating over your enemies to draw them in the screen.

Also I recomend you to put some sleep inside the loop, to release the processor for the rest of the OS.

You aren’t using your enemy array list to draw anything. All you are drawing is the enemy a single time when you create it. You need to loop through all your enemies every frame and draw every single one of them.


for(Inimigo ini : inimigo)
      ini.desenha(grafico);