/*
 * @(#)Viewer.java 1.0 99/10/4
 * Copyright (c) 1999 Larry Rodrigues
 */
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.io.*;
import javax.swing.*;
import javax.swing.event.*;
/**
   * @version 1.0  10 April 1999
   * @author Lawrence Rodrigues
   */
public class Viewer extends ApplicationFrame {
   ImagePanel viewer;
   JSlider brightSlider;
   JTextField brightField;
   float prevbright = (float)1.0;
   int count = 0;
   CropImage crp;
   public Viewer() {
      setJMenuBar(new ViewerMenuBar());
   }
   public Image loadImage(String filename){
      Image img = Toolkit.getDefaultToolkit().getImage(filename);
      try {
          MediaTracker tracker = new MediaTracker(this);
          tracker.addImage(img, 0);
          tracker.waitForID(0);
      } catch (Exception e) {return null;}
      return img;
   }

   public void loadAndDisplay(String filename){
      Image img = loadImage(filename);
      displayImage(img);
   }


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

   public void displayImage(Image img, int width, int height) {
      viewer = new ImagePanel(img, width, height);
      crp = new CropImage(viewer);
      viewer.addMouseListener(crp);
      viewer.addMouseMotionListener(crp);

      viewer.setPreferredSize(new Dimension(width, height));
      Container cp = getContentPane();
      GridBagLayout gb = new GridBagLayout() ;
      GridBagConstraints c = new GridBagConstraints();
      cp.setLayout(gb);
      c.weighty = 1.0; c.weightx = 1.0;
      c.insets = new Insets(0,1, 0,10);
      c.fill = GridBagConstraints.BOTH;
      c.gridwidth = GridBagConstraints.REMAINDER;
      gb.setConstraints(viewer, c);
      cp.add(viewer);
      c.weighty = 0.05; c.weightx = 1.0;
      c.fill = GridBagConstraints.HORIZONTAL;
      JPanel bp = createBrightnessPanel();
      gb.setConstraints(bp, c);
      cp.add(bp);
      pack();
      setSize(new Dimension(width, height+80));
      show();
      viewer.repaint();
   }

   public JPanel createBrightnessPanel (){
        int minValue = 10;
        int maxValue =100;
        int brightStartValue = 75;
        brightField = new JTextField(5);
        brightField.setText(Integer.toString(brightStartValue));
        brightSlider = new JSlider(minValue,maxValue, brightStartValue);
        int majorTickSpacing = 10;
        int minorTickSpacing = 2;
        brightSlider.setMajorTickSpacing(majorTickSpacing);
        brightSlider.setMinorTickSpacing(minorTickSpacing);
        brightSlider.setExtent(minorTickSpacing);
        brightSlider.setPaintTicks(true);
        brightSlider.setPaintLabels(true);

        brightField.addActionListener(
           new ActionListener() {
              public void actionPerformed(ActionEvent e){
                  try {
                    String str = ((JTextField)e.getSource()).getText();
                    int value = (Integer.valueOf(str)).intValue();
                    brightSlider.setValue((int)value);
                  } catch (Exception e1){}
              }
           }
       );

       brightSlider.addChangeListener(
            new ChangeListener(){
              public void stateChanged(ChangeEvent e){
                 Object obj = e.getSource();
                 if(obj instanceof JSlider) {
                    JSlider jsr = (JSlider)obj;
                    int bright = ((JSlider)(e.getSource())).getValue();
                    brightField.setText(Integer.toString(bright));
                 }
              }
            }
       );

       JButton apply = new JButton("Apply");
       apply.addActionListener(
          new ActionListener() {
              public void actionPerformed(ActionEvent e){
                 int value = brightSlider.getValue();
                 viewer.setBrightness(value);
             }
          }
       );

       JPanel brightpan = new JPanel();
       GridBagLayout gb = new GridBagLayout() ;
       GridBagConstraints c = new GridBagConstraints();
       brightpan.setLayout(gb);
       c.weighty = 1.0; c.weightx = 1.0;
       c.insets = new Insets(0,1, 0,10);
       c.fill = GridBagConstraints.HORIZONTAL;
       JLabel lab = new JLabel("Brightness");
       gb.setConstraints(lab, c);
       brightpan.add(lab);
       gb.setConstraints(brightSlider, c);
       brightpan.add(brightSlider);
       gb.setConstraints(brightField, c);
       brightpan.add(brightField);
       c.gridwidth = GridBagConstraints.REMAINDER;
       gb.setConstraints(apply, c);
       brightpan.add(apply);
       return brightpan;
    }

    protected void printContents() {
       java.util.Properties pr = new java.util.Properties();
       pr.put("awt.print.orientation", "landscape");
       pr.put("awt.print.numCopies", new Integer(2));
       PrintJob pj = Toolkit.getDefaultToolkit().getPrintJob(this, "Print", pr);
       Graphics printG = pj.getGraphics();
       if(printG==null) return;
       viewer.paint(printG);
       printG.dispose();
       pj.end();
   }

   public class ViewerMenuBar extends AppMenuBar {
      public JMenu createPrintMenu(){
         JMenuItem printActual = new JMenuItem("Actual size");
         JMenuItem printFitPage = new JMenuItem("Fit page");
         PrintAdapter pa = new PrintAdapter(this);
         printActual.addActionListener(pa);
         JMenu printMenu = new JMenu("Print");
         printMenu.add(printActual);
         printMenu.add(printFitPage);
         add(printMenu);
         return printMenu;
      }
  }

   public static void  main(String[] args){
     Viewer ip = new Viewer();
     if(args.length <1){
        System.out.println("Enter a path name for the image ");
        System.exit(0);
     }
     ip.loadAndDisplay(args[0]);
   }
  
}
