
/** Affichage d'images : Exercise 2
 *
 *	@version 0.9  12/07/2001
 *	@author Pascal Vuylsteker
 */
 
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import javax.swing.*;
import java.awt.geom.*;


public class NewPuzzle extends JFrame	{	

	public NewPuzzle()	{
		setTitle("Here is a pretty picture");
		// setSize(600,400);
		
		
		JFileChooser chooser = new JFileChooser(); 

/*		ExtensionFileFilter filter = new ExtensionFileFilter(); 
		filter.addExtension("jpg"); 
		filter.addExtension("jpeg"); 
		filter.addExtension("gif"); 
		filter.addExtension("png"); 
		filter.setDescription("JPG, png & GIF Images"); 
		chooser.setFileFilter(filter); 	*/
		
		
		int returnVal = chooser.showOpenDialog(this); 
		if	(returnVal == JFileChooser.APPROVE_OPTION) 	{	
			fichier = chooser.getSelectedFile();
			System.out.println("You chose to open this file: " + fichier.getName());
			System.out.println("You chose to open this file: " + fichier.getAbsolutePath());
			
			
		} 
		
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		ShowPanel panel = new ShowPanel(fichier);
		Container contentPane = getContentPane();
		contentPane.add(panel);
		Dimension dim = panel.getSize();
		System.out.println("Frame x :" + dim.getWidth());
		System.out.println("Frame y :" + dim.getHeight());
		
		Toolkit	tk = Toolkit.getDefaultToolkit();
		int xscr = (int)tk.getScreenSize().getWidth();
		int yscr = (int)tk.getScreenSize().getHeight();

		setBounds((xscr-(int)dim.getWidth())/2, (yscr-(int)dim.getHeight()-20)/2, (int)dim.getWidth(), (int)dim.getHeight()+20);		
	}
	
	public static void main(String[] args)	{		
		NewPuzzle frame = new NewPuzzle();
		frame.show();
	}
	
private	File	fichier;	
}


class ShowPanel extends JPanel implements MouseListener, MouseMotionListener	{	
	ShowPanel(File f)	{	setBackground(Color.white);
		addMouseMotionListener(this);
        addMouseListener(this);
		
		
		setBackground(Color.white);
		fichier = f;
		name = fichier.getAbsolutePath();
		System.out.println("You chose to open this file: " + name);
		
		Toolkit	tk = Toolkit.getDefaultToolkit();
		
		img = tk.getImage(name);
		
		
		try	{
        	MediaTracker tracker = new MediaTracker(this);
        	tracker.addImage(img, 0);
        	tracker.waitForID(0);
        }
        catch ( Exception e ) {}
        
        x = img.getWidth(this);
		y = img.getHeight(this);
		
		xscr = tk.getScreenSize().getWidth();
		yscr = tk.getScreenSize().getHeight();
		System.out.println("Screen x :" + xscr);
		System.out.println("Screen y :" + yscr);
		System.out.println("Image  x :" + x);
		System.out.println("Image  y :" + y);
		
		double areascr = xscr*yscr;
		double areaimg = x*y;
		double areafr = 4*areaimg;
		double ratio = x/y;
		if (areafr > areascr/2)	{
			areafr = areascr/2;
			yfr = Math.sqrt(areafr/ratio);
			xfr = ratio * yfr;
			x = xfr/2;
			y = yfr/2;
		}	
		else	{
			xfr = x*2;
			yfr = y*2;
		}
		setSize((int)xfr,(int)yfr);
		
		xp = (xfr-x)/2;
		yp = (yfr-y)/2;

		System.out.println("Frame  xfr :" + xfr);
		System.out.println("Frame  yfr :" + yfr);
		System.out.println("Image  x :" + x);
		System.out.println("Image  y :" + y);
		System.out.println("Image  xp :" + xp);
		System.out.println("Image  yp :" + yp);
		
/*		bi = new BufferedImage((int)x, (int)y, BufferedImage.TYPE_INT_RGB);
		Graphics2D biContext2 = bi.createGraphics();
		biContext2.drawImage(img, 0, 0, null);
			
		// Raster myRaster = (Raster)getRaster(bi);
		// ColorModel myColorModel = getColorModel(bi);
		// Raster littleRaster = 
		// lbi = new BufferedImage(myColorModel, littleRaster, false, null);
		lbi1 = bi.getSubimage(0,0,(int)x/2,(int)y/2);
		try	{
        	MediaTracker tracker = new MediaTracker(this);
        	tracker.addImage(lbi1, 0);
        	tracker.waitForID(0);
        }
        catch ( Exception e )	{}
*/


//		img = img.getScaledInstance((int)x, (int)y, Image.SCALE_SMOOTH);
		img = img.getScaledInstance(200, 200, Image.SCALE_SMOOTH);
		try	{
        	MediaTracker tracker = new MediaTracker(this);
        	tracker.addImage(img, 0);
        	tracker.waitForID(0);
        }
        catch ( Exception e )	{}
		
		System.out.println("After Scaling...");
		System.out.println("Image  x :" + img.getWidth(this));
		System.out.println("Image  y :" + img.getHeight(this));

		rect = new Rectangle((int)xp, (int)yp, (int)x, (int)y);
		area = new Rectangle(0, 0, (int)xfr, (int)yfr);
		System.out.println("Rect : "+ rect);
		System.out.println("Area : "+ area);
		System.out.println("Ratio Rect : "+ (1.*rect.width)/rect.height + "  ... :"+ x/y);
		System.out.println("Ratio Area : "+ (1.*area.width)/area.height + "  ... :"+ xfr/yfr);
		System.out.println("Ratio Screen : "+ xscr/yscr);
		
			
// 		bi = new BufferedImage((int)x, (int)y, BufferedImage.TYPE_INT_RGB);
 		bi = new BufferedImage(200, 200, BufferedImage.TYPE_INT_RGB);
//		bi = (BufferedImage)createImage((int)x, (int)y);
		Graphics2D biContext = bi.createGraphics();
		biContext.drawImage(img, 0, 0, null);
		
		biContext.setPaint(Color.red);
		biContext.draw(new Line2D.Double(0,0,x,y));
			
		// Raster myRaster = (Raster)getRaster(bi);
		// ColorModel myColorModel = getColorModel(bi);
		// Raster littleRaster = 
		// lbi = new BufferedImage(myColorModel, littleRaster, false, null);
		lbi = bi.getSubimage(0,0,199,199);
		try	{
        	MediaTracker tracker = new MediaTracker(this);
        	tracker.addImage(lbi, 0);
        	tracker.waitForID(0);
        }
        catch ( Exception e )	{}
        
        System.out.println("bi width : "+bi.getWidth() );
        System.out.println("bi width : "+bi.getHeight() );
        System.out.println("lbi width : "+lbi.getWidth() );
        System.out.println("lbi width : "+lbi.getHeight() );

	} 
	

