Skip to main content

Hello World

Posted in

Writing your first plugin – HelloWorld

 

  1. Right-click on the src directory of your new project and New > Class
  2. Give your class a name e.g. HelloWorld (the convention for classes is to be capitalised like that)
  3. Eclipse initialises a new file and puts a little code into it for you.
    1. Edit the first line so it reads: public class HelloWorld implements ImageJPlugin {
    2. Note the squiggly red line under ImageJPlugin indicating a compile error, move your cursor to it and hit Ctrl+1
    3. 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;
    4. 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
    5. We want HelloWorld to show up in the ImageJ menus, so add an annotation above the public class... line: @Plugin(menuPath = "Plugins>Hello World")
    6. Ctrl+1 again on the @Plugin squiggly line to autocorrect the missing import
    7. You can Ctrl+Space and Eclipse will suggest options for autocompletion
  4. Let's check ImageJ and see if our plugin is available in the menus
    1. Run ImageJ
    2. You should now see in ImageJ's Plugins menu an item called Hello World [IJ2]
    3. But it doesn't do anything yet because we haven't added any useful code
  5. Add code to your HelloWorld until it matches HelloWorld.java below, using Ctrl+1 and Ctrl+Space to investigate possibilities in the API
    1. Each time you update your code, Ant rebuilds your jar and copies it to your plugins directory
    2. 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 {
    @Parameter
    private UIService uiService;
    
    @Override
    public void run() {
        uiService.showDialog("Hello world.");        
    }
}