package oracle.forms.fd;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import javax.swing.ImageIcon;
import javax.swing.JPanel;

/*
 * A panel to draw the image
 */
public class myPanel extends JPanel
{
  private Image  img = null ;
  private Color  colorBG = null ;
  private int    iImageWidth = 0, iImageHeight = 0 ;
  private int    iInitialWidth = 0, iInitialHeight = 0 ;
  
  public myPanel( int iWidth, int iHeight)
  {
    iInitialWidth  = iWidth ;
    iInitialHeight = iHeight ;
    this.setPreferredSize(new Dimension(iInitialWidth,iInitialHeight));
  }
  
  // set the image
  public void setImage( Image p_image )
  {
    img = p_image ;
    if( img != null )
    {
      ImageIcon ii = new ImageIcon(img) ;
      iImageWidth  = ii.getIconWidth() ;
      iImageHeight = ii.getIconHeight() ;    
      // ajust the panel size to the new image size
      this.setPreferredSize(new Dimension(iImageWidth,iImageHeight));
    }
    else
    {
       // restore panel size to its initial values
       this.setPreferredSize(new Dimension(iInitialWidth,iInitialHeight)) ;
       iImageWidth = iImageHeight = 0 ;
    }
    this.revalidate();
  }
  
  // set the background color
  public void setBGColor( Color p_color )
  {
    colorBG = p_color ;
  }
  
  // get the image width
  public int getImageWidth()
  {
    return iImageWidth ;
  }  
  
  // get the image height
  public int getImageHeight()
  {
    return iImageHeight ;
  }    
  
  // draw the image
  protected void paintComponent(Graphics g) {
    if( colorBG != null )
    {
       g.setColor(colorBG);
       g.fillRect(0, 0, this.getWidth(), this.getHeight());
    }
    if( img != null ) g.drawImage(img, 0, 0, this);  
  }  
}