ImageJ GUI Import Error

Hi everyone!

I am trying to create a GUI for a macro that I have written, and I followed this example provided by ImageJ. However, when I tried to run this code, there was an error in the very first line, and I could not import the necessary packages. Any ideas on how to fix this?


import ij.;
import ij.plugin.
;
import ij.gui.*;
public class Generic_Dialog_Example implements PlugIn

{
static String title=“Example”;
static int width=512,height=512;
public void run(String arg)

{
GenericDialog gd = new GenericDialog(“New Image”);
gd.addStringField("Title: ", title);
gd.addNumericField("Width: ", width, 0);
gd.addNumericField("Height: ", height, 0);
gd.showDialog();
if (gd.wasCanceled()) return;
title = gd.getNextString();
width = (int)gd.getNextNumber();
height = (int)gd.getNextNumber();
IJ.newImage(title, “8-bit”, width, height, 1);
}
}

There should be an asterisk after the period in the first two lines, not sure why it did not show up.

The following Java-code works for me:

import ij.*;
import ij.gui.*;
import java.awt.*;
import ij.plugin.*;

public class My_Plugin implements PlugIn {
	static String title="Example";
	static int width=512,height=512;

	public void run(String arg) {

		GenericDialog gd = new GenericDialog("New Image");
			gd.addStringField("Title: ", title);
			gd.addNumericField("Width: ", width, 0);
			gd.addNumericField("Height: ", height, 0);
		gd.showDialog();
		
		if (gd.wasCanceled()) return;
		title = gd.getNextString();
		width = (int)gd.getNextNumber();
		height = (int)gd.getNextNumber();
		IJ.newImage(title, "8-bit", width, height, 1);
	}
}

Hmm, it still doesn’t work for me for some reason. I am writing this in the ImageJ macro language, so maybe that’s why. Can I create this GUI in the macro language? Or do I need to switch to Java?

The code you’ve posted appears being Java-code and needs to be compiled.

If you prefer the ImageJ-macro language, you need to study

https://imagej.nih.gov/ij/developer/macro/functions.html

where you find everything that is needed to construct a parameter dialog.

Ohhh I see. Is there a way to still make a GUI in this language though? Or would you recommend me to use Java?

Please study

and especially the section starting with:
Dialog.create(“Title”)

1 Like

Got it. Thank you so much for your help! Much appreciated!

1 Like