Posted in
Writing your first plugin – HelloWorld
- Right-click on the src directory of your new project and New > Class
- Give your class a name e.g. HelloWorld (the convention for classes is to be capitalised like that)
- Eclipse initialises a new file and puts a little code into it for you.
- Edit the first line so it reads: public class HelloWorld implements ImageJPlugin {
- Note the squiggly red line under ImageJPlugin indicating a compile error, move your cursor to it and hit Ctrl+1
- Eclipse gives you a bunch of autocorrect options, select the top one, Import 'ImageJPlugin' (imagej.ext.plugin) and hit Enter – Eclipse adds a line of code for you at the top of the file: import imagej.ext.plugin.ImageJPlugin;
- Now HelloWorld gets a squiggly line, so Ctrl+1 again and select Add Unimplemented Methods – Eclipse adds a bunch of code, which is the run() method needed because we are implementing the ImageJPlugin interface
- We want HelloWorld to show up in the ImageJ menus, so add an annotation above the public class... line: @Plugin(menuPath = "Plugins>Hello World")
- Ctrl+1 again on the @Plugin squiggly line to autocorrect the missing import
- You can Ctrl+Space and Eclipse will suggest options for autocompletion
- Let's check ImageJ and see if our plugin is available in the menus
- Run ImageJ
- You should now see in ImageJ's Plugins menu an item called Hello World [IJ2]
- But it doesn't do anything yet because we haven't added any useful code
- Add code to your HelloWorld until it matches HelloWorld.java below, using Ctrl+1 and Ctrl+Space to investigate possibilities in the API
- Each time you update your code, Ant rebuilds your jar and copies it to your plugins directory
- At the moment, you have to restart ImageJ to test your changes
HelloWorld.java
import imagej.ext.plugin.ImageJPlugin;import imagej.ext.plugin.Parameter;import imagej.ext.plugin.Plugin;import imagej.ui.UIService;@Plugin(menuPath = "Plugins>Hello World")public class HelloWorld implements ImageJPlugin {@Parameterprivate UIService uiService;@Overridepublic void run() {uiService.showDialog("Hello world.");}}
- Printer-friendly version
- Login to post comments
