Re: API equivalent of Overlay.drawString()

Hi Wayne

Thank you for the quick response.

I got this to work as a Python script, but I am unable to get the
JavaScript to work because JS is not importing the classes. I get errors
like
nashorn:mozilla_compat.js:67 ReferenceError: "Font" is not defined
even with auto import set active.

I was able to fix this one with importPackage(java.awt),
but then I got the same error for Overlay.
I tried importPackage(ij.gui), but I then got an error that ij is
undefined, and I could not find a way around that.

> Hi Wayne
>
> Thank you for the quick response.
>
> I got this to work as a Python script, but I am unable to get the
> JavaScript to work because JS is not importing the classes. I get errors
> like
> nashorn:mozilla_compat.js:67 ReferenceError: "Font" is not defined
> even with auto import set active.
>
> I was able to fix this one with importPackage(java.awt),
> but then I got the same error for Overlay.
> I tried importPackage(ij.gui), but I then got an error that ij is
> undefined, and I could not find a way around that.
>
> According to https://imagej.nih.gov/ij/developer/javascript.html#import> these classes should all be imported by default in JS.
>
> Best regards,
> --aryeh
>
> On 28/01/2018 23:26, Wayne Rasband wrote:
>>> On Jan 28, 2018, at 12:46 PM, Aryeh Weiss <[hidden email]> wrote:
>>>
>>> The macro language has an Overlay.drawString("text", x, y) function
>>> that creates a nondestructive text overlay.
>>>
>>> What is the equivalent API command or commands for doing this in a
>>> Python script?
>> Create a TextRoi and add it to the overlay. Here is a JavaScript example:
>>
>> img = IJ.openImage("http://wsr.imagej.net/images/blobs.gif");
>> text = "These three lines\nwill be overlayed\non the image.";
>> font = new Font("SansSerif", Font.PLAIN, 24);
>> roi = new TextRoi(10, 10, text, font);
>> roi.setStrokeColor(Color.white);
>> roi.setFillColor(Color.blue);
>> overlay = new Overlay();
>> overlay.add(roi);
>> img.setOverlay(overlay);
>> img.show();
>>
>> -wayne
>>
>> --
>> ImageJ mailing list: http://imagej.nih.gov/ij/list.html>>
>

Re: API equivalent of Overlay.drawString()

Hi Jan

Thank you for your reply.

On 29/01/2018 12:47, Jan Eglinger wrote:

> Hi Aryeh,
>
> On 29.01.2018 06:04, Aryeh Weiss wrote:
> > I tried importPackage(ij.gui), but I then got an error that ij is
> > undefined, and I could not find a way around that.
>
> This should work:
>
> importPackage(Packages.ij.gui);
> or
> importClass(Packages.ij.gui.Overlay);
>

I did not know to add Packages in front of ij.gui. Now I get it.
>
> Note that it is always safer to explicitly specify the classes you
> want to import, instead of relying on package-wide auto-import.
>
Yes, and I generally use the auto-import when testing out something
"quick and dirty", before putting it int a better constructed script.

Best regards
--aryeh

> See also:
> https://stackoverflow.com/q/147454/1919049> https://imagej.net/Script_Editor_Auto_Import>
> Jan
>
>
> On 29.01.2018 06:04, Aryeh Weiss wrote:
>> Hi Wayne
>>
>> Thank you for the quick response.
>>
>> I got this to work as a Python script, but I am unable to get the
>> JavaScript to work because JS is not importing the classes. I get
>> errors like
>> nashorn:mozilla_compat.js:67 ReferenceError: "Font" is not defined
>> even with auto import set active.
>>
>> I was able to fix this one with importPackage(java.awt),
>> but then I got the same error for Overlay.
>> I tried importPackage(ij.gui), but I then got an error that ij is
>> undefined, and I could not find a way around that.
>>
>> According to https://imagej.nih.gov/ij/developer/javascript.html#import>> these classes should all be imported by default in JS.
>>
>> Best regards,
>> --aryeh
>>
>> On 28/01/2018 23:26, Wayne Rasband wrote:
>>>> On Jan 28, 2018, at 12:46 PM, Aryeh Weiss <[hidden email]> wrote:
>>>>
>>>> The macro language has an Overlay.drawString("text", x, y) function
>>>> that creates a nondestructive text overlay.
>>>>
>>>> What is the equivalent API command or commands for doing this in a
>>>> Python script?
>>> Create a TextRoi and add it to the overlay. Here is a JavaScript
>>> example:
>>>
>>> img = IJ.openImage("http://wsr.imagej.net/images/blobs.gif");
>>> text = "These three lines\nwill be overlayed\non the image.";
>>> font = new Font("SansSerif", Font.PLAIN, 24);
>>> roi = new TextRoi(10, 10, text, font);
>>> roi.setStrokeColor(Color.white);
>>> roi.setFillColor(Color.blue);
>>> overlay = new Overlay();
>>> overlay.add(roi);
>>> img.setOverlay(overlay);
>>> img.show();
>>>
>>> -wayne
>>>
>>> --
>>> ImageJ mailing list: http://imagej.nih.gov/ij/list.html>>>
>>
>