Package

Source Code of AppletGame

// Decompiled by Jad v1.5.8e. Copyright 2001 Pavel Kouznetsov.
// Jad home page: http://www.geocities.com/kpdus/jad.html
// Decompiler options: packimports(3)
// Source File Name:   AppletGame.java

import java.applet.Applet;
import java.applet.AudioClip;
import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.Hashtable;
import java.util.Vector;

public abstract class AppletGame extends Applet
    implements Runnable
{

    public AppletGame()
    {
        teclas_pulsadas = new Hashtable();
        anima = null;
        retardo = 50L;
        sprites = new Vector();
        lista_imagenes = new Vector();
        lista_sonidos = new Vector();
        cargando = true;
    }

    public void init()
    {
        tracker = new MediaTracker(this);
        initComponents();
    }

    public void start()
    {
        if(anima == null)
        {
            anima = new Thread(this);
            anima.start();
        }
    }

    public void stop()
    {
        if(anima != null)
        {
            anima.stop();
            anima = null;
        }
    }

    private void initComponents()
    {
        setLayout(new BorderLayout(400, 300));
        setBackground(Color.white);
        addKeyListener(new KeyAdapter() {

            public void keyPressed(KeyEvent evt)
            {
                formKeyPressed(evt);
            }

            public void keyReleased(KeyEvent evt)
            {
                formKeyReleased(evt);
            }

        });
    }

    private void formKeyReleased(KeyEvent evt)
    {
        teclas_pulsadas.remove(evt.getKeyCode() + "");
    }

    private void formKeyPressed(KeyEvent evt)
    {
        teclas_pulsadas.put(evt.getKeyCode() + "", new Integer(evt.getKeyCode()));
    }

    public void paint(Graphics g)
    {
        int ancho = getSize().width;
        int alto = getSize().height;
        if(gBuffer == null)
        {
            imag = createImage(ancho, alto);
            gBuffer = imag.getGraphics();
        }
        gBuffer.setColor(getBackground());
        gBuffer.fillRect(0, 0, ancho, alto);
        if(cargando)
        {
            pantallaDeCarga(gBuffer);
        } else
        {
            gBuffer.setColor(Color.green);
            if(fondo != null)
                gBuffer.drawImage(fondo, 0, 0, this);
            Vector sprites_tmp = (Vector)sprites.clone();
            for(int a = 0; a < sprites_tmp.size(); a++)
            {
                Sprite sprite = (Sprite)sprites_tmp.elementAt(a);
                if(sprite.getEsta_vivo())
                {
                    sprite.movimiento();
                    if(!debug)
                        gBuffer.drawImage(sprite.getImagen(), sprite.getX_pintar(), sprite.getY_pintar(), this);
                    else
                        gBuffer.drawArc(sprite.getX_pintar(), sprite.getY_pintar(), sprite.getRadio() * 2, sprite.getRadio() * 2, 0, 360);
                } else
                {
                    sprites.removeElement(sprite);
                }
            }

            drawNoSprites(gBuffer);
        }
        g.drawImage(imag, 0, 0, null);
    }

    public void update(Graphics g)
    {
        paint(g);
    }

    public void run()
    {
        long t = System.currentTimeMillis();
        repaint();
        procesoDeCarga();
        try
        {
            tracker.waitForID(0);
        }
        catch(InterruptedException interruptedexception)
        {
            return;
        }
        cargando = false;
        initGame();
        do
        {
            runGame();
            repaint();
            try
            {
                t += retardo;
                Thread.sleep(Math.max(0L, t - System.currentTimeMillis()));
            }
            catch(InterruptedException interruptedexception1)
            {
                return;
            }
        } while(true);
    }

    protected abstract void initGame();

    protected abstract void runGame();

    protected abstract void drawNoSprites(Graphics g);

    protected abstract void pantallaDeCarga(Graphics g);

    protected abstract void procesoDeCarga();

    public boolean pulsadaTecla(int tecla)
    {
        if(tecla == -1)
            return !teclas_pulsadas.isEmpty();
        else
            return teclas_pulsadas.containsKey("" + tecla);
    }

    public void setFondo(Image fondo)
    {
        this.fondo = fondo;
    }

    protected int cargaAnimacion(String url, String extension, int tamanio)
    {
        int posicion = lista_imagenes.size();
        for(int a = 0; a < tamanio; a++)
        {
            lista_imagenes.addElement(getImage(getDocumentBase(), url + a + "." + extension));
            tracker.addImage(getImage(getDocumentBase(), url + a + "." + extension), 0);
        }

        return posicion;
    }

    public void generaImagenesAnimacion(int puesto, int inicio_ani, int tamano, Image img[])
    {
        for(int carga = inicio_ani; carga < inicio_ani + tamano; carga++)
            img[puesto + (carga - inicio_ani)] = (Image)lista_imagenes.elementAt(carga);

    }

    public int cargaSonido(String audio)
    {
        lista_sonidos.addElement(getAudioClip(getDocumentBase(), audio));
        return lista_sonidos.size() - 1;
    }

    public int cargaImagen(String imagen)
    {
        lista_imagenes.addElement(getImage(getDocumentBase(), imagen));
        tracker.addImage(getImage(getDocumentBase(), imagen), 0);
        return lista_imagenes.size() - 1;
    }

    public Image dameImagen(int imagen)
    {
        return (Image)lista_imagenes.elementAt(imagen);
    }

    public void ponerSprite(Sprite sprite)
    {
        sprites.addElement(sprite);
    }

    public Sprite dameSprite(int sprite)
    {
        return (Sprite)sprites.elementAt(sprite);
    }

    public int numeroSprites()
    {
        return sprites.size();
    }

    public void playSound(int codigo)
    {
        AudioClip audioClip = (AudioClip)lista_sonidos.elementAt(codigo);
        audioClip.play();
    }

    protected void limpiaSprites()
    {
        sprites = new Vector();
    }

    protected Hashtable teclas_pulsadas;
    protected Thread anima;
    protected long retardo;
    protected Vector sprites;
    protected MediaTracker tracker;
    private Image fondo;
    protected Image imag;
    protected Vector lista_imagenes;
    protected Vector lista_sonidos;
    protected Graphics gBuffer;
    protected static boolean debug = true;
    private boolean cargando;
    public static final int CUALQUIER_TECLA = -1;



}
TOP

Related Classes of AppletGame

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.