WindowImagePanel.java

package ac.essex.ooechs.imaging.commons.window; 
 
import ac.essex.ooechs.imaging.commons.util.panels.ImagePanel; 
import ac.essex.ooechs.imaging.commons.PixelLoader; 
import ac.essex.ooechs.imaging.commons.window.WindowGUI; 
 
import java.awt.event.MouseMotionAdapter; 
import java.awt.event.MouseEvent; 
import java.awt.event.MouseAdapter; 
import java.awt.*; 
 
/** 
 * <p/> 
 * This program is free software; you can redistribute it and/or 
 * modify it under the terms of the GNU General Public License 
 * as published by the Free Software Foundation; either version 2 
 * of the License, or (at your option) any later version, 
 * provided that any use properly credits the author. 
 * This program is distributed in the hope that it will be useful, 
 * but WITHOUT ANY WARRANTY; without even the implied warranty of 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 
 * GNU General Public License for more details at http://www.gnu.org 
 * </p> 
 * 
 * @author Olly Oechsle, University of Essex, Date: 19-Mar-2008 
 * @version 1.0 
 */ 
public class WindowImagePanel extends ImagePanel { 
 
    public ac.essex.ooechs.imaging.commons.window.data.Window currentWindow; 
 
    public WindowGUI gui; 
 
    public WindowImagePanel(final WindowGUI gui) { 
 
        this.gui = gui; 
 
        setDisplayCentered(true); 
 
        addMouseMotionListener(new MouseMotionAdapter() { 
 
            public void mouseMoved(MouseEvent e) { 
 
                if (gui.currentClass == null) return; 
 
                if (image == null) return; 
 
                currentWindow = gui.getWindow(); 
 
                int x = getX(e); 
                int y = getY(e); 
 
                if (x < 0) x = 0; 
                if (y < 0) y = 0; 
 
                if (x + currentWindow.width > image.getWidth()) { 
                    x = image.getWidth() - currentWindow.width; 
                } 
                if (y + currentWindow.height > image.getHeight()) { 
                    y = image.getHeight() - currentWindow.height; 
                } 
 
                currentWindow.x = x; 
                currentWindow.y = y; 
 
 
 
                gui.setStatus("X: " + x + ", Y: " + y); 
 
                repaint(); 
 
            } 
 
        }); 
 
        addMouseListener(new MouseAdapter() { 
 
            public void mouseClicked(MouseEvent e) { 
 
                if (gui.currentClass == null) return; 
 
                if (image == null) return;                 
 
                gui.currentImage.addWindow(currentWindow); 
                 
            } 
 
        }); 
 
    } 
 
 
    public void setImage(PixelLoader image) { 
        currentWindow = null; 
        super.setImage(image); 
    } 
 
    public void paintComponent(Graphics g) { 
        super.paintComponent(g); 
        if (gui.currentImage != null)  { 
            for (int i = 0; i < gui.currentImage.getWindows().size(); i++) { 
                ac.essex.ooechs.imaging.commons.window.data.Window window = gui.currentImage.getWindows().elementAt(i); 
                drawRect(g, window); 
            } 
        } 
        if (currentWindow != null) { 
            drawRect(g, currentWindow); 
        } 
    } 
 
    public void drawRect(Graphics g, ac.essex.ooechs.imaging.commons.window.data.Window w)  { 
        g.setColor(w.getWindowClass().getColor()); 
        g.drawRect(w.x + offsetX, w.y + offsetY, w.width * zoom, w.height * zoom); 
    } 
 
 
 
}