All Downloads are FREE. Search and download functionalities are using the official Maven repository.

JSci.instruments.TwoROI Maven / Gradle / Ivy

Go to download

JSci is a set of open source Java packages. The aim is to encapsulate scientific methods/principles in the most natural way possible. As such they should greatly aid the development of scientific based software. It offers: abstract math interfaces, linear algebra (support for various matrix and vector types), statistics (including probability distributions), wavelets, newtonian mechanics, chart/graph components (AWT and Swing), MathML DOM implementation, ... Note: some packages, like javax.comm, for the astro and instruments package aren't listed as dependencies (not available).

The newest version!
package JSci.instruments;

import java.awt.*;
import java.awt.geom.*;
import java.awt.event.*;
import java.util.*;
import java.awt.geom.*;
import javax.swing.*;
import javax.swing.event.*;

/**
 * A ROI made by two rectangular regions, one inside the other (two levels of interest)
 */
public class TwoROI extends ROIAdapter implements Control {
    private Rectangle template = new Rectangle(0,0,-2,-2);
    private Rectangle search = new Rectangle(0,0,-2,-2);
    private int factor = 1;
    private boolean useFactor = true;
    private static final int LW = 3;
    private int state;
    private int iX;
    private int iY;


    private Component comp = null;
    public void setComponent(Component c) {
        if (comp!=null) {
            comp.removeMouseListener((MouseListener)this);
            comp.removeMouseMotionListener((MouseMotionListener)this);
        }
        comp=c;
        if (comp!=null) {
            comp.addMouseListener((MouseListener)this);
            comp.addMouseMotionListener((MouseMotionListener)this);
        }
    }


    /**
     * draws the ROI. Doesn't overwrite the background; call it after the other paint operations
     */
    public void paint(Graphics g) {
	Graphics2D g2 = (Graphics2D)g;
	g2.setPaint(Color.GREEN);
	g2.draw(search);
	g2.setPaint(Color.MAGENTA);
	g2.draw(template);
    }

    /**
     * Use this to select the behaviour of the outer rectangle
     * @param u The outer rectangle is n-times the inner one?
     */
    public void setUseFactor(boolean u) { 
	useFactor = u;
	if (useFactor) search.setFrame(
				       template.getMinX()-factor*template.getWidth(),
				       template.getMinY()-factor*template.getHeight(),
				       (2*factor+1)*template.getWidth(),
				       (2*factor+1)*template.getHeight());
	if (comp!=null) comp.repaint();
    }


    /**
     * Use this to select the behaviour of the outer rectangle
     * @param f The outer rectangle is f-times the inner one.
     */
    public void setFactor(int f) { 
	factor = f;
	if (useFactor) search.setFrame(
				       template.getMinX()-factor*template.getWidth(),
				       template.getMinY()-factor*template.getHeight(),
				       (2*factor+1)*template.getWidth(),
				       (2*factor+1)*template.getHeight());
	if (comp!=null) comp.repaint();
    }


    /**
     * @return the inner rectangle
     */
    public Shape getShape() {
        return (Rectangle)template.clone();
    }

    /**
     * @return the outer rectangle.
     */
    public Rectangle getOuterRectangle() { return (Rectangle)search.clone(); }

    /**
     * Testing...
     */
    public static void main(String[] args) {
        final ROI r = new TwoROI();
        Player p = new Player();
        JSci.instruments.ImageSource fs = new TestImageSource();
	ImageFilter filter = new ImageFilterAdapter() { 
		public void filter(Image f) {} 
		public Component getFilterControlComponent() { return ((TwoROI)r).getControlComponent(); }
	    };
        fs.setSink(filter);
	filter.setSink(p);
        p.addROI(r);
        p.start();
    }


    JPanel ccomp=null;
    /** @return a component that controls this ROI */ 
    public Component getControlComponent() {
	if (ccomp!=null) return ccomp;
	ccomp=new JPanel();
	ccomp.setLayout(new BorderLayout());
	final JCheckBox modifySearchArea;
	final JSlider factor;
	modifySearchArea = new JCheckBox("Modify search area",false);
	modifySearchArea.addActionListener(new ActionListener() {
		public void actionPerformed(ActionEvent e) {
		    setUseFactor(!modifySearchArea.isSelected());
		}
	    });
	factor = new JSlider(1,5,1);
	setFactor(1);
	factor.addChangeListener(new ChangeListener() {
		public void stateChanged(ChangeEvent e) {
		    setFactor(factor.getValue());
		}
	    });
	ccomp.add(BorderLayout.NORTH,modifySearchArea);
	ccomp.add(BorderLayout.SOUTH,factor);
	return ccomp;
    }

