//==================================================================================================
// G r a p h i c                                                                               Java
// I m a g e C o m p o n e n t
//                                                                                By Bruno Bachelet
//==================================================================================================
// Copyright (c) 1999-2016
// Bruno Bachelet - bruno@nawouak.net - http://www.nawouak.net
//
// This file is part of the B++ Library. This library is free software; you can redistribute it
// and/or modify it under the terms of the GNU Library General Public License as published by the
// Free Software Foundation; either version 2 of the License, or (at your option) any later
// version.
//
// This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
// without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
// the GNU Library General Public License for more details (http://www.gnu.org).

/*DESCRIPTION*/
/* This module provides a class to manipulate an image as a graphical component. */

// Package //---------------------------------------------------------------------------------------
package bpp.graphic;

// Importation //-----------------------------------------------------------------------------------
import java.awt.*;
import java.awt.image.*;
import java.io.*;

// I m a g e C o m p o n e n t  Class //------------------------------------------------------------
/*CLASS ImageComponent */ /* Represents an image embedded inside a graphical component. */
public class ImageComponent extends Container {
 //---------------------------------------------------------------------------------------Attributes
 protected Image atImage; // The image.
 //--------------------------------------------------------------------------------------Constructor
 /*METHOD ImageComponent */ /* Builds a component with an image read from a file. */
 public ImageComponent(String agFileName) throws Exception {
  super();
  setVisible(false);
  setImage(agFileName);
  setSize(atImage.getWidth(this),atImage.getHeight(this));
 }
 //--------------------------------------------------------------------------------------Constructor
 /*METHOD ImageComponent */ /* Builds a component with a given image. */
 public ImageComponent(Image agImage) {
  super();
  setVisible(false);
  atImage=agImage;
  setSize(atImage.getWidth(this),atImage.getHeight(this));
 }
 //--------------------------------------------------------------------------------------------Paint
 /*METHOD ImageComponent */ /* Draws the component. */
 public void paint(Graphics agGraphic) { agGraphic.drawImage(atImage,0,0,this); }
 //--------------------------------------------------------------------------------------------Image
 /*METHOD ImageComponent */ /* Returns the image of the component. */
 public Image image() { return (atImage); }
 //-----------------------------------------------------------------------------------------SetImage
 /*METHOD ImageComponent */ /* Sets the image of the component from the content of a file. */
 public void setImage(String agFileName) throws Exception {
  File lcFile = new File(agFileName);

  if (!lcFile.exists() || !lcFile.isFile()) throw new Exception("Graphic - File doesn't exist.");
  setImage(getToolkit().createImage(agFileName));
 }
 //-----------------------------------------------------------------------------------------SetImage
 /*METHOD ImageComponent */ /* Sets the image of the component. */
 public void setImage(Image agImage) {
  atImage=agImage;

  while ((checkImage(atImage,this)&(ImageObserver.WIDTH|ImageObserver.HEIGHT))==0) {
   prepareImage(atImage,this);
   try { Thread.sleep(5); } catch (Exception e) {}
  }
 }
}

// End //-------------------------------------------------------------------------------------------