Mostrando las entradas con la etiqueta animacion. Mostrar todas las entradas
Mostrando las entradas con la etiqueta animacion. Mostrar todas las entradas

Animacion Con Lineas en Java

Capturas de Pantalla



Codigo

package masterdavidjavabasico;

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

public class BouncingLines extends Applet implements Runnable {

    Thread runner = null;

    final static int WIDTH  = 600;
    final static int HEIGHT = 500;

    Image    image;
    Graphics graphics;



    int[] x1;
    int[] y1;
    int[] x2;
    int[] y2;

    int dx1 = 2 + (int)( 3 * Math.random() );
    int dy1 = 2 + (int)( 3 * Math.random() );
    int dx2 = 2 + (int)( 3 * Math.random() );
    int dy2 = 2 + (int)( 3 * Math.random() );

    static int first = 0;

    final static int LINES = 50;


    public void init() {


        x1 = new int[LINES];
        y1 = new int[LINES];
        x2 = new int[LINES];
        y2 = new int[LINES];

    

        x1[0] = (int)( WIDTH  * Math.random() );
        y1[0] = (int)( HEIGHT * Math.random() );
        x2[0] = (int)( WIDTH  * Math.random() );
        y2[0] = (int)( HEIGHT * Math.random() );

      

        for ( int i = 1; i < LINES; i++ ) {

            x1[i] = x1[0];
            y1[i] = y1[0];
            x2[i] = x2[0];
            y2[i] = y2[0];
        }
        image    = createImage( WIDTH, HEIGHT );
        graphics = image.getGraphics();
    }


    public void start() {

      

        if ( runner == null ) {

            runner = new Thread( this );
            runner.start();
        }
    }


    public void stop() {

       

        if ( runner != null && runner.isAlive() )
            runner.stop();

        runner = null;
    }


    public void run() {

        while (runner != null) {

            repaint();

            try {

                Thread.sleep( 20 );

            } catch ( InterruptedException e ) {

              
            }
        }
    }


    public void paint( Graphics g ) {

        update( g );
    }


    public void update( Graphics g ) {


        graphics.setColor( Color.white );
        graphics.fillRect( 0, 0, WIDTH, HEIGHT );

       

        graphics.setColor( Color.red );

        int line = first;

        for ( int i = 0; i < LINES; i++ ) {

            graphics.drawLine( x1[line], y1[line],
                               x2[line], y2[line] );
            line++;
            if ( line == LINES ) line = 0;
        }

        line = first;

        first--;

        if ( first < 0 ) first = LINES - 1;

        x1[first] = x1[line];
        y1[first] = y1[line];
        x2[first] = x2[line];
        y2[first] = y2[line];

     
        if (x1[first] + dx2 < WIDTH)
            x1[first] += dx1;
        else
            dx1 = -(2 + (int)( 3 * Math.random() ));

        if (x1[first] + dx1 >= 0)
            x1[first] += dx1;
        else
            dx1 = 2 + (int)( 3 * Math.random() );

        if (y1[first] + dy1 < HEIGHT)
            y1[first] += dy1;
        else
            dy1 = -(2 + (int)( 3 * Math.random() ));

        if (y1[first] + dy1 >= 0)
            y1[first] += dy1;
        else
            dy1 = 2 + (int)( 3 * Math.random() );

        if (x2[first] + dx2 < WIDTH)
            x2[first] += dx2;
        else
            dx2 = -(2 + (int)( 3 * Math.random() ));

        if (x2[first] + dx2 >= 0)
            x2[first] += dx2;
        else
            dx2 = 2 + (int)( 3 * Math.random() );

        if (y2[first] + dy2 < HEIGHT)
            y2[first] += dy2;
        else
            dy2 = -(2 + (int)( 3 * Math.random() ));

        if (y2[first] + dy2 >= 0)
            y2[first] += dy2;
        else
            dy2 = 2 + (int)( 3 * Math.random() );



        g.drawImage( image, 0, 0, this );
    }
}

Descargar Programa

Animacion Sencilla en Java usando un circulo




Codigo

