package org.avmdti.josast.demarage;

import java.awt.Container;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.MediaTracker;
import java.awt.Window;
import java.util.logging.Level;
import java.util.logging.Logger;



/*
 * 
 * <b>Description :</b>
 * 
 * 
 * <p>Projet : JCP <BR>
 * 
 * <br>
 * <b>JOSAST</b> : Java Open Source Amateur Satellite  Toolbox 
 * <br>The aim of the project is to create a set of tools for amateur satellite purpose. All this tools could be used together to create specific software.
 * <b>JOSAST</b> project is managed by AVMDTI (<A HREF="http://www.avmdti.org">http://www.avmdti.org </a> )
 * <br> This software is an open source software. Please read the <b><i>JOSAST licence</b></i><BR>(<A HREF="http://www.avmdti.org">http://www.avmdti.org </a> )
 * <p>for more information contact <a href="mailto:josast@avmdti.org">josast@avmdti.org</a></p>
 * @author  <a href="mailto:mercier.josast@avmdti.org">mercier</a>
 * @version 1.0
 * <p><b>date </b>23 avr. 2003</p>
 * <p><b><i> Source Update </b></i></p>
 * <br> date : name : comments 
 * <br> V1 : mercier : create file
 * <p>
 * 
 */

/**
 *  Display a windows.
 *  <br>
 *  @author XTOPHE
 *  @version 1.0
 */
public class ImageIntroduction extends Container {

	private Logger log = Logger.getLogger(getClass().getName());
	private static final long serialVersionUID = -3692619926235822726L;
	private Image image;
	private int iw, ih;
	/**
	 * Constructor for ImageIntroduction.
	 * @param Filename : nom du fichier image
	 */
	public ImageIntroduction(String Filename) {
		image = getToolkit().getImage(Filename);
		MediaTracker mt = new MediaTracker(this);
		mt.addImage(image, 0);
		try {
			mt.waitForID(0);
		} catch (InterruptedException e) {
			log.log(Level.SEVERE,"error to load the image");
		}
		iw = image.getWidth(this);
		ih = image.getHeight(this);
		if (iw < 0 || ih < 0) {
			log.log(Level.SEVERE,"image size null");
			image=null;
		}

	}

	/** (non-Javadoc)
	 * @see java.awt.Container#paint(java.awt.Graphics)
	 */
	public void paint(Graphics g) {

		if (image == null) {
			log.log(Level.SEVERE,"image is not loaded");
			return;
		}

		g.drawImage(image, 0, 0, this);
	}

	/**
	 * Rescale the image to the argument value.
	 * 
	 * @param width
	 * @param height
	 */
	public void setscale(int width, int height) {
		if (image != null)
		{
		Image imagescale =
			image.getScaledInstance(width, height, Image.SCALE_DEFAULT);
		image = imagescale;
		do {
			iw = image.getWidth(this);
			ih = image.getHeight(this);
		} while (iw < 0);
		}
	}

	/**
	 * Returns the height.
	 * @return int
	 */
	public int getIh() {
		return ih;
	}

	/**
	 * Returns the width.
	 * @return int
	 */
	public int getIw() {
		return iw;
	}
	
	
	/**
	 * Demonstrate the usage of this class
	 * <br>
	 * usage : ImageIntroduction filename
	 * <br>
	 * @param args image filename
	 */
	public static void main(String[] args) {
		ImageIntroduction im = new ImageIntroduction(args[0]);
		Window w = new Window(new Frame());
		w.add(im);
		w.setSize(im.getIw(), im.getIh());
		w.setVisible(true);

	}
}
