// ImageDisplay.java
//              demonstrate display of .gif  .jpg  .png  images 

import java.awt.*;
import java.awt.event.*;
import java.text.*;
import java.io.*;
import javax.swing.*;

public class ImageDisplay extends JFrame
{
  MyJPanel panel;
  private JDesktopPane theDesktop;
  ImageIcon imageActive = new ImageIcon(" "); // one of three in this program
  Dimension dimActive;
  int Pwidth = 600;
  int Pheight = 500;
  
  public ImageDisplay()
  {
    setTitle("ImageDisplay demonstration");
    setSize(Pwidth,Pheight);
    setBackground(Color.white);
    setForeground(Color.black);

    // create menu bar and attach it to AwtMenuTest window
    JMenuBar bar = new JMenuBar();  
    setJMenuBar(bar);  

    // set up Image menu
    JMenu imageMenu = new JMenu("Image");
    bar.add(imageMenu);  // add File menu to menu bar
    // set up image types
    JMenuItem gifItem = new JMenuItem(".gif");
    imageMenu.add(gifItem);
    JMenuItem jpgItem = new JMenuItem(".jpg");
    imageMenu.add(jpgItem);
    JMenuItem printItem = new JMenuItem(".png");
    imageMenu.add(printItem);

    // set up display area
    theDesktop = new JDesktopPane();
    getContentPane().add(theDesktop);
    // Create internal frame
    JInternalFrame frame = new JInternalFrame(
        "Display area", true, true, true, true);
    // attach panel to internal frame
    Container container = frame.getContentPane();
    panel = new MyJPanel();
    container.add(panel, BorderLayout.CENTER);
    // set size of internal frame to size of its contents
    frame.pack(); // uses class Dimension
    theDesktop.add(frame);
    panel.addMouseListener (new mousePressHandler());
    frame.setVisible(true);

    gifItem.addActionListener(new ActionListener()
          {public void actionPerformed(ActionEvent event)
            { System.out.println("Image p1.gif");
              // load image
              imageActive = new ImageIcon( "p1.gif" );
              dimActive = new Dimension( imageActive.getIconWidth(),
                                         imageActive.getIconHeight() );  
              System.out.println("imageGif dimension = "+dimActive); // debug print
              panel.repaint();
          }});
    jpgItem.addActionListener(new ActionListener()
          {public void actionPerformed(ActionEvent event)
            { System.out.println("Image skull.jpg");
              // load image
              imageActive = new ImageIcon( "skull.jpg" );
              dimActive = new Dimension( imageActive.getIconWidth(),
                                         imageActive.getIconHeight() );  
              System.out.println("imageJpg dimension = "+dimActive); // debug print
              panel.repaint();
          }});
    printItem.addActionListener(new ActionListener()
          {public void actionPerformed(ActionEvent event)
            { System.out.println("Image chess2.png");
              // load image
              imageActive = new ImageIcon( "chess2.png" );
              dimActive = new Dimension( imageActive.getIconWidth(),
                                         imageActive.getIconHeight() );  
              System.out.println("imagePng dimension = "+dimActive); // debug print
              panel.repaint();
          }});
    addWindowListener(new WindowAdapter()
    {
      public void windowClosing(WindowEvent e)
      {
        System.exit(0);
      }
    });
 
    setVisible(true);
  } // end constructor ImageDisplay

  class mousePressHandler extends MouseAdapter
  {
    public void mousePressed (MouseEvent e)
    {

      // for future use

      panel.repaint();
    }
  } // end mousePressHandler

  class MyJPanel extends JPanel
  {
    ImageIcon imageLocal;

    public void paintComponent(Graphics g)
    {
      g.clearRect(0, 0, Pwidth, Pheight); // cover non-erase bug
      g.setColor(Color.blue);
      g.drawString("make menu selection", Pwidth-100, Pheight-100);

      super.paintComponent( g );
      // display image
	  imageLocal = imageActive;
      imageLocal.paintIcon( this, g, 0, 0 );
    } // end paintComponent

    // return dimensions for sizing
    public Dimension getPreferredSize()
    {
      return new Dimension(Pwidth-15, Pheight-90);
    }
  } // end MyJPanel

  public static void main (String[] args) // no args expected
  {
    new ImageDisplay();
  } // end main
} // end class ImageDisplay

