/*
 * opart1.java
 *
 * Created on 21 May 2004, 23:00
 */

package opartgui;
import com.sun.image.codec.jpeg.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import java.awt.image.*;
import java.io.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.filechooser.*;
import java.awt.image.BufferedImage;



/**
 *
 * @author  Jorden
 */
public class opart1 extends javax.swing.JFrame {
    //Initialise image classes
    protected static Brittania Brittania = new Brittania();
    protected static Fall Fall = new Fall();
    protected static Cataract Cataract = new Cataract();
    protected static Rect Rect = new Rect();
    protected static Rect2 Rect2 = new Rect2();
    protected static Ellipse Ellipse = new Ellipse();
    protected static Ellipse2 Ellipse2 = new Ellipse2();
    protected static Accident Accident = new Accident();
    protected static Crest Crest = new Crest();
    protected static Arrest Arrest = new Arrest();
    protected static Orient Orient = new Orient();
    protected static Pause Pause = new Pause();
    protected static Entice Entice = new Entice();

    //Initialise frames for wizard and shear and rotate
    protected JFrame wizardFrame;
    protected JFrame manipFrame;
    protected JFrame animFrame;

    //Initialise display panels
    protected static ImagePanel imagePanel = new ImagePanel();
    protected static AnimPanel animPanel = new AnimPanel();

    //Initialise object holder for option panes
    protected static JPanel OptionPanel = new JPanel();
    protected static JScrollPane OptionPane = new JScrollPane();
    
    //Initialise panel loader class
    private PanelLoader panelLoader = new PanelLoader();

    private int imagecase;
    private JFileChooser fc;
    static private String newline = "\n";
    String imagetype;

    /** Creates new form opart1 */
    public opart1(String it) {
        imagetype = it;
        initComponents();
        if (imagetype!=null)imageCaseChecker();
    }

    //Declaration of menu variables etc.
        private static JTextPane imageLabel = new javax.swing.JTextPane();
        private JMenuBar opartMenuBar = new javax.swing.JMenuBar();
        private JMenu fileMenu = new javax.swing.JMenu();
        private JMenuItem openJPGMenuItem = new javax.swing.JMenuItem();
        private JMenuItem saveMenuItem = new javax.swing.JMenuItem();
        private JMenuItem imageWizMenuItem = new javax.swing.JMenuItem();
        private JMenuItem closeMenuItem = new javax.swing.JMenuItem();
        private JMenu editMenu = new javax.swing.JMenu();
        private JMenuItem shearMenuItem = new javax.swing.JMenuItem();
        private JCheckBoxMenuItem invertMenuItem = new javax.swing.JCheckBoxMenuItem();
        private JMenu rotateMenu = new javax.swing.JMenu();
        private JMenuItem rotate45MenuItem = new javax.swing.JMenuItem();
        private JMenuItem rotateN45MenuItem = new javax.swing.JMenuItem();
        private JMenuItem rotate180MenuItem = new javax.swing.JMenuItem();
        private JCheckBoxMenuItem clippedMenuItem = new javax.swing.JCheckBoxMenuItem();
        private JCheckBoxMenuItem flickeringInvertMenuItem = new javax.swing.JCheckBoxMenuItem();

        private JMenu imageMenu = new javax.swing.JMenu();
        private JMenuItem fallMenuItem = new javax.swing.JMenuItem();
        private JMenuItem cataractMenuItem = new javax.swing.JMenuItem();
        private JMenuItem rectMenuItem = new javax.swing.JMenuItem();
        private JMenuItem rect2MenuItem = new javax.swing.JMenuItem();
        private JMenuItem ellipseMenuItem = new javax.swing.JMenuItem();
        private JMenuItem ellipse2MenuItem = new javax.swing.JMenuItem();
        private JMenuItem brittaniaMenuItem = new javax.swing.JMenuItem();
        private JMenuItem accidentMenuItem = new javax.swing.JMenuItem();
        private JMenuItem crestMenuItem = new javax.swing.JMenuItem();
        private JMenuItem arrestMenuItem = new javax.swing.JMenuItem();
        private JMenuItem OrientMenuItem = new javax.swing.JMenuItem();
        private JMenuItem pauseMenuItem = new javax.swing.JMenuItem();
        private JMenuItem enticeMenuItem= new javax.swing.JMenuItem();

