package edu.school;

import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.JPanel;

public class ImagePanel extends JPanel{

    private BufferedImage image;

    public ImagePanel(String path) {
       try {                
          image = ImageIO.read(new File(path));
       } catch (IOException ex) {
            // handle exception...
       }
    }

    public void setImage(String path)
    {
    	try{
    		image = ImageIO.read(new File(path));
    		invalidate();
    		repaint();
    	}
    	catch(IOException ex)
    	{
    		
    	}
    }
    
    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2d = (Graphics2D) g;
        g2d.scale((double)this.getWidth()/(double)image.getWidth(), (double)this.getHeight()/(double)image.getHeight());
        g2d.drawImage(image, 0, 0, this); // see javadoc for more info on the parameters            
    }

}