// Workshop 1 part 3

import javax.swing.*;
import java.awt.*;
import java.io.*;
import java.awt.image.*;
import javax.imageio.*;

class ImageViewer extends JComponent
{
		// This will be our image
	BufferedImage image;

	public void ImageViewer()
	{
			// Set to null in constructor
		image = null;
	}

		// This function will load our image from a file
	public boolean loadfile(File file)
	{
		try
		{
				// Nice and easy: reads the file
				// and loads it into our image
			image = ImageIO.read(file);
			return true;
		}
		catch( IOException e )
		{
				// Something went wrong with the image loading
			return false;
		}
	}

		// As part of JComponent, this function is called
		// whenever the component needs to be drawn to the screen
	public void paint(Graphics g)
	{
		if( image != null ) // Draw our image, if we have one.
			g.drawImage(image,0,0,null);
	}

}

class part3
{
	public static void main(String[] args)
	{

			// This will be the file that we will be opening
		File file;

			// Create an instance of our image viewer
		ImageViewer viewer = new ImageViewer();

			// This is a very useful Java 2D class:
			// We create an instance of the file chooser...
		JFileChooser chooser = new JFileChooser();
			// ...and then tell it to appear to our user.
			// It will present the user with an "open dialog"
			// file browser / chooser window.
			// The user selects a file, and then the function returns.
		int chooser_result = chooser.showOpenDialog(null);

			// What we do next depends on what the user clicked
		switch(chooser_result)
		{
				// If the user clicked on the "Open" button:
			case JFileChooser.APPROVE_OPTION:
					// Retrieve the selected file
				file = chooser.getSelectedFile();
					// We can print something out to help us debug if need be
				System.out.println("Using file: " + file.getPath());
				break;
			default:
					// Something went wrong - perhaps the user clicked "Cancel"
				System.out.println("Open dialog did not return a file");
					// Returning from main will exit
				return;
		}


		// If program control reaches this point here, we have asked the user
		// to find a file for us to open, and we have retrieved that file.

			// Send the file to our image viewer
			// Our load function returns true if the file loaded okay...
		if( viewer.loadfile(file) )
			System.out.println("File loaded");
		else
		{
				// ... and false if it failed
			System.out.println("File failed to load");
			return;
		}


			// A JFrame is a framed window for our desktop.
			// We shall create one of these so that we can display our image.
			// The string we give it will appear as the window title
		JFrame frame = new JFrame("My Image Viewer");

			// In Java 2D, windows have multiple 'panes'.
			// We need to put our viewer on one of these to allow it to be displayed.
			// There are a variety of different panes available,
			// but we usually want to use the "content pane" as it's for general purpose use.
		frame.getContentPane().add(viewer);

			// Setting window size
		frame.setSize(640,480);

			// We need to specify what to do when the user clicks on the close widget.
			// We shall exit.
		frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );

			// Finally, make our window visible.
		frame.setVisible(true);
	}
}