        private JMenu animationMenu = new javax.swing.JMenu();
        private JMenuItem animation1MenuItem= new javax.swing.JMenuItem();
        private JMenuItem animation2MenuItem= new javax.swing.JMenuItem();
        private JMenuItem animation3MenuItem= new javax.swing.JMenuItem();
        private JMenuItem animation4MenuItem= new javax.swing.JMenuItem();
        private JMenuItem customanimationMenuItem= new javax.swing.JMenuItem();

        private JMenu helpMenu = new javax.swing.JMenu();
        private JMenuItem aboutMenuItem = new javax.swing.JMenuItem();
        private JMenuItem helpMenuItem = new javax.swing.JMenuItem();


    private void initComponents() {

        //Creates the GUI
        setBackground(Color.white);
        getContentPane().setLayout(null);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                exitForm(evt);
            }
        });

        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        setLocation(screenSize.width/2 - (400),
                    screenSize.height/2 - (340));
        setSize(830,680);
        setResizable(false);

        animPanel.setBackground(Color.white);
        getContentPane().add(animPanel);
        animPanel.setBounds(0, 0, 600, 600);

        imagePanel.setBackground(Color.white);
        getContentPane().add(imagePanel);
        imagePanel.setBounds(0, 0, 600, 600);

        imageLabel.setEditable(false);
        imageLabel.setBackground(new java.awt.Color(153, 153, 255));
        imageLabel.setBorder(new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.RAISED));
        imageLabel.setText("Image Name");
        getContentPane().add(imageLabel);
        imageLabel.setBounds(0, 600, 820, 20);

        opartMenuBar.setBackground(new java.awt.Color(153, 153, 255));
        opartMenuBar.setBorder(new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.RAISED));

        fileMenu.setBackground(new java.awt.Color(153, 153, 255));
        fileMenu.setText("File");
        openJPGMenuItem.setLabel("Open JPG");
        openJPGMenuItem.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                openJPGMenuItemMouseReleased(evt);
            }
        });
        fileMenu.add(openJPGMenuItem);

        saveMenuItem.setLabel("Save as JPG");
        saveMenuItem.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                saveMenuItemMouseReleased(evt);
            }
        });
        fileMenu.add(saveMenuItem);

        imageWizMenuItem.setLabel("Image Wizard");
        imageWizMenuItem.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                imageWizMenuItemMouseReleased(evt);
            }
        });
        fileMenu.add(imageWizMenuItem);
        
        closeMenuItem.setLabel("Exit");
        closeMenuItem.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                System.exit(0);
            }
        });
        fileMenu.add(closeMenuItem);

        opartMenuBar.add(fileMenu);

        clippedMenuItem.setLabel("Clipped");
        clippedMenuItem.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                clippedMenuItemMouseReleased(evt);
            }
        });

        editMenu.add(clippedMenuItem);


        editMenu.setBackground(new java.awt.Color(153, 153, 255));
        editMenu.setText("Edit");

        shearMenuItem.setLabel("Rotate and Shear");
        shearMenuItem.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                shearMenuItemMouseReleased(evt);
            }
        });

        editMenu.add(shearMenuItem);

        invertMenuItem.setLabel("Invert");
        invertMenuItem.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                invertMenuItemMouseReleased(evt);
            }
        });

        editMenu.add(invertMenuItem);

        rotateMenu.setLabel("Rotate");

        rotate45MenuItem.setLabel("Rotate 45");
        rotate45MenuItem.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                rotate45MenuItemMouseReleased(evt);
            }
        });

        rotateMenu.add(rotate45MenuItem);

        rotateN45MenuItem.setLabel("Rotate -45");
        rotateN45MenuItem.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                rotateN45MenuItemMouseReleased(evt);
            }
        });

        rotateMenu.add(rotateN45MenuItem);

        rotate180MenuItem.setLabel("Rotate 180");
        rotate180MenuItem.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                rotate180MenuItemMouseReleased(evt);
            }
        });

        rotateMenu.add(rotate180MenuItem);

        editMenu.add(rotateMenu);

        flickeringInvertMenuItem.setLabel("Flickering Invert");
        flickeringInvertMenuItem.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                flickeringInvertMenuItemMouseReleased(evt);
            }
        });

        editMenu.add(flickeringInvertMenuItem);

        opartMenuBar.add(editMenu);

        imageMenu.setBackground(new java.awt.Color(153, 153, 255));
        imageMenu.setText("Images");
        fallMenuItem.setText("Fall");
        fallMenuItem.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                fallMenuItemMouseReleased(evt);
            }
        });
        imageMenu.add(fallMenuItem);

        cataractMenuItem.setText("Cataract");
        cataractMenuItem.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                cataractMenuItemMouseReleased(evt);
            }
        });
        imageMenu.add(cataractMenuItem);

        rectMenuItem.setText("Rect");
        rectMenuItem.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                rectMenuItemMouseReleased(evt);
            }
        });
        imageMenu.add(rectMenuItem);

        rect2MenuItem.setText("Rect 2");
        rect2MenuItem.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                rect2MenuItemMouseReleased(evt);
            }
        });
        imageMenu.add(rect2MenuItem);

        ellipseMenuItem.setText("Ellipse");
        ellipseMenuItem.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                ellipseMenuItemMouseReleased(evt);
            }
        });
        imageMenu.add(ellipseMenuItem);

        ellipse2MenuItem.setText("Ellipse 2");
        ellipse2MenuItem.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                ellipse2MenuItemMouseReleased(evt);
            }
        });
        imageMenu.add(ellipse2MenuItem);

        brittaniaMenuItem.setText("Brittania");
        brittaniaMenuItem.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                brittaniaMenuItemMouseReleased(evt);
            }
        });
        imageMenu.add(brittaniaMenuItem);

        accidentMenuItem.setText("Accident");
        accidentMenuItem.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                accidentMenuItemMouseReleased(evt);
            }
        });
        imageMenu.add(accidentMenuItem);

        crestMenuItem.setText("Crest");
        crestMenuItem.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                crestMenuItemMouseReleased(evt);
            }
        });
        imageMenu.add(crestMenuItem);

        arrestMenuItem.setText("Arrest");
        arrestMenuItem.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                arrestMenuItemMouseReleased(evt);
            }
        });
        imageMenu.add(arrestMenuItem);

        OrientMenuItem.setText("Orient");
        OrientMenuItem.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                OrientMenuItemMouseReleased(evt);
            }
        });
        imageMenu.add(OrientMenuItem);

        pauseMenuItem.setText("Pause");
        pauseMenuItem.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                pauseMenuItemMouseReleased(evt);
            }
        });
        imageMenu.add(pauseMenuItem);

        enticeMenuItem.setText("Entice");
        enticeMenuItem.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                enticeMenuItemMouseReleased(evt);
            }
        });
        imageMenu.add(enticeMenuItem);

        opartMenuBar.add(imageMenu);

        animationMenu.setBackground(new java.awt.Color(153, 153, 255));
        animationMenu.setText("Animations");
        animation1MenuItem.setText("Animation 1");
        animation1MenuItem.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                animation1MenuItemMouseReleased(evt);
            }
        });
        animationMenu.add(animation1MenuItem);

        animation2MenuItem.setText("Animation 2");
        animation2MenuItem.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                animation2MenuItemMouseReleased(evt);
            }
        });
        animationMenu.add(animation2MenuItem);

        animation3MenuItem.setText("Animation 3");
        animation3MenuItem.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                animation3MenuItemMouseReleased(evt);
            }
        });
        animationMenu.add(animation3MenuItem);
        
        animation4MenuItem.setText("Animation 4");
        animation4MenuItem.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                animation4MenuItemMouseReleased(evt);
            }
        });
        animationMenu.add(animation4MenuItem);

        customanimationMenuItem.setText("Custom Animation");
        customanimationMenuItem.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                customanimationMenuItemMouseReleased(evt);
            }
        });
        animationMenu.add(customanimationMenuItem);


        opartMenuBar.add(animationMenu);

        helpMenu.setBackground(new java.awt.Color(153, 153, 255));
        helpMenu.setLabel("Help");
        aboutMenuItem.setLabel("About");
        aboutMenuItem.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                aboutMenuItemMouseReleased(evt);
            }
        });
        helpMenu.add(aboutMenuItem);

        helpMenuItem.setLabel("Help");
        helpMenuItem.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                helpMenuItemMouseReleased(evt);
            }
        });
        helpMenu.add(helpMenuItem);

        opartMenuBar.add(helpMenu);

        setJMenuBar(opartMenuBar);

        //pack();
    }//GEN-END:initComponents

    public void imageCaseChecker(){
        if (imagetype.equals ("Cataract")) {imagecase = 1;}
	if (imagetype.equals ("Fall")) {imagecase = 2;}
	if (imagetype.equals ("Brittania")) {imagecase = 3;}
	if (imagetype.equals ("Rect")) {imagecase = 4;}
	if (imagetype.equals ("Rect2")) {imagecase = 5;}
	if (imagetype.equals ("Ellipse")) {imagecase = 6;}
	if (imagetype.equals ("Ellipse2")) {imagecase = 7;}
	if (imagetype.equals ("Accident")) {imagecase = 8;}
	if (imagetype.equals ("Crest")) {imagecase = 9;}
	if (imagetype.equals ("Arrest")) {imagecase = 10;}
	if (imagetype.equals ("Orient")) {imagecase = 11;}
	if (imagetype.equals ("Pause")) {imagecase = 12;}
	if (imagetype.equals ("Entice")) {imagecase = 13;}
	doShow();
    }

    private void saveMenuItemMouseReleased(java.awt.event.MouseEvent evt) {
        doSave(imagePanel.displayImage);
    }

    public void doSave(BufferedImage bi){
           //Initiates the file chooser.
           fc = new JFileChooser();
           //Adds a  file filter and disables All file filter.
           fc.addChoosableFileFilter(new ImageFilter());
           fc.setAcceptAllFileFilterUsed(false);
           int returnVal = fc.showSaveDialog(opart1.this);
           //Returns the name of the file to load
           if (returnVal == JFileChooser.APPROVE_OPTION) {
                File file = fc.getSelectedFile();
                String filename = file.toString();
                if (!file.isFile()){
                   filename = filename +".jpg";
                }
                saveImageAsJPEG(bi, filename);
           }
    }

    private void openJPGMenuItemMouseReleased(java.awt.event.MouseEvent evt) {
        //Inititates the file chooser.
        if (fc == null) {
            fc = new JFileChooser();

	    //Add a file filter and disable the default file filter.
            fc.addChoosableFileFilter(new ImageFilter());
            fc.setAcceptAllFileFilterUsed(false);
            //fc.setFileView(new ImageFileView());
	    //Add the preview pane.
            fc.setAccessory(new ImagePreview(fc));
        }
        int returnVal = fc.showDialog(opart1.this,
                                      "Open");

        //Save the file to the name selected
        if (returnVal == JFileChooser.APPROVE_OPTION){
           File file = fc.getSelectedFile();
           String filename = file.toString();
           System.out.println(filename);
           readAsBufferedImage(filename);
        }
        //Reset the file chooser
        fc.setSelectedFile(null);
    }

    private void invertMenuItemMouseReleased(java.awt.event.MouseEvent evt) {
        //Calls invert on the image dislayed
        imagePanel.invert();
    }

    private void clippedMenuItemMouseReleased(java.awt.event.MouseEvent evt) {
        //Applies a clip to the image displayed
        imagePanel.setClipped();
    }

    private void rotate45MenuItemMouseReleased(java.awt.event.MouseEvent evt) {
            //Rotestes the image by 45 degrees
            imagePanel.rotate((0-45)*(Math.PI/180.0));
    }

    private void rotateN45MenuItemMouseReleased(java.awt.event.MouseEvent evt) {
            //Rotestes the image by -45 degrees
            imagePanel.rotate(45*(Math.PI/180.0));
    }

    private void rotate180MenuItemMouseReleased(java.awt.event.MouseEvent evt) {
            //Rotestes the image by 180 degrees
            imagePanel.rotate(180*(Math.PI/180.0));
    }


    private void shearMenuItemMouseReleased(java.awt.event.MouseEvent evt) {
            // Initialises a frame to show shear and rotate options
            if (manipFrame==null){
               manipFrame = new JFrame();
               ManipUI manipui = new ManipUI();
               manipFrame.getContentPane().add(manipui);
               manipFrame.pack();
            }
            //Shows frame with shear and rotate options
            manipFrame.show();
    }

    private void imageWizMenuItemMouseReleased(java.awt.event.MouseEvent evt) {
            // Initialises a frame to show image wizard
            if (wizardFrame==null){
               wizardFrame = new JFrame();
               ImageWizard wiz = new ImageWizard();
               wizardFrame.getContentPane().add(wiz);
               wizardFrame.pack();
            }
            //Shows frame containing image wizard
            wizardFrame.show();
    }

    private void flickeringInvertMenuItemMouseReleased(java.awt.event.MouseEvent evt) {
        //Calls flickering invert which cycles the image between inverted and not inverted
        imagePanel.flickeringInvert();
    }

    private void cataractMenuItemMouseReleased(java.awt.event.MouseEvent evt) {
        //Sets the Image case and shows the image
        imagecase =1;
        doShow();
    }

    private void fallMenuItemMouseReleased(java.awt.event.MouseEvent evt) {
        //Sets the Image case and shows the image
        imagecase =2;
        doShow();
    }

    private void brittaniaMenuItemMouseReleased(java.awt.event.MouseEvent evt) {
        //Sets the Image case and shows the image
        imagecase =3;
        doShow();
    }

    private void rectMenuItemMouseReleased(java.awt.event.MouseEvent evt) {
        //Sets the Image case and shows the image
        imagecase =4;
        doShow();
    }

    private void rect2MenuItemMouseReleased(java.awt.event.MouseEvent evt) {
        //Sets the Image case and shows the image
        imagecase =5;
        doShow();
    }

    private void ellipseMenuItemMouseReleased(java.awt.event.MouseEvent evt) {
        //Sets the Image case and shows the image
        imagecase =6;
        doShow();
    }
    private void ellipse2MenuItemMouseReleased(java.awt.event.MouseEvent evt) {
        //Sets the Image case and shows the image
        imagecase =7;
        doShow();
    }

    private void accidentMenuItemMouseReleased(java.awt.event.MouseEvent evt) {
        //Sets the Image case and shows the image
        imagecase =8;
        doShow();
    }

    private void crestMenuItemMouseReleased(java.awt.event.MouseEvent evt) {
        //Sets the Image case and shows the image
        imagecase =9;
        doShow();
    }

    private void arrestMenuItemMouseReleased(java.awt.event.MouseEvent evt) {
        //Sets the Image case and shows the image
        imagecase =10;
        doShow();
    }

    private void OrientMenuItemMouseReleased(java.awt.event.MouseEvent evt) {
        //Sets the Image case and shows the image
        imagecase =11;
        doShow();
    }

    private void pauseMenuItemMouseReleased(java.awt.event.MouseEvent evt) {
        //Sets the Image case and shows the image
        imagecase =12;
        doShow();
    }

    private void enticeMenuItemMouseReleased(java.awt.event.MouseEvent evt) {
        //Sets the Image case and shows the image
        imagecase =13;
        doShow();
    }

    private void animation1MenuItemMouseReleased(java.awt.event.MouseEvent evt) {
        doAnimPanelShow();
        animPanel.frameNumber = 0;
        animPanel.NUM_FRAMES = 16;
        animPanel.location = 0;
        animPanel.images = new ImageIcon[16];
        animPanel.animateStart();
    }

    private void animation2MenuItemMouseReleased(java.awt.event.MouseEvent evt) {
        doAnimPanelShow();
        animPanel.frameNumber = 0;
        animPanel.NUM_FRAMES = 15;
        animPanel.location = 1;
        animPanel.images = new ImageIcon[15];
        animPanel.animateStart();
    }

    private void animation3MenuItemMouseReleased(java.awt.event.MouseEvent evt) {
        doAnimPanelShow();
        animPanel.frameNumber = 0;
        animPanel.NUM_FRAMES = 21;
        animPanel.location = 2;
        animPanel.images = new ImageIcon[21];
        animPanel.animateStart();
    }
    
    private void animation4MenuItemMouseReleased(java.awt.event.MouseEvent evt) {
        doAnimPanelShow();
        animPanel.frameNumber = 0;
        animPanel.NUM_FRAMES = 40;
        animPanel.location = 3;
        animPanel.images = new ImageIcon[40];
        animPanel.animateStart();
    }

    private void customanimationMenuItemMouseReleased(java.awt.event.MouseEvent evt) {
        doAnimPanelShow();
        animPanel.frameNumber = 0;
        animPanel.NUM_FRAMES = 10;
        animPanel.location = 4;
        animPanel.images = new ImageIcon[10];
        animPanel.animateStart();
    }

    private void aboutMenuItemMouseReleased(java.awt.event.MouseEvent evt) {
        //Shows the about window
        SplashWindow splash = new SplashWindow(new javax.swing.JFrame());
        splash.show();
    }

    private void helpMenuItemMouseReleased(java.awt.event.MouseEvent evt) {
        // Launches the Help file in browser window
        try {
                Runtime.getRuntime().exec("cmd /c start  opartgui/web/index.htm");
        }catch(IOException ie) {
                System.out.println("Runtime Error Initialising Browser");
        }
    }


    /** Exit the Application */
    private void exitForm(java.awt.event.WindowEvent evt) {
        System.exit(0);
    }
    
    public void doPaneShow(){
           // Shows the Option Pane relevant to the image loaded
           getContentPane().add(OptionPane);
           OptionPane.setBounds(600, 0, 220, 600);
    }

    public static void doAnimPanelShow(){
           //Shows the animation panel if animation called
           animPanel.animateStop();
           animPanel.hide();
           imagePanel.hide();
           animPanel.show();
           OptionPane.hide();
           OptionPanel.setBackground(new java.awt.Color(153, 153, 255));
           OptionPanel.setBorder(new javax.swing.border.EtchedBorder());
           OptionPanel.setPreferredSize(new java.awt.Dimension(200, 580));
           OptionPane.setViewportView(OptionPanel);
           imageLabel.setText("Preset Animation displayed");
           OptionPane.show();
    }

    public static void doImagePanelShow(){
           //Shows the image panel when animation stopped or image modified
           animPanel.animateStop();
           animPanel.hide();
           imagePanel.show();
    }

    public void doShow() {
      //Shows the chosen image and its option pane
      switch (imagecase){
               case 1:     imageLabel.setText("Cataract");
                           imagePanel.doDisplay(Cataract.imageDraw());
                           doImagePanelShow();
                           OptionPane.hide();
                           OptionPane = panelLoader.cataractPanelLoad();
                           OptionPane.show();
                           doPaneShow();
                           break;
               case 2:     imageLabel.setText("Fall");
                           imagePanel.doDisplay(Fall.imageDraw());
                           doImagePanelShow();
                           OptionPane.hide();
                           OptionPane = panelLoader.fallPanelLoad();
                           OptionPane.show();
                           doPaneShow();break;
               case 3:     imageLabel.setText("Brittania");
                           imagePanel.doDisplay(Brittania.imageDraw(false));
                           doImagePanelShow();
                           OptionPane.hide();
                           OptionPane = panelLoader.brittaniaPanelLoad();
                           OptionPane.show();
                           doPaneShow();
                           break;
               case 4:     imageLabel.setText("Rect");
                           imagePanel.doDisplay(Rect.imageDraw());
                           doImagePanelShow();
                           OptionPane.hide();
                           OptionPane = panelLoader.rectPanelLoad();
                           OptionPane.show();
                           doPaneShow();
                           break;
               case 5:     imageLabel.setText("Rect2");
                           imagePanel.doDisplay(Rect2.imageDraw());
                           doImagePanelShow();
                           OptionPane.hide();
                           OptionPane = panelLoader.rect2PanelLoad();
                           OptionPane.show();
                           doPaneShow();
                           break;
               case 6:     imageLabel.setText("Ellipse");
                           imagePanel.doDisplay(Ellipse.imageDraw());
                           doImagePanelShow();
                           OptionPane.hide();
                           OptionPane = panelLoader.ellipsePanelLoad();
                           OptionPane.show();
                           doPaneShow();
                           break;
               case 7:     imageLabel.setText("Ellipse2");
                           imagePanel.doDisplay(Ellipse2.imageDraw());
                           doImagePanelShow();
                           OptionPane.hide();
                           OptionPane = panelLoader.ellipse2PanelLoad();
                           OptionPane.show();
                           doPaneShow();
                           break;
               case 8:     imageLabel.setText("Accident");
                           imagePanel.doDisplay(Accident.imageDraw());
                           doImagePanelShow();
                           OptionPane.hide();
                           OptionPane = panelLoader.accidentPanelLoad();
                           OptionPane.show();
                           doPaneShow();
                           break;
               case 9:     imageLabel.setText("Crest");
                           imagePanel.doDisplay(Crest.imageDraw());
                           doImagePanelShow();
                           OptionPane.hide();
                           OptionPane = panelLoader.crestPanelLoad();
                           OptionPane.show();
                           doPaneShow();
                           break;
               case 10:    imageLabel.setText("Arrest");
                           imagePanel.doDisplay(Arrest.imageDraw());
                           doImagePanelShow();
                           OptionPane.hide();
                           OptionPane = panelLoader.arrestPanelLoad();
                           OptionPane.show();
                           doPaneShow();
                           break;
               case 11:    imageLabel.setText("Orient");
                           imagePanel.doDisplay(Orient.imageDraw());
                           doImagePanelShow();
                           OptionPane.hide();
                           OptionPane = panelLoader.orientPanelLoad();
                           OptionPane.show();
                           doPaneShow();
                           break;
               case 12:    imageLabel.setText("Pause");
                           imagePanel.doDisplay(Pause.imageDraw());
                           doImagePanelShow();
                           OptionPane.hide();
                           OptionPane = panelLoader.pausePanelLoad();
                           OptionPane.show();
                           doPaneShow();
                           break;
               case 13:    imageLabel.setText("Entice");
                           imagePanel.doDisplay(Entice.imageDraw());
                           doImagePanelShow();
                           OptionPane.hide();
                           break;
               default:    break;
        }
    }


    public static void saveImageAsJPEG(BufferedImage bi, String filename){
           //Uses jpeg codec to save image displayed
           try{
               ByteArrayOutputStream boutstream = new ByteArrayOutputStream();
               JPEGImageEncoder enc = JPEGCodec.createJPEGEncoder(boutstream);
               enc.encode(bi);
               FileOutputStream fimage = new FileOutputStream(new File(filename));
               boutstream.writeTo(fimage);
               fimage.close();
           }
           catch(Exception e){
                System.out.println(e);
           }
    }

    public static void readAsBufferedImage(String filename){
           //reads in a JPG image as a buffered image and displays it
           try{
               FileInputStream fis = new FileInputStream(filename);
               JPEGImageDecoder decoder = JPEGCodec.createJPEGDecoder(fis);
               BufferedImage bi = decoder.decodeAsBufferedImage();
               imagePanel.doDisplay(bi);
               OptionPane.hide();
               OptionPanel.setBackground(new java.awt.Color(153, 153, 255));
               OptionPanel.setBorder(new javax.swing.border.EtchedBorder());
               OptionPanel.setPreferredSize(new java.awt.Dimension(200, 580));
               OptionPane.setViewportView(OptionPanel);
               imageLabel.setText("Non-parametric JPG displayed");
               OptionPane.show();
           }
           catch(Exception e){
                System.out.println(e);
           }
    }
    }
