import java.awt.BorderLayout;
import java.awt.Graphics2D;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.JTextField;
import javax.swing.event.*;

public class JFrameTailleImage extends JFrame implements ActionListener, ChangeListener {


	private static final long serialVersionUID = 1L;
	private JPanelTailleImage monJPanelTailleImage;
	private JCheckBox ratio;
	private JSlider JSlideX, JSlideY;
	private JTextField JTextX, JTextY;
	private JButton annuler, enregistrer, tourner;
	private int tailleX, tailleY;
	private float rapport;
	private JFileChooser fc;
	private Image monImage;
	private BufferedImage monImageresize;
	
	public JFrameTailleImage(Image monim)
	{
		super("Dimension image");
		this.monImage = monim;
		this.setLayout(new BorderLayout());
		this.monJPanelTailleImage = new JPanelTailleImage(this.monImage);
		this.tailleX = monim.getWidth(this);
		this.tailleY = monim.getHeight(this);
		this.rapport = ((float) this.tailleX)/this.tailleY;
	
		this.fc = new JFileChooser();
		//this.fc.setCurrentDirectory(new File("D:\\java\\Image_diaporama"));
		this.fc.setCurrentDirectory(new File("U:\\cours\\java\\Image_diaporama"));
		
		JPanel panoutils = new JPanel (new GridLayout(4, 3));
		this.ratio = new JCheckBox("");
		this.ratio.setSelected(true);
		this.JTextX = new JTextField(""+this.tailleX);
		this.JTextX.setText(""+this.tailleX);
		this.JTextX.addActionListener(this);
		this.JTextY = new JTextField(""+this.tailleY);
		this.JTextY.setText(""+this.tailleY);
		this.JTextY.addActionListener(this);
		
		this.JSlideX = new JSlider(0, 2000, this.tailleX);
		this.JSlideX.setMajorTickSpacing(500);
		this.JSlideX.setMinorTickSpacing(100);
		this.JSlideX.setPaintTicks(true);
		this.JSlideX.setPaintLabels(true);
		this.JSlideX.addChangeListener(this);

		this.JSlideY = new JSlider(0, 2000, this.tailleY);
		this.JSlideY.setMajorTickSpacing(500);
		this.JSlideY.setMinorTickSpacing(100);
		this.JSlideY.setPaintTicks(true);
		this.JSlideY.setPaintLabels(true);
		this.JSlideY.addChangeListener(this);

		this.enregistrer = new JButton("Enregistrer ...", new ImageIcon("icons/Save_enabled.PNG"));
		this.enregistrer.addActionListener(this);
		//this.tourner = new JButton("Rotation ...", new ImageIcon("icons/Save_enabled.PNG"));
		//this.tourner.addActionListener(this);
		this.annuler = new JButton("Annuler");
		this.annuler.addActionListener(this);
		
		panoutils.add(new JLabel("Taille horizontale :"));
		panoutils.add(this.JTextX);
		panoutils.add(this.JSlideX);
		
		panoutils.add(new JLabel("Taille verticale :"));
		panoutils.add(this.JTextY);
		panoutils.add(this.JSlideY);		
		
		panoutils.add(new JLabel("Garder ratio :"));
		panoutils.add(this.ratio);
		panoutils.add(new JLabel(" "));	
		
		panoutils.add(this.enregistrer);
		//panoutils.add(this.tourner);
		panoutils.add(new JLabel(" "));
		panoutils.add(this.annuler);	
		
		this.add(panoutils, BorderLayout.SOUTH);
		this.add(this.monJPanelTailleImage, BorderLayout.CENTER);
		
		this.pack();
	}

	// Les action sur les bouttons et les JTextField
	public void actionPerformed(ActionEvent e) {
	
		if (e.getSource() == this.annuler)
		{
			this.dispose();
		}
		if (e.getSource() == this.enregistrer)
		{	
			// Sauver le fichier
	        int returnVal = fc.showOpenDialog(this);
	        if (returnVal == JFileChooser.APPROVE_OPTION) {
	            File fichier = fc.getSelectedFile();
	            try{
	            	ImageIO.write(this.monImageresize, "png", fichier);
	            }
	            catch (IOException ee){
	            	System.out.println("Problème de sauvegarde " + ee.getMessage());
	            }
	        } 
			this.dispose();
		}
		if (e.getSource() == this.tourner){
			
		}
		if (e.getSource() == this.JTextX){
			this.JSlideX.setValue(Integer.parseInt(this.JTextX.getText()));
		}
		if (e.getSource() == this.JTextY){
			this.JSlideY.setValue(Integer.parseInt(this.JTextY.getText()));
		}
	}
	
	// Redimensionne l'image
	// ATTENTION : mauvaise qualité en sortie !!!!!!
	private static BufferedImage resize(Image image, int width, int height) {		
		BufferedImage resizedImage = new BufferedImage(width, height,
		BufferedImage.TYPE_INT_ARGB);
		Graphics2D g = resizedImage.createGraphics();
		g.drawImage(image, 0, 0, width, height, null);
		g.dispose();
		return resizedImage;
		} 
	
	// Pour traiter les actions sur les JSlider
	public void stateChanged(ChangeEvent e) {
	    JSlider source = (JSlider)e.getSource();
	    
	    if (!source.getValueIsAdjusting()) {
	    	if (e.getSource() == this.JSlideX)
			{
				if (this.ratio.isSelected())
				{
					this.JTextX.setText(Integer.toString(this.JSlideX.getValue()));
					int valY = Math.round(this.JSlideX.getValue()/this.rapport);
					this.JTextY.setText(Integer.toString(valY));
					this.JSlideY.setValue(valY);
				}
				else
				{
					this.JTextX.setText(Integer.toString(this.JSlideX.getValue()));
				}
			}
	    	else
	    	{
	    		if (this.ratio.isSelected())
				{
					this.JTextY.setText(Integer.toString(this.JSlideY.getValue()));
					int valX = Math.round(this.JSlideY.getValue()*this.rapport);
					this.JTextX.setText(Integer.toString(valX));
					this.JSlideX.setValue(valX);
				}
				else
				{
					this.JTextY.setText(Integer.toString(this.JSlideY.getValue()));
				}
	    	}
	    		
	    	// Resize de l'image et repaint du JPanel
	    	this.monImageresize = resize(this.monImage, 
					Integer.parseInt(this.JTextX.getText()),
					Integer.parseInt(this.JTextY.getText()));
	        this.monJPanelTailleImage.monImage = this.monImageresize;
	        this.monJPanelTailleImage.repaint();
	        }
	    }
}
