title: animated background java game
body: <p>I was programming a game similar to asteroid, but I do not understand how to spawn the asteroids in the background.</p>
  
  <p><img src="https://i.stack.imgur.com/yfZdz.png" alt="http://gyazo.com/aebb41b4ef4074823fc46e8e124fc570"></p>
  
  <p>now i spawn an asteroid in the main class but i want create a class for the asteroid ho i do it?</p>
  
  <p>MAIN CLASS</p>
  
  <pre><code>public void paintComponent(Graphics g)
  {
      super.paintComponent(g);
      Graphics2D g2d = (Graphics2D) g;
      if(flag_img)
      {
          background(g2d);  
          logo(g2d);          
          menu(g2d);          
          spaceship(g2d);   
          crediti(g2d);    
      }
  }
  </code></pre>
  
  <p>background function(now)</p>
  
  <pre><code>private void background(Graphics2D g2d) 
  {
  
      asteroidi_g_x+=r.nextInt(4);
      asteroidi_g_y+=r.nextInt(1);
      g2d.drawImage(asteroidi_g[0], asteroidi_g_x,asteroidi_g_y,this);
  }
  </code></pre>
  
  <p>background function(what i want)</p>
  
  <pre><code>private void background(Graphics2D g2d) 
  {
      asteroid asteroid = new asteroid[10];
  }
  </code></pre>
  
  <p>and class asteroid</p>
  
  <pre><code>public class asteroid extends JPanel implements ActionListener
  {
     private BufferedImage images_asteroid;
     private boolean flag_img;
  
     private JPanel jp;
  
     private int x,y;
  
     public asteroide_grande(JPanel jp)
     {
      flag_img = true;
      x = (jp.getWidth()/2);
      y = (jp.getHeight()/2);
      this.jp = jp;
  
          try {
              images_asterod = ImageIO.read(this.getClass().getResource("images/asteroid/a1.png"));
          } catch(IOException e){flag = false;}
  }
  
  @Override
  public void paintComponent(Graphics g)
  {
      super.paintComponent(g);
     if(flag_img)
    {
          g.drawImage(images_asteroid, 100, 100,this);
    }
  }
  
  
  @Override
  public void actionPerformed(ActionEvent e) 
  {
      x=x-1;
      y=y+1;
      repaint();
  }
  </code></pre>
  
  <p>method paintcomponent in class doesn't work</p>