import javax.swing.*;
import java.awt.*;
import java.net.MalformedURLException;
import java.net.URL;

/**
 * 
 * @author capolsin
 *
 * Chargement et affichage de deux images, l'une en image de fond d'un JPanel, l'autre par dessus
 * Utilisation du Toolkit du JPanel pour charger les images 
 * Utilisation d'un MediaTarcker pour :
 * 		1) surveiller le chargement de l'image
 * 		2) donner au JPanel la taille exacte de l'image de fond 
 */
class Affiche extends JPanel
{
    // Eviter un Warning
	private static final long serialVersionUID = 10L;
	private Image fond, monde;
	private MediaTracker tracker;

  Affiche(String s1, String s2)
    {
  	  // getImage attend un nom de fichier pour une application et une URL pour une Applet   
  	  this.fond = this.getToolkit().getImage(s1);
      this.monde = this.getToolkit().getImage(s2);
      this.chargeImages();
    }
  
  Affiche(URL url, String s1, String s2)
  {
	  // getImage attend un nom de fichier pour une application et une URL pour une Applet 
  	try{
	  this.fond = this.getToolkit().getImage(new URL(url.toString()+s1));
	  this.monde = this.getToolkit().getImage(new URL(url.toString()+s2));
  	}catch (MalformedURLException e) {}
  	
    this.chargeImages();
  }

private void chargeImages()
{
      //Lancer et surveiller le chargement des images
      this.tracker = new MediaTracker(this);
      this.tracker.addImage(this.fond, 1);
      this.tracker.addImage(this.monde, 2);
      // Attendre le chargement de toutes les images
      try
	  {
    	  this.tracker.waitForAll();
	  }
      catch (InterruptedException e){}
      
      
      // Si toutes les images n'ont pas été chargées avec succès
      if (this.tracker.statusAll(true) != MediaTracker.COMPLETE)
      	{
      		// Donne au JPanel une taille par défaut
        	// pour le JFrame
      		this.setPreferredSize(new Dimension(300, 200));
          	// pour la JApplet
        	this.setSize(new Dimension(300, 200)); 
      		
      		System.out.println("Problème de chargement des images");
      	}
      else
      {
      	// Donne au JPanel la taille de l'image de fond
      	// pour le JFrame
    	this.setPreferredSize(new Dimension(this.fond.getWidth(this), 
      										this.fond.getHeight(this)));
      	// pour la JApplet
    	this.setSize(new Dimension(this.fond.getWidth(this), 
      										this.fond.getHeight(this)));
      }   	
}
  
  public void paintComponent(Graphics g)
  {
    super.paintComponent(g);
    
    g.drawImage(this.fond, 0, 0, this);
    g.drawImage(this.monde, 30, 30, this);
  }
} 
