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.MouseEvent; 025 026import icy.canvas.IcyCanvas; 027import icy.canvas.IcyCanvas2D; 028import icy.gui.dialog.MessageDialog; 029import icy.gui.frame.progress.AnnounceFrame; 030import icy.painter.Overlay; 031import icy.plugin.abstract_.PluginActionable; 032import icy.sequence.Sequence; 033import icy.type.point.Point5D; 034 035/** 036 * This example displays a simple cross over the sequence. 037 * 038 * @author Fabrice de Chaumont 039 * @author Stephane Dallongeville 040 */ 041public class SimpleOverlayTutorial1 extends PluginActionable 042{ 043 private class SimpleCrossOverlay extends Overlay 044 { 045 public SimpleCrossOverlay() 046 { 047 super("Simple cross"); 048 } 049 050 @Override 051 public void paint(Graphics2D g, Sequence sequence, IcyCanvas canvas) 052 { 053 // check if we are dealing with a 2D canvas and we have a valid Graphics object 054 if ((canvas instanceof IcyCanvas2D) && (g != null)) 055 { 056 // display a big yellow cross all over the sequence 057 g.setColor(Color.YELLOW); 058 g.setStroke(new BasicStroke(5)); 059 g.drawLine(0, 0, sequence.getWidth(), sequence.getHeight()); 060 g.drawLine(0, sequence.getHeight(), sequence.getWidth(), 0); 061 } 062 } 063 064 @Override 065 public void mouseClick(MouseEvent e, Point5D.Double imagePoint, IcyCanvas canvas) 066 { 067 // check if we are dealing with a 2D canvas 068 if (canvas instanceof IcyCanvas2D) 069 { 070 // tools are provided to avoid keeping a global reference on the sequence 071 // which could lead to a memory leak 072 073 // remove painter from all sequence where it is attached 074 remove(); 075 } 076 } 077 } 078 079 // automatically called when plugin is launched from ICY for "PluginImageAnalysis" type plugin 080 @Override 081 public void run() 082 { 083 // get the current active sequence 084 // we store it for easier painter remove on mouse click 085 Sequence sequence = getActiveSequence(); 086 087 // no sequence has been found ? 088 if (sequence == null) 089 { 090 // display an information message as we need an opened sequence 091 MessageDialog.showDialog("This example needs a sequence to start. Please load an image file.", 092 MessageDialog.INFORMATION_MESSAGE); 093 return; 094 } 095 096 // display an announcement with Plugin description 097 new AnnounceFrame( 098 "This example display a yellow cross over the sequence in 2D. Click over the sequence to remove the painter."); 099 100 // Add the cross overlay, it becomes active after being added. 101 sequence.addOverlay(new SimpleCrossOverlay()); 102 } 103}