package masterdavidjavabasico;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Demo1 extends JComponent {

    private final static int ANCHO = 512;

    private final static int ALTO = 384;

    private final static int DIAMETRO = 20;

    private float x, y;

    private float vx, vy;

    public Demo1() {
        setPreferredSize(new Dimension(ANCHO, ALTO));
        x = 10;
        y = 20;
        vx = 300;
        vy = 400;
    }

    private void fisica(float dt) {
        x += vx * dt;
        y += vy * dt;
        if (vx < 0 && x <= 0 || vx > 0 && x + DIAMETRO >= ANCHO)
            vx = -vx;
        if (vy < 0 && y < 0 || vy > 0 && y + DIAMETRO >= ALTO)
            vy = -vy;
    }

    public void paint(Graphics g) {
        g.setColor(Color.WHITE);
        g.fillRect(0, 0, ANCHO, ALTO);
        g.setColor(Color.RED);
        g.fillOval(Math.round(x), Math.round(y), DIAMETRO, DIAMETRO);
    }

    private void dibuja() throws Exception {
        SwingUtilities.invokeAndWait(new Runnable() {
                public void run() {
                    paintImmediately(0, 0, ANCHO, ALTO);
                }
            });
    }

    public void cicloPrincipalJuego() throws Exception {
        long tiempoViejo = System.nanoTime();
        while (true) {
            long tiempoNuevo = System.nanoTime();
            float dt = (tiempoNuevo - tiempoViejo) / 1000000000f;
            tiempoViejo = tiempoNuevo;
            fisica(dt);
            dibuja();
        }
    }

    public static void main(String[] args) throws Exception {
        JFrame jf = new JFrame("Demo1");
        jf.addWindowListener(new WindowAdapter() {
                public void windowClosing(WindowEvent e) {
                    System.exit(0);
                }
            });
        jf.setResizable(false);
        Demo1 demo1 = new Demo1();
        jf.getContentPane().add(demo1);
        jf.pack();
        jf.setVisible(true);
        demo1.cicloPrincipalJuego();
    }
}
Descargar el Programa

Animacion Relog en Java Con Imagenes

Capturas 





Codigo

package relog03dic09;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

/**
 *
 * @author Master
 */
public class AnimadorLogo extends JPanel
        implements ActionListener{
    protected ImageIcon imagenes[]; // declaramos un arreglo de imagenes
    protected int totalImagenes=24, //variable de tipo entero ultima imagen
            imagenActual=0, // variable de tipo entero primera imagen
            retardoAnimacion=1500; //1500 milisegundos de retardo
    protected Timer cronoAnimacion; // variable de tipo tiempo
    public AnimadorLogo() // constructor
    {
        setSize(getPreferredSize()); // toma el valor de las dimenciones de la imagen
        imagenes=new ImageIcon[totalImagenes]; // manda una instancia al arreglo de imagenes
        for(int i=0; i<imagenes.length; ++i) // se hace un incremento en i empesando de 0 de 1 en 1 hasta el valor asignado a la variable totalImagenes
            imagenes[i]=
                    new ImageIcon("imgrelog/relog"+i+".gif"); // genera un nombre para la imagen y lo va cambiando dependiendo el valor de i
        iniciaAnimacion(); // manda a llamar el metodo iniciaAnimacion que esta mas abajo
            }
public void paintComponent(Graphics g)
{
        super.paintComponent(g);//inicia los componentes del metodo graphics
if(imagenes[imagenActual].getImageLoadStatus()==MediaTracker.COMPLETE){//va cambiando la imagen
    imagenes[imagenActual].paintIcon(this, g, 0, 0);// imprime la imagen en las cordenandas 0,0
            imagenActual=(imagenActual+1)%totalImagenes;// cuando la imagen llega a la ultima regresa al principio
}


}
public void actionPerformed(ActionEvent e)
{
    repaint();// actualiza la ventana para que no se amontonen las imagenes
}
public void iniciaAnimacion()
{
    if(cronoAnimacion ==null){
        imagenActual=0;
        cronoAnimacion = new Timer(retardoAnimacion, this);
        cronoAnimacion.start();
    }
    else
        if(!cronoAnimacion.isRunning())
            cronoAnimacion.restart();
}
public void terminaAnimacion()
{
    cronoAnimacion.stop();
}
public Dimension getMinimumSize()
{
    return getPreferredSize();
}
public Dimension getPreferedSize()
{
    return new Dimension(500,500);
}
public static void main(String args[])
{
    AnimadorLogo anim = new AnimadorLogo();
    JFrame app = new JFrame("Master Animacion");
    app.getContentPane().add( anim, BorderLayout.CENTER);
    app.addWindowListener(new WindowAdapter(){
        public void windowClosing(WindowEvent e)
        {
            System.exit(0);
        }
    }
    );
    app.setSize ( anim.getPreferredSize().width+58+250,
            anim.getPreferredSize().height+80+250);
    app.show();
}
}
Descargar Proyecto