public class Prueba extends Jpanel implements Runnable
{
Thread animator = null;
boolean running = false;
Graphics g;
Image img = null;
public Prueba(int ancho, int alto)
{
setPreferredSize(new Dimension(ancho, alto));
setBackground(Color.BLACK);
}
public void addNotify()
{
super.addNotify();
if(animator == null || !running)
{
animator = new Thread(this);
animator.start();
}
}
public void render() {
if(image == null)
{
image = createImage(ancho, alto);
if(image == null)
{
System.out.println("Image vale null");
return;
}
else
g = image.getGraphics();
}
g.setColor(Color.BLACK);
g.fillRect(0, 0, ancho, alto);
// Tus dibujos van a partir de esta linea<----------------------------------------
g.setColor(Color.WHITE);
g.fillRect(0,0,300,300);
}
/*
* Pasa la imagen dibujada a la pantalla
*/
public void paintScreen()
{
Graphics d = this.getGraphics();
d.drawImage(image, 0, 0, null);
Toolkit.getDefaultToolkit().sync();
d.dispose();
}
public void run()
{
running = true;
while(running)
{
render()
paintScreen();
}
System.exit(0);
}
}