package yi.iu.ui;

import java.awt.*;
import java.awt.image.*;
import java.awt.event.*;
import yi.util.io.ImageReader;

/**
 * <p>Title: </p>
 * <p>Description: </p>
 * <p>Copyright: Copyright (c) 2002</p>
 * <p>Company: </p>
 * @author unascribed
 * @version 1.0
 */

public class ImagePanel extends Panel {
  /**
   * the Image Canvas containing the image
   */
  ImageCanvas m_imageCanvas;
  /**
   * Left message label
   */
  TextField m_lMsgLabel;
  /**
   * Right message label
   */
  TextField m_rMsgLabel;
  /**
   * Status message label
   */
  TextField m_statusLabel;
  /**
   * (R,G,B) Image data stored in an array
   */
  int[] m_imageData;
  /**
   * Image Dimension
   */
  Dimension m_imageSize = new Dimension();

  /**
   * Construct Image Panel with the image canvas size changed according to the
   * image size
   *
   * @param image the image to display
   */
  public ImagePanel(Image image) {
    m_imageCanvas = new ImageCanvas(image);
    m_imageData = ImageReader.getPixels(image, m_imageSize);
    init();
  }

  /**
   * Contruct Image Panel with defined image display size
   *
   * @param image image to display
   * @param imageWidth image display width
   * @param imageHeight image display height
   */
  public ImagePanel(Image image, int imageWidth, int imageHeight) {
    m_imageCanvas = new ImageCanvas(image, imageWidth, imageHeight);
    m_imageData = ImageReader.getPixels(image, m_imageSize);
    init();
  }

  public ImagePanel(int[] imageData, int imageWidth, int imageHeight) {
    m_imageData = imageData;
    m_imageSize.width = imageWidth;
    m_imageSize.height = imageHeight;
    // construct the image from the array
    Image image = createImage(new MemoryImageSource(imageWidth,
        imageHeight, imageData, 0, imageWidth));
    m_imageCanvas = new ImageCanvas(image, imageWidth, imageHeight);
    init();
  }

  /**
   * Initialize GUI component, including labels and panels
   */
  void init() {
    // define the labels
    m_lMsgLabel = new TextField(15);
    m_lMsgLabel.setEditable(false);
    m_lMsgLabel.setBackground(Color.lightGray);
    m_rMsgLabel = new TextField(15);
    m_rMsgLabel.setEditable(false);
    m_rMsgLabel.setBackground(Color.lightGray);
    m_statusLabel = new TextField("Size: " +
      m_imageSize.width + " x " + m_imageSize.height);
    m_statusLabel.setEditable(false);
    m_statusLabel.setBackground(Color.lightGray);


    // add Mouse(Motion) Listener to image canvas and add it to the image panel
    m_imageCanvas.addMouseListener(new MouseAdapter() {
      public void MousePressed(MouseEvent e) {
        MousePressedOnImage(e);
      }
    });
    m_imageCanvas.addMouseMotionListener(new MouseMotionAdapter() {
      public void mouseMoved(MouseEvent e) {
        MouseMovedOnImage(e);
      }
    });


    this.setLayout(new GridBagLayout());
    // image canvas is on the top
    this.add(m_imageCanvas, new GridBagConstraints(0,0,2,1,
      1,1,
      GridBagConstraints.CENTER,
      GridBagConstraints.NONE,
      new Insets(0,0,0,0), 0, 0));
    this.add(m_lMsgLabel, new GridBagConstraints(0,1,1,1,
      1,0,
      GridBagConstraints.WEST,
      GridBagConstraints.HORIZONTAL,
      new Insets(0,0,0,0), 0, 0));
    this.add(m_rMsgLabel, new GridBagConstraints(1,1,1,1,
      1,0,
      GridBagConstraints.EAST,
      GridBagConstraints.HORIZONTAL,
      new Insets(0,0,0,0), 0, 0));
    this.add(m_statusLabel, new GridBagConstraints(0,2,2,1,
      1,0,
      GridBagConstraints.WEST,
      GridBagConstraints.HORIZONTAL,
      new Insets(0,0,0,0), 0, 0));
  }

  /**
   * Do nothing when Mouse Pressed on the image (mainly for override)
   *
   * @param e the mouse event
   */
  public void MousePressedOnImage(MouseEvent e) {
  }

  /**
   * Display the pixel info when Mouse moved on the image
   *
   * @param e
   */
  public void MouseMovedOnImage(MouseEvent e) {
    int x = e.getX(), y = e.getY();
    int rgb = m_imageData[y * m_imageSize.width + x];
    int r = (rgb >> 16) & 0xff;
    int g = (rgb >>  8) & 0xff;
    int b = rgb & 0xff;
    m_lMsgLabel.setText("(" + x + "," + y + ")");
    m_rMsgLabel.setText(Integer.toString(r) + "," + g + "," +b);
  }

  public static void main(String[] args) {
    // get the image first
    Image image = Toolkit.getDefaultToolkit().getImage("t.gif");

    Frame f = new Frame("Image Panel");
    f.add(new ImagePanel(image, 400, 400));
    f.addWindowListener(new WindowAdapter() {
      public void windowClosing(WindowEvent e) {
        System.exit(0);
      };
    });
    f.pack();
    f.show();

  }
}