Developing simple plugins for ImageJ can be accomplished with the default editor and a little effort. For more complex programs, the Fiji editor provides extra niceties (such as syntax coloring and more informative error messages). But the real breakthrough comes when combining the ImageJ plugin system and an advanced Java IDE. In this section, we will set up Eclipse (http://www.eclipse.org/) to work with ImageJ. This will allow us to write, debug, and run our plugins in a better way.
This section assumes that you already have a working Eclipse installation. If you do not have one, please go to http://www.eclipse.org/downloads/packages/eclipse-classic-422/junosr2 (the latest Eclipse version at the time of the writing) and follow the installation instructions for your platform.
Let's set up our system, by performing the following steps:
imagejexample
.In the next window, in the Source tab, choose imagejexample/plugins
(instead of imagejexamples/bin
) as the output folder. In the Libraries tab, you will have the common libraries for the JRE system library that you chose in the first step:
Now click on the Add External JARs...button and select the ij.jar
file of your ImageJ installation. The following screenshot shows these tabs with their appropriate values. Click on Finish to and create your project:
We are almost finished. Now you need to check whether your project is using the ImageJ's main class from the ij.jar
file when it is run. You can do so by right-clicking on your project in the Package Explorer tab and then on Properties.... From there, select the Run/Debug Settings menu. You will see a configuration called New_configuration (if not, create a new one clicking on New... and the selecting Java Application), click on it and then on the Edit… button. In the window that shows up, in the Main tab, check whether ij.ImageJ
has been entered in the Main class text field, as shown in the following screenshot:
Eclipse may have done this for you, but it does not hurt to check. Click on OK and close all setup windows. We are done!
You can test if your setup is correct by clicking on your project name and then navigating to the Run | Run (Ctrl + F11) menu option. A new ImageJ window should pop up, and the Plugins menu should be empty below the divider bar. It is empty because this ImageJ version now expects to find the plugins within the specified output folder (where the .class
files will be created). As it is currently empty (we have not started to code our own plugins under this system), there are no plugins to be shown.
Now it is time to start coding our first plugin with the Eclipse configuration. Right-click on your project name and create a new class by navigating to the New | Class option. Implement the PlugInFilter
interface in the class (clicking on the Browse... button and searching all the classes inside the ij
package) and give it any name you want. Using the default package is fine, even though Eclipse displays a warning.
When you do this, you will have a new editor window with the following contents:
import ij.ImagePlus; import ij.plugin.filter.PlugInFilter; import ij.process.ImageProcessor; public class Eclipse_Sample implements PlugInFilter { @Override public void run(ImageProcessor arg0) { // TODO Auto-generated method stub } @Override public int setup(String arg0, ImagePlus arg1) { // TODO Auto-generated method stub return 0; } }
As you can see, Eclipse automatically creates the skeleton of your class for you and overrides the methods you need to implement. This skeleton is not as complete as the one that ImageJ provides when we create plugins from its menus, but it is fine. What is great about developing plugins in Eclipse is that now, for instance, we can autocomplete the code, as this environment has internal knowledge of the classes contained within the ij.jar
file. As an example, when we are filling in the setup
method we can ask it to complete the name of the constants which are to be returned (Eclipse will do it automatically for us as we type, or we can press Ctrl + Space bar), as shown in the following screenshot:
This also works for every method of every class inside the ImageJ package.
There is one more thing you can do to ease the development process: associate the ImageJ Javadoc (the API documentation) with the ij.jar
file that is located in the Referenced Libraries
folder in your project. Right-click on the filename and select Properties. In the window that appears, select Javadoc Location and add the API URL. You can check if the URL is the right one, by clicking on the Validate... button. If everything is correct, you should see the following screenshot:
The autocomplete function is now more informative, as it is accompanied by the exact description of the code published in the API in a yellow box next to the selected method or constant: