animation

ok here is my problem. i have the following code that draws a character and a level. pressing right or left on the arrow keys will move the character forward or backward. up and down raise or lower the angle to shoot at. holding space will increase power and when space is released the character shoots a bullet. however when the bullet hits the ground i want it to disappear and i want to animate an explosion. so basically what is a good way to do multiple animations? in particular whats a good way to make animations appear and disappear?

import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;

public class Test extends Applet implements Runnable, KeyListener {
    int x = 0;
    int y = 500;
    int z = 0;
    int q = 44;
    int d=0, p =0;
    
    double xfloat = 0;
    double yfloat = 0;
    double vx = 0,vy = 0;
    double angle = 0;
    double power = 0;
    final double gravity = 0.3;
    double pi = 2*Math.PI;
    
    Thread th = new Thread(this);
    BufferedImage offscreen = new BufferedImage(1000,600,BufferedImage.TYPE_INT_RGB);
    Graphics2D bufferGraphics = offscreen.createGraphics();
    boolean keys[];
    boolean paused;
    Image sprite[] = new Image[30];

    
    public void init() {
        setBackground(Color.white);
        addKeyListener(this);
        keys = new boolean[65536];
        th.start();
        Characters create = new Characters();
        for (int i = 0; i < 30; i++)
        {
            sprite[i] = create.createBSprite(q);
            q--;
        }
        
        paused = false;
    }

    public void start() {
        paused = false;
    }
    
    public void stop() {
        paused = true;
    }

    public void keyTyped(KeyEvent e) {
    }

    public void keyReleased(KeyEvent e) {
        keys[e.getKeyCode()] = false;
    }

    public void keyPressed(KeyEvent e) {
        keys[e.getKeyCode()] = true;
    }

    //override paint and update to stop any system rendering
  
    public void paint(Graphics g) {}
    public void update(Graphics g) {}

    public void run() {
        while (true) {
            //game loop
            if(!paused) {
                Graphics2D g = (Graphics2D)getGraphics();
                bufferGraphics.setColor(Color.WHITE);
                bufferGraphics.fillRect(0, 0, getWidth(), getHeight());
                //game logic
                if(xfloat != 0 && yfloat != 0)
                {
                        xfloat += vx;
                        yfloat += vy;
                        vy += 0.3;
                }
                
                
                if(keys[KeyEvent.VK_RIGHT] && x < 374) {
                            x++;
                } 
                if(keys[KeyEvent.VK_LEFT] && x > -3) {
                    x--;
                }
                
                if(keys[KeyEvent.VK_UP] && z < 15)  {
                    z++;
                    angle += 5;
                }
                
                if(keys[KeyEvent.VK_DOWN] && z > 0) {
                    z--;
                    angle -= 5;
                }
               
                
                if(keys[KeyEvent.VK_SPACE] ) {
                    d = 10;
                    p = 10;
                    xfloat = x+63;
                    yfloat = y+20;
                    power += 2;
                    vx = (power / 10.0) * (float)Math.cos((pi * (double)angle) / 360.0);
                    vy = -((power / 10.0) * (float)Math.sin((pi * (double)angle) / 360.0));
                    
                } 
                
                
                if (xfloat > 450 && xfloat < 550 && yfloat > 280 && yfloat < 290)
                    vy = -((power / 15.0) * (float)Math.sin((pi * (double)angle) / 360.0));
                
                if(yfloat > 564 || xfloat > 1000)
                {
                    power = 0;
                    d = 0;
                    p = 0;
                    xfloat = 0;
                    yfloat = 0;
                    
                }
                
                    
                    
                
                
                //rendering
               
                bufferGraphics.setColor(Color.red);
                bufferGraphics.fillRect(0,300,30,300);
                bufferGraphics.fillRect(450,300,100,300);
                bufferGraphics.setColor(Color.green);
                bufferGraphics.fillRect(0,565,1000,35);
                bufferGraphics.setColor(Color.magenta);
                bufferGraphics.fillOval((int)xfloat,(int)yfloat,d,p);
                bufferGraphics.setColor(Color.blue);
                bufferGraphics.fillRect(0,0,(int)power,10);
                bufferGraphics.drawImage(sprite[z], x, y,this); 
                g.drawImage(offscreen,0,0,this);
                g.dispose();
                
                
                

                
                try {
                    Thread.sleep(20);
                }
                catch (InterruptedException ex) {
                    ex.printStackTrace();
                }
            }
            else {
                try {
                    Thread.yield();
                    Thread.sleep(100);
                }
                catch (InterruptedException ex) {
                    ex.printStackTrace();
                }
            }
        }
        
    }
    
    

}

class Characters
{
 BufferedImage createBSprite(int x)
        {
            BufferedImage sprite = new BufferedImage(100,100,BufferedImage.TYPE_INT_ARGB);
            Graphics2D g2D = sprite.createGraphics();
            g2D.setComposite(AlphaComposite.getInstance(AlphaComposite.CLEAR, 0.0f));
            g2D.fillRect(0,0,100,100);
            g2D.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 1.0f));
            g2D.setColor(Color.black);
            //head
            g2D.fillOval(42,20,17,16);
            //neck
            g2D.fillRect(48,35,7,4);
            //body
            g2D.fillArc(42,36,17,16,0,180);
            g2D.fillRect(43,44,16,10);
            //left leg and foot
            g2D.fillRect(44,54,3,8);
            g2D.fillRect(44,62,6,3);
            //right leg and foot
            g2D.fillRect(55,54,3,8);
            g2D.fillRect(55,62,6,3);
            //right arm
            //g2D.fillRect(58,44,10,3);
            //g2D.fillOval(64,41,12,8);
            g2D.drawLine(58,44,68,x);
            g2D.drawLine(58,45,68,x+1);
            g2D.drawLine(58,46,68,x+2);
            //left arm
            g2D.fillRect(36,44,8,3);
            g2D.fillOval(32,43,5,5);
            return sprite;
        
        }
        
       
    }



Typically you want to do something like having a list of objects/animations in your game, and then give them their own run() method. The main game loop calls this run method, which then through the use of local variables (each Animation is its own instantiation), the animation updates itself. Then when it’s finished it can tell the main game to delete it by removing it from the list.

I will usually make Explosion and Bullet different objects, and when a Bullet impacts with something it instantiates an Explosion. The Explosion applies force/damage to the stuff around it, meanwhile playing its animation. And as I said before, it deletes itself when it’s done.

Really what you want to think about is the encapsulation of your animations and objects, and give them their own respective timers. The main class should not handle that sort of thing.

first of all sorry for posting this twice. i just figured it would go better in this section of the forum. anyways that makes more sense than lots of threads but i dont quite understand how to do this. could you post a short example program? or do you know of any examples of this on the internet?