	/** Handles the event of the user pressing down the mouse button.	*/
    public void mousePressed(MouseEvent e)	{
		last_x = rect.x - e.getX();
		last_y = rect.y - e.getY();

		// Checks whether or not the cursor is inside of the rectangle
		// while the user is pressing the mouse.
		if ( rect.contains(e.getX(), e.getY()) )	{
			updateLocation(e);
		} else	{
			System.out.println("First position the cursor on the rectangle and then drag.");
			pressOut = true;
		}
    }
    
	/** Handles the event of a user dragging the mouse while holding
    	down the mouse button.
    	*/
    public void mouseDragged(MouseEvent e)	{
		if ( !pressOut )	{
			updateLocation(e);
        } else 	{
			System.out.println("First position the cursor on the rectangle and then drag.");
	    }
    }

	/** Handles the event of a user releasing the mouse button.	*/
	public void mouseReleased(MouseEvent e)	{	
		// Checks whether or not the cursor is inside of the rectangle
		// when the user releases the mouse button.
		if ( rect.contains(e.getX(), e.getY()) )	{
	 		updateLocation(e);
		} else	{
			System.out.println("First position the cursor on the rectangle and then drag.");
			pressOut = false;
		}
	}
	
	/** This method is required by MouseListener.	*/
	public void mouseMoved(MouseEvent e)	{}
	
	/** These methods are required by MouseMotionListener.	*/
	public void mouseClicked(MouseEvent e)	{}
	public void mouseExited(MouseEvent e)	{}
	public void mouseEntered(MouseEvent e)	{}
					 
	/** Updates the coordinates representing the location of the current rectangle.	*/
	public void updateLocation(MouseEvent e)	{
		rect.setLocation(last_x + e.getX(), last_y + e.getY());
			/*
			 * Updates the label to reflect the location of the
			 * current rectangle
			 * if checkRect returns true; otherwise, returns error message.
			 */
		if (checkRect())	{
			System.out.println("Rectangle located at " + rect.getX() + ", " + rect.getY());
			} else	{
				System.out.println("Please don't try to "+ " drag outside the area.");
			}
	
	repaint();
	}


	
	public void paintComponent(Graphics g)	{
		super.paintComponent(g);
		Graphics2D g2 = (Graphics2D) g;
		
		
        // Draws and fills the newly positioned rectangle.
		g2.setPaint(Color.red);
		g2.drawImage(lbi,rect.x,rect.y,null);

		g2.drawImage(bi,0,0,null);
		
//		g2.drawImage(lbi1,(int)x,(int)y,null);

		g.drawString("Bonjour...  "+name, 20, 20);
	}
	

    /*
     Checks if the rectangle is contained within the applet window.  If the rectangle
     is not contained withing the applet window, it is redrawn so that it is adjacent
     to the edge of the window and just inside the window.
    */

boolean checkRect()	{
	if (area == null)	{
		return false;
	}
	
	if(area.contains(rect))	{
		return true;
	}
	
	int new_x = rect.x;
	int new_y = rect.y;
	
	if((rect.x+rect.width)>area.getWidth())	{
		new_x = (int)area.getWidth()-rect.width+1;
	}
	if(rect.x < 0)	{
		new_x = -1;  
	}
	if((rect.y+rect.height )>area.getHeight())	{
		new_y = (int)area.getHeight()-rect.height+1;
	}
	if(rect.y < 0)	{
		new_y = -1;
	}
	rect.setLocation(new_x, new_y);
	return false;
}


// to memorize Image position
private	Rectangle rect,area;
// screen dimensions
private double 		xscr,yscr;
// image dimensions and position
private double 		x, y, xp, yp;
// frame/panel dimensions
private double 		xfr, yfr;
// Holds the coordinates of the user's last mousePressed event.
private int last_x, last_y;
// True if the user pressed, dragged or released the mouse outside of
// the rectangle; false otherwise.
boolean pressOut = false;   
private	Image img;
private	BufferedImage bi,lbi,lbi1,lbi2;
private Graphics2D big;
private String	name;
private File	fichier;
}