root/graph/JavaPopWeb/src/jp/ac/nime/computer/grpsimulator/ImageViewPanel.java

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. ImageViewPanel
  2. setImage
  3. paintComponent
  4. addActionListener
  5. removeActionListener
  6. processMouseEvent

package jp.ac.nime.computer.grpsimulator;

import java.awt.*;
import java.awt.event.*;
import javax.swing.JComponent;

/** ImageViewPanelは、イメージを表示し、イメージ上をクリックされたら登録されたActionListenerに通知します。
 *
 * @author igarashi
 * @version 1.0.0
 */
public class ImageViewPanel extends JComponent {
    
    private Image img_;   
    private ActionListener actionListener_;
    
    public ImageViewPanel(int width, int height) {
        super();
        Dimension dim = new Dimension(width,height);
        setMaximumSize(dim);
        setMinimumSize(dim);
        setPreferredSize(dim);
        setSize(dim);
    }
    
    /** パネルに表示する画像を設定する
     * @param img 表示するイメージ
     */    
    public void setImage(Image img) {
        img_ = img;
        repaint();
    }
    
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        if (img_ != null) g.drawImage(img_,0,0,this);
    }
    
    public void addActionListener(ActionListener listener) {
        actionListener_ = AWTEventMulticaster.add(actionListener_, listener);
        enableEvents(AWTEvent.MOUSE_EVENT_MASK);
    }
    public void removeActionListener(ActionListener listener) {
        actionListener_ = AWTEventMulticaster.remove(actionListener_, listener);
    }
    protected void processMouseEvent(MouseEvent mouseEvent) {
        if (mouseEvent.getID() == MouseEvent.MOUSE_PRESSED) {
            actionListener_.actionPerformed(
                new ActionEvent(mouseEvent.getPoint(),ActionEvent.ACTION_PERFORMED,"ImageClick")
            );
        }
        super.processMouseEvent(mouseEvent);
    }
}

/* [<][>][^][v][top][bottom][index][help] */