/**
 * @(#) QuantAnalyzer.java 1.0 99/10/4
 * Copyright (c) 1999 Larry Rodrigues
 */
package app;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.io.*;
import javax.swing.*;
import javax.swing.event.*;
import java.awt.color.*;
import java.util.*;
import com.vistech.util.*;
import vis.beans.plotter.*;
import java.awt.geom.*;
import com.vistech.roi.*;
import com.sun.image.codec.jpeg.*;

import java.beans.*;
 /** An ROI application developed using Java 2D classes. Because this application uses the JPEG codec, it takes
   * only JPEG images.
   * @version 1.0  10 April 1999
   * @author Lawrence Rodrigues
   */
public class QuantAnalyzer extends ApplicationFrame {
    ROIManager roiManager;

   public QuantAnalyzer() {
      setTitle("Quantitative Analysis");
      ViewerMenuBar viewerMenuBar = new ViewerMenuBar();
      setJMenuBar((JMenuBar)viewerMenuBar);
   }

   public static BufferedImage readAsBufferedImage(String filename) {
      try {
         FileInputStream fis = new FileInputStream(filename);
         JPEGImageDecoder decoder = JPEGCodec.createJPEGDecoder(fis);
         BufferedImage bi = decoder.decodeAsBufferedImage();
         return bi;
      } catch(Exception e) {
         System.out.println(e);
         return null;
      }
   }

   public void loadAndDisplay(String filename){
      BufferedImage img = readAsBufferedImage(filename);
      displayImage(img);
   }

   public void displayImage(BufferedImage img) {
      int width = img.getWidth();
      int height = img.getHeight();
      displayImage(img, width, height);
   }

   public void displayImage(BufferedImage img, int width, int height) {
      if(img == null) return;
      roiManager = new ROIManager();
      JPanel pan = roiManager.createUI(img);

      Container cp = getContentPane();
      getContentPane().setLayout(new GridLayout(1,1));
      cp.add(pan);
      pack();
      setSize(roiManager.getViewerSize());
      show();
  }
  public class ViewerMenuBar extends ApplicationFrame.AppMenuBar {
      public ViewerMenuBar(){
         // createParamsMenu();
      }
     public JMenu createFileMenu() {
        JMenu fileMenu = new JMenu("File");
             JMenuItem exit = new JMenuItem("Exit");
             exit.addActionListener(
               new ActionListener(){
                  public void actionPerformed(ActionEvent e){
                     quit();
                 }
               }
             );
             JMenuItem reset = new JMenuItem("Reset ROI");
             reset.addActionListener(
                new ActionListener() {
                  public void actionPerformed(ActionEvent e){
                     roiManager.reset();
                  }
                }
             );
             fileMenu.add(reset);
             fileMenu.addSeparator();
             fileMenu.add(exit);
             add(fileMenu);
             return fileMenu;
         }

          public JMenu createParamsMenu(){
            JMenu paramsMenu = new JMenu("Params");
            JMenuItem clr = new JMenuItem("Color");
            clr.addActionListener(
                new ActionListener() {
                  public void actionPerformed(ActionEvent e){

                  }
                }
             );
             paramsMenu.add(clr);
             add(paramsMenu);
             return paramsMenu;
         }
   }
   public static void  main(String[] args){
      QuantAnalyzer ip = new QuantAnalyzer();
      if(args.length <1) {
         System.out.println("Enter a valid JPEG file name");
         System.exit(0);
      }
      ip.loadAndDisplay(args[0]);
   }
 }
