Sunday, 15 June 2014

These instructions for getting started writing plugins for ImageJ2 were developed at the Fiji / ImageJ2 Hackathon in Dresden, Germany, December 2011, using the Eclipse IDE version 3.7 and Sun JDK 1.6 on Ubuntu 11.04. It assumes that you have a working installation of ImageJ2, from http://developer.imagej.net/downloads.

Under the Libraries tab Add External Jars... and navigate to the imagej-2.0.0-SNAPSHOT-all.jar you downloaded in step 3. This gives you access to the IJ2 application programming interface (API). At the time of writing, to get context Javadoc for ImgLib classes you also have to specify the URL of its Javadoc. Click on the triangle to the left of imagej-2.0.0-SNAPSHOT-all.jar, select "Javadoc location:", hit Edit, add the URL:

http://jenkins.imagej.net/job/ImgLib-daily/javadoc/

Hit Validate and if everything is OK, hit OK.

Configure Ant to do your build using the javac compiler

Copy this code and paste it into your project in a file called build.xml

Hello World

Writing your first plugin: HelloWorld

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 RunnablePlugin {

Note the squiggly red line under RunnablePlugin 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 'RunnablePlugin' (imagej.ext.plugin) and hit Enter. Eclipse adds a line of code for you at the top of the file: import imagej.ext.plugin.RunnablePlugin;

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 RunnablePlugin 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

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