All pastes #466650 Raw Edit

Untitled

public text v1 · immutable
·published 2007-05-01 03:07 UTC
rendered paste body
/*
 * TheApplet.java
 *
 * Created on April 30, 2007, 7:26 PM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

//package javaapplication4;

import java.awt.*;
import java.applet.*;

/**
 *
 * @author Administrator
 */
public class TheApplet extends Applet implements Runnable {
    
    int appletWidth = 600;
    int appletHeight = 400;
    
    private Image dbImage;
    private Graphics dbg;
    
    private Bala bala;
    
    Thread th;
    
    /** Creates a new instance of TheApplet */
    public TheApplet() {
    }
    
    public void init() {
        setBackground(Color.BLACK);
        bala = new Bala();
    }
    
    public void start() {
        th = new Thread (this);
	th.start ();
    }
    
    public void stop() {
        th.stop();
    }
    
    public void destroy() {/*TODO*/}
    
    public void paint (Graphics g) {
        bala.DibujaBala(g);
    }
    
    public void run() {
        Thread.currentThread().setPriority(Thread.MIN_PRIORITY);
        
        while (true) {
            bala.mueve();
			repaint();
            
            try {
                Thread.sleep(15);
            }
            catch (InterruptedException ex) {
                //HACE NADA
            }
            
        Thread.currentThread().setPriority(Thread.MAX_PRIORITY);
        }
    }
    
/* Para la técnica llamada "Double buffering"
 * y evitar que el applet parpadee. */
    public void update (Graphics g) {
        //inicializar buffer
        if (dbImage==null) {
            dbImage = createImage(this.getSize().width,this.getSize().height);
            dbg = dbImage.getGraphics();
        }
        
        //limpiar la pantalla en el fondo
        dbg.setColor(getBackground());
        dbg.fillRect(0,0,this.getSize().width,this.getSize().height);
        
        //dibujar elementos en el fondo
        dbg.setColor(getForeground());
        paint(dbg);
        
        //dibujar imágen en la pantalla
        g.drawImage(dbImage,0,0,this);
    }
    
}