    private int region(Point2D p) {
	if (p.getX()>=template.getMinX()-LW &&
	    p.getX()<=template.getMaxX()+LW &&
	    p.getY()>=template.getMinY()-LW &&
	    p.getY()<=template.getMaxY()+LW ) {
	    if (p.getX()<=template.getMinX()) {
		if (p.getY()<=template.getMinY()) return 10;
		if (p.getY()=search.getMinX()-LW &&
		p.getX()<=search.getMaxX()+LW &&
		p.getY()>=search.getMinY()-LW &&
		p.getY()<=search.getMaxY()+LW ) {
		if (p.getX()<=search.getMinX()) {
		    if (p.getY()<=search.getMinY()) return 2;
		    if (p.getY()template.getMaxX()) 
		dX=(int)(template.getMaxX()-template.getMinX());
	    if (template.getMinY()+dY>template.getMaxY()) 
		dY=(int)(template.getMaxY()-template.getMinY());
	    template.setFrame(template.getMinX()+dX,template.getMinY()+dY,template.getWidth()-dX,template.getHeight()-dY);
	    iX=event.getX();
	    iY=event.getY();
	    break;
	case 11:
	    dY=event.getY()-iY;
	    if (template.getMinY()+dY>template.getMaxY()) 
		dY=(int)(template.getMaxY()-template.getMinY());
	    template.setFrame(template.getMinX(),template.getMinY()+dY,template.getWidth(),template.getHeight()-dY);
	    iY=event.getY();
	    break;
	case 12:
	    dX=event.getX()-iX;
	    dY=event.getY()-iY;
	    if (template.getMaxX()+dXtemplate.getMaxY()) 
		dY=(int)(template.getMaxY()-template.getMinY());
	    template.setFrame(template.getMinX(),template.getMinY()+dY,template.getWidth()+dX,template.getHeight()-dY);
	    iX=event.getX();
	    iY=event.getY();
	    break;
	case 13:
	    dX=event.getX()-iX;
	    if (template.getMaxX()+dXtemplate.getMaxX()) 
		dX=(int)(template.getMaxX()-template.getMinX());
	    if (template.getMaxY()+dYtemplate.getMaxX()) 
		dX=(int)(template.getMaxX()-template.getMinX());
	    template.setFrame(template.getMinX()+dX,template.getMinY(),template.getWidth()-dX,template.getHeight());
	    iX=event.getX();
	    break;

	}
	if (!useFactor) switch (state) {
	case 0:
	    minX=(iXtemplate.getMinX()) 
		dX=(int)(template.getMinX()-search.getMinX());
	    if (search.getMinY()+dY>template.getMinY()) 
		dY=(int)(template.getMinY()-search.getMinY());
	    search.setFrame(search.getMinX()+dX,search.getMinY()+dY,search.getWidth()-dX,search.getHeight()-dY);
	    iX=event.getX();
	    iY=event.getY();
	    break;
	case 3:
	    dY=event.getY()-iY;
	    if (search.getMinY()+dY>template.getMinY()) 
		dY=(int)(template.getMinY()-search.getMinY());
	    search.setFrame(search.getMinX(),search.getMinY()+dY,search.getWidth(),search.getHeight()-dY);
	    iY=event.getY();
	    break;
	case 4:
	    dX=event.getX()-iX;
	    dY=event.getY()-iY;
	    if (search.getMinY()+dY>template.getMinY()) 
		dY=(int)(template.getMinY()-search.getMinY());
	    if (search.getMaxX()+dXtemplate.getMinX()) 
		dX=(int)(template.getMinX()-search.getMinX());
	    search.setFrame(search.getMinX()+dX,search.getMinY(),search.getWidth()-dX,search.getHeight()+dY);
	    iX=event.getX();
	    iY=event.getY();
	    break;
	case 9:
	    dX=event.getX()-iX;
	    if (search.getMinX()+dX>template.getMinX()) 
		dX=(int)(template.getMinX()-search.getMinX());
	    search.setFrame(search.getMinX()+dX,search.getMinY(),search.getWidth()-dX,search.getHeight());
	    iX=event.getX();
	    break;
	case 10:
	    dX=event.getX()-iX;
	    dY=event.getY()-iY;
	    if (template.getMinX()+dX>template.getMaxX()) 
		dX=(int)(template.getMaxX()-template.getMinX());
	    if (template.getMinX()+dXtemplate.getMaxY()) 
		dY=(int)(template.getMaxY()-template.getMinY());
	    if (template.getMinY()+dYtemplate.getMaxY()) 
		dY=(int)(template.getMaxY()-template.getMinY());
	    if (template.getMinY()+dYsearch.getMaxX()) 
		dX=(int)(search.getMaxX()-template.getMaxX());
	    if (template.getMinY()+dY>template.getMaxY()) 
		dY=(int)(template.getMaxY()-template.getMinY());
	    if (template.getMinY()+dYsearch.getMaxX()) 
		dX=(int)(search.getMaxX()-template.getMaxX());
	    template.setFrame(template.getMinX(),template.getMinY(),template.getWidth()+dX,template.getHeight());
	    iX=event.getX();
	    break;
	case 14:
	    dX=event.getX()-iX;
	    dY=event.getY()-iY;
	    if (template.getMaxX()+dXsearch.getMaxX()) 
		dX=(int)(search.getMaxX()-template.getMaxX());
	    if (template.getMaxY()+dYsearch.getMaxY()) 
		dY=(int)(search.getMaxY()-template.getMaxY());
	    template.setFrame(template.getMinX(),template.getMinY(),template.getWidth()+dX,template.getHeight()+dY);
	    iX=event.getX();
	    iY=event.getY();
	    break;
	case 15:
	    dY=event.getY()-iY;
	    if (template.getMaxY()+dYsearch.getMaxY()) 
		dY=(int)(search.getMaxY()-template.getMaxY());
	    template.setFrame(template.getMinX(),template.getMinY(),template.getWidth(),template.getHeight()+dY);
	    iY=event.getY();
	    break;
	case 16:
	    dY=event.getY()-iY;
	    dX=event.getX()-iX;
	    if (template.getMinX()+dX>template.getMaxX()) 
		dX=(int)(template.getMaxX()-template.getMinX());
	    if (template.getMinX()+dXsearch.getMaxY()) 
		dY=(int)(search.getMaxY()-template.getMaxY());
	    template.setFrame(template.getMinX()+dX,template.getMinY(),template.getWidth()-dX,template.getHeight()+dY);
	    iX=event.getX();
	    iY=event.getY();
	    break;
	case 17:
	    dX=event.getX()-iX;
	    if (template.getMinX()+dX>template.getMaxX()) 
		dX=(int)(template.getMaxX()-template.getMinX());
	    if (template.getMinX()+dX




© 2015 - 2024 Weber Informatics LLC | Privacy Policy