import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.util.Timer;
import java.util.TimerTask;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Prueba extends JPanel{
int x=0;
int y=0;
boolean Graficar = false;
public Prueba(){
Timer tiempo = new Timer();//creamos una instancia para el Timer
TimerTask task = new TimerTask(){
public void run(){
x=x+1;
if(x>1000){
x=0;
y=y+10;
}
if(x>100){
Graficar = true;
}
if(y>20){
Graficar = false;
}
repaint();
}};
// se inicia la animacion
tiempo.schedule(task,0, 50);
}
/*----------------------------------------------------------------------------*/
public void paintComponent(Graphics g) {// metodo para dibujar componentes en Panel
try{
super.paintComponent(g);
if(Graficar){
Graphics2D g2 = (Graphics2D)g;
BufferedImage Imagen = ImageIO.read(new File("/Imagenes/images.jpg"));
g2.drawImage(Imagen, x +150, y+150, null);
BufferedImage Imagen2 = ImageIO.read(new File("/Imagenes/descarga.png"));
g2.drawImage(Imagen2, x, y+300, null);
BufferedImage Imagen3 = ImageIO.read(new File("/Imagenes/images.png"));
g2.drawImage(Imagen3, x, y+500, null);
g2.setColor(Color.GREEN); // color de la figura
for(int i=0;i<10;i++){
int x2 = (int)(Math.random()*1000);
int y2 = (int)(Math.random()*1000);
g2.fillRect(x2, y2, 100, 100);
g2.fillOval(x2+150, y2, 100, 100);
}
g2.setColor(Color.CYAN); // color de la figura
for(int i=0;i<10;i++){
int x2 = (int)(Math.random()*1000);
int y2 = (int)(Math.random()*1000);
g2.fillRect(x2, y2, 100, 100);
g2.fillOval(x2+150, y2, 100, 100);
}
g2.setColor(Color.RED); // color de la figura
for(int i=0;i<10;i++){
int x2 = (int)(Math.random()*1000);
int y2 = (int)(Math.random()*1000);
g2.fillRect(x2, y2, 100, 100);
g2.fillOval(x2+150, y2, 100, 100);
}
g2.dispose();
}
g.dispose();
}catch(Exception ex){}
}//end paintComponent
public static void main(String[] args) {
JFrame jf = new JFrame();
jf.add(new Prueba());
jf.setVisible(true);
jf.setExtendedState(JFrame.MAXIMIZED_BOTH);
}
}