001/*
002 * Copyright 2010, 2011 Institut Pasteur.
003 * 
004 * This file is part of ICY.
005 * 
006 * ICY is free software: you can redistribute it and/or modify
007 * it under the terms of the GNU General Public License as published by
008 * the Free Software Foundation, either version 3 of the License, or
009 * (at your option) any later version.
010 * 
011 * ICY is distributed in the hope that it will be useful,
012 * but WITHOUT ANY WARRANTY; without even the implied warranty of
013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
014 * GNU General Public License for more details.
015 * 
016 * You should have received a copy of the GNU General Public License
017 * along with ICY. If not, see <http://www.gnu.org/licenses/>.
018 */
019package plugins.tutorial.painter;
020
021import java.awt.BasicStroke;
022import java.awt.Color;
023import java.awt.Graphics2D;
024import java.awt.event.KeyEvent;
025import java.awt.event.MouseEvent;
026import java.awt.geom.Rectangle2D;
027
028import icy.canvas.IcyCanvas;
029import icy.canvas.IcyCanvas2D;
030import icy.gui.dialog.MessageDialog;
031import icy.gui.frame.progress.AnnounceFrame;
032import icy.painter.Overlay;
033import icy.plugin.abstract_.PluginActionable;
034import icy.sequence.Sequence;
035import icy.type.point.Point5D;
036
037/**
038 * This plugin shows interaction of painters with users.
039 * 
040 * @author Fabrice de Chaumont
041 */
042public class SimpleOverlayTutorial2 extends PluginActionable
043{
044    // Icy call this method when user launch a plugin
045    @Override
046    public void run()
047    {
048        // get the current active sequence
049        Sequence sequence = getActiveSequence();
050
051        // no sequence has been found
052        if (sequence == null)
053        {
054            // display an information message as we need an opened sequence
055            MessageDialog.showDialog("This example needs a sequence to start. Please load an image file.",
056                    MessageDialog.INFORMATION_MESSAGE);
057            return;
058        }
059
060        // display an announcement with Plugin description
061        new AnnounceFrame(
062                "This example displays a custom mouse cursor and draw a single line on drag operation. It also displays a line along the diagonal current view");
063
064        // add a new "Interaction Test" overlay to the sequence
065        sequence.addOverlay(new InteractionTestOverlay());
066    }
067
068    // our painter extends AbstractPainter as it provides painter facilities
069    class InteractionTestOverlay extends Overlay
070    {
071        int xm, ym;
072        boolean dragging;
073        int xt, yt;
074
075        public InteractionTestOverlay()
076        {
077            super("Simple overlay");
078
079            dragging = false;
080        }
081
082        @Override
083        public void keyPressed(KeyEvent e, Point5D.Double imagePoint, IcyCanvas canvas)
084        {
085            // check if we are dealing with a 2D canvas
086            if (canvas instanceof IcyCanvas2D)
087            {
088                // display key press information in output window
089                System.out.print("Plugin Simple Overlay : ");
090                System.out.println("Keypressed : " + e);
091            }
092        }
093
094        @Override
095        public void mouseClick(MouseEvent e, Point5D.Double imagePoint, IcyCanvas canvas)
096        {
097            // check if we are dealing with a 2D canvas and we have a valid image position
098            if ((canvas instanceof IcyCanvas2D) && (imagePoint != null))
099            {
100                // display mouse click information in output window
101                System.out.print("Plugin Simple Overlay : ");
102                System.out.println("mouseClick : " + e);
103
104                // test if we are in the close box
105                int x = (int) imagePoint.getX();
106                int y = (int) imagePoint.getY();
107
108                // remove the overlay from all sequence if we click in the specific area
109                if ((x < 10) && (x > 0) && (y < 10) && (y > 0))
110                    remove();
111            }
112        }
113
114        @Override
115        public void mouseDrag(MouseEvent e, Point5D.Double imagePoint, IcyCanvas canvas)
116        {
117            // check if we are dealing with a 2D canvas
118            if ((canvas instanceof IcyCanvas2D) && (imagePoint != null))
119            {
120                // update mouse drag position
121                xt = (int) imagePoint.getX();
122                yt = (int) imagePoint.getY();
123
124                dragging = true;
125
126                // notify painter changed as dragging property and coordinates may be changed
127                painterChanged();
128            }
129        }
130
131        @Override
132        public void mouseMove(MouseEvent e, Point5D.Double imagePoint, IcyCanvas canvas)
133        {
134            // check if we are dealing with a 2D canvas
135            if ((canvas instanceof IcyCanvas2D) && (imagePoint != null))
136            {
137                // update mouse position
138                xm = (int) imagePoint.getX();
139                ym = (int) imagePoint.getY();
140
141                dragging = false;
142
143                // notify painter changed as dragging property and coordinates may be changed
144                painterChanged();
145            }
146        }
147
148        @Override
149        public void paint(Graphics2D g, Sequence sequence, IcyCanvas canvas)
150        {
151            // check if we are dealing with a 2D canvas and we have a valid Graphics object
152            if ((canvas instanceof IcyCanvas2D) && (g != null))
153            {
154                final IcyCanvas2D canvas2D = (IcyCanvas2D) canvas;
155
156                // display informative text about closing painter
157                g.setColor(Color.darkGray);
158                g.drawString("<--- click here to close SimpleOverlay", 21, 11);
159                g.setColor(Color.white);
160                g.drawString("<--- click here to close SimpleOverlay", 20, 10);
161
162                // display the cross cursor
163                g.setStroke(new BasicStroke(3));
164                g.setColor(Color.black);
165                g.drawLine(xm - 5, ym - 5, xm + 5, ym + 5);
166                g.drawLine(xm - 5, ym + 5, xm + 5, ym - 5);
167                g.setStroke(new BasicStroke(1));
168                g.setColor(Color.yellow);
169                g.drawLine(xm - 5, ym - 5, xm + 5, ym + 5);
170                g.drawLine(xm - 5, ym + 5, xm + 5, ym - 5);
171
172                // if we're dragging, display the dragging line
173                if (dragging)
174                    g.drawLine(xm, ym, xt, yt);
175
176                // get visible rectangle of canvas
177                final Rectangle2D visibleRect = canvas2D.getImageVisibleRect();
178
179                if (!visibleRect.isEmpty())
180                {
181                    // draw a line all across the canvas
182                    g.drawLine((int) visibleRect.getMinX(), (int) visibleRect.getMinY(), (int) visibleRect.getMaxX(),
183                            (int) visibleRect.getMaxY());
184                }
185
186                // display close box
187                g.drawRect(0, 0, 10, 10);
188            }
189        }
190    }
191}