Source Code : Image Panel

Java Is Open Source Programming Language You Can Download From Java and Java Libraries From http://www.oracle.com. Click Here to download
We provide this code related to title for you to solve your developing problem easily. Libraries which is import in this program you can download from http://www.oracle.com. Click Here or search from google with Libraries Name you get jar file related it

Image Panel

      

//   ImagePanel
//
//   Copyright (C) by Andrea Carboni.
//   This file may be distributed under the terms of the LGPL license.
//

import java.awt.Dimension;
import java.awt.Graphics;

import javax.swing.ImageIcon;
import javax.swing.JPanel;

public class ImagePanel extends JPanel
{
  private ImageIcon image  = new ImageIcon();
  private int       margin = 0;
  
  //---------------------------------------------------------------------------

  public ImagePanel()
  {
  }

  //---------------------------------------------------------------------------

  public ImagePanel(String imageFile)
  {
    setImage(imageFile);
  }

  //---------------------------------------------------------------------------

  public void setImage(String imageFile)
  {
    image = new ImageIcon(imageFile);
    
    updatePrefSize();
  }

  //---------------------------------------------------------------------------

  public void setMargin(int m)
  {
    margin = m;
    
    updatePrefSize();
  }
  
  //---------------------------------------------------------------------------
  //---
  //---   Internal methods
  //---
  //---------------------------------------------------------------------------
  
  public void paintComponent(Graphics g)
  {
    super.paintComponent(g);
    
    if (image.getImage() != null)
      g.drawImage(image.getImage(), margin, margin, this);
  }
  
  //---------------------------------------------------------------------------
  
  private void updatePrefSize()
  {
    int iw = image.getIconWidth();
    int ih = image.getIconHeight();
    
    setPreferredSize(new Dimension(iw + margin*2, ih + margin*2));
  }
}

   
    
    
    
    
    
  

Thank with us