/*========================================================================= Program: FusionViewer Module: $RCSfile: RectangleROIMouseTool.java,v $ Language: Java Date: $Date: 2007/02/02 19:25:27 $ Version: $Revision: 1.2 $ Copyright (c) Insightful Corporation. All rights reserved. See Copyright.txt for details. This software is distributed WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the above copyright notice for more information. =========================================================================*/ package org.fusionviewer.display; import java.awt.Point; import java.awt.event.MouseEvent; /**\class RectangleROIMouseTool *\brief Construct a rectagle ROI responding to Mouse events */ public class RectangleROIMouseTool implements MouseTool { private ImageSliceView m_parent; private Point m_anchor = new Point(); private Point m_tempPoint = new Point(); public RectangleROIMouseTool(ImageSliceView parent) { m_parent = parent; } public boolean canHandle(MouseEvent e) { m_tempPoint.setLocation(e.getX(), e.getY()); m_parent.transformPoint(m_tempPoint); if ((m_tempPoint.x < 0) || (m_tempPoint.y < 0) || (m_tempPoint.x >= m_parent.getViewWidth()) || (m_tempPoint.y >= m_parent.getViewHeight())) return false; return true; } public void mousePressed(MouseEvent e) { m_anchor.setLocation(e.getX(), e.getY()); m_parent.transformPoint(m_anchor); m_tempPoint.setLocation(m_anchor.x, m_anchor.y); dragCorner(m_tempPoint); } public void mouseReleased(MouseEvent e) { m_parent.releaseMouseTool(); } public void mouseDragged(MouseEvent e) { m_tempPoint.setLocation(e.getX(), e.getY()); m_parent.transformPoint(m_tempPoint); dragCorner(m_tempPoint); } public void mouseMoved(MouseEvent e) { } private void dragCorner(Point position) { float x = (float) position.getX(); float y = (float) position.getY(); if (x < 0) x = 0; if (y < 0) y = 0; if (x >= m_parent.getViewWidth()) x = m_parent.getViewWidth() - 1; if (y >= m_parent.getViewHeight()) y = m_parent.getViewHeight() - 1; m_tempPoint.setLocation(x, y); m_parent.getParent().display(); } public Point getAnchor1() { return new Point(m_anchor); } public Point getAnchor2() { return new Point(m_tempPoint); } }