Moviendo una PelotaEl código original es de:
http://www.javacooperation.gmxhome.de/Esta programado como Applet (Para ser reproducido en un Navegador).
Yo lo traduje y le agrege algunos comentarios para aclarar. ¡Cuidense!
import java.applet.*; // El import es lo mismo que el #Include en C y Php. El * Significa TODO.
import java.awt.*;
public class Ballbewegung1 extends Applet implements Runnable //Recordamos incluir el Applet
{
// Inicializamos las variables
int x_pos = 10; // Coordenada inicial X
int y_pos = 100; // Coordenada Inicial Y
int radius = 20; // Radio de la Pelota
public void init()
{
setBackground (Color.blue); //Color de fondo
}
public void start ()
{
// Creamos un nuevo thread (Este mismo) con el nombre th
Thread th = new Thread (this);
// Iniciamos th (El thread recien creado)
th.start ();
}
public void stop()
{
}
public void destroy()
{
}
public void run ()
{
// Thread de Minima Prioridad
Thread.currentThread().setPriority(Thread.MIN_PRIORITY);
// Mientras el Thread este corriendo...
while (true)
{
// Aumenta la coordenada
x_pos ++;
// Re-Pinta el Applet
repaint();
try
{
// Para al Thread por 20 milisegundos
Thread.sleep (20);
}
catch (InterruptedException ex)
{
// do nothing
}
// Thread de Maxima Prioridad (En el while, mientras este corriendo)
Thread.currentThread().setPriority(Thread.MAX_PRIORITY);
}
}
public void paint (Graphics g)
{
g.setColor (Color.red); //Declaramos el color de la pelota
g.fillOval (x_pos - radius, y_pos - radius, 2 * radius, 2 * radius); //Dibujamos la pelota
}
}
Demostracion del Applet:
http://www.javacooperation.gmxhome.de/Applets/Ballbewegung1/BallBewegung1Applet.htmlMoviendo una Pelota con DobleBufferComparen con el anterior para aprender mas

.
Yo lo traduje y le agrege algunos comentarios para aclarar. ¡Cuidense!
import java.applet.*; // El import es lo mismo que el #Include en C y Php. El * Significa TODO.
import java.awt.*;
public class Ballbewegung1 extends Applet implements Runnable //Recordamos incluir el Applet
{
// Inicializamos las variables
int x_pos = 10; // Coordenada inicial X
int y_pos = 100; // Coordenada Inicial Y
int radius = 20; // Radio de la Pelota
// Variables de doblebuffer
private Image dbImage;
private Graphics dbg;
public void init()
{
setBackground (Color.blue); //Color de fondo
}
public void start ()
{
// Creamos un nuevo thread (Este mismo) con el nombre th
Thread th = new Thread (this);
// Iniciamos th (El thread recien creado)
th.start ();
}
public void stop()
{
}
public void destroy()
{
}
public void run ()
{
// Thread de Minima Prioridad
Thread.currentThread().setPriority(Thread.MIN_PRIORITY);
// Mientras el Thread este corriendo...
while (true)
{
// Aumenta la coordenada
x_pos ++;
// Re-Pinta el Applet
repaint();
try
{
// Para al Thread por 20 milisegundos
Thread.sleep (20);
}
catch (InterruptedException ex)
{
// do nothing
}
// Thread de Maxima Prioridad (En el while, mientras este corriendo)
Thread.currentThread().setPriority(Thread.MAX_PRIORITY);
}
}
public void update (Graphics g)
{
// Iniciamos el doble Buffer
if (dbImage == null)
{
dbImage = createImage (this.getSize().width, this.getSize().height);
dbg = dbImage.getGraphics ();
}
// Sera igual a nuestra imagen actual.
dbg.setColor (getBackground ());
dbg.fillRect (0, 0, this.getSize().width, this.getSize().height);
// El fondo sera nuestra imagen actual.
dbg.setColor (getForeground());
paint (dbg);
// Dibujamos el doble.
g.drawImage (dbImage, 0, 0, this);
}
public void paint (Graphics g)
{
g.setColor (Color.red); //Declaramos el color de la pelota
g.fillOval (x_pos - radius, y_pos - radius, 2 * radius, 2 * radius); //Dibujamos la pelota
}
}
Demostracion del Applet:
http://www.javacooperation.gmxhome.de/Applets/Doppelpufferung/BallBewegung2Applet.htmlCambiando velocidad y direccion de una PelotaComparen con el anterior para aprender mas

. Aquí hacemos que la pelota "rebote" al tocar un extremo.
Yo lo traduje y le agrege algunos comentarios para aclarar. ¡Cuidense!
import java.applet.*; // El import es lo mismo que el #Include en C y Php. El * Significa TODO.
import java.awt.*;
public class Ballbewegung1 extends Applet implements Runnable //Recordamos incluir el Applet
{
// Inicializamos las variables
int x_pos = 10; // Coordenada inicial X
int y_pos = 100; // Coordenada Inicial Y
int radius = 20; // Radio de la Pelota
int appletsize_x = 300; // Tamaño del applet
int appletsize_y = 300; // Tamaño del applet
// Variables de doblebuffer
private Image dbImage;
private Graphics dbg;
public void init()
{
setBackground (Color.blue); //Color de fondo
}
public void start ()
{
// Creamos un nuevo thread (Este mismo) con el nombre th
Thread th = new Thread (this);
// Iniciamos th (El thread recien creado)
th.start ();
}
public void stop()
{
}
public void destroy()
{
}
public void run ()
{
// Thread de Minima Prioridad
Thread.currentThread().setPriority(Thread.MIN_PRIORITY);
// Mientras el Thread este corriendo...
while (true)
{
// Aumenta la coordenada
x_pos ++;
// Re-Pinta el Applet
repaint();
try
{
// Para al Thread por 20 milisegundos
Thread.sleep (20);
}
catch (InterruptedException ex)
{
// do nothing
}
// Thread de Maxima Prioridad (En el while, mientras este corriendo)
Thread.currentThread().setPriority(Thread.MAX_PRIORITY);
}
}
public void update (Graphics g)
{
// Iniciamos el doble Buffer
if (dbImage == null)
{
dbImage = createImage (this.getSize().width, this.getSize().height);
dbg = dbImage.getGraphics ();
}
// Sera igual a nuestra imagen actual.
dbg.setColor (getBackground ());
dbg.fillRect (0, 0, this.getSize().width, this.getSize().height);
// El fondo sera nuestra imagen actual.
dbg.setColor (getForeground());
paint (dbg);
// Dibujamos el doble.
g.drawImage (dbImage, 0, 0, this);
}
public void paint (Graphics g)
{
g.setColor (Color.red); //Declaramos el color de la pelota
g.fillOval (x_pos - radius, y_pos - radius, 2 * radius, 2 * radius); //Dibujamos la pelota
}
}
Demostracion del Applet:
http://www.javacooperation.gmxhome.de/Applets/BallStop/BallBewegungReverseApplet.html