Easiest way to set font size of drawString()

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

Easiest way to set font size of drawString()

alexwatson
Does anyone know the best, or easiest way to do this?
Reply | Threaded
Open this post in threaded view
|

Re: Easiest way to set font size of drawString()

Jim Passmore-2
On Tue, Jun 12, 2012 at 10:26 AM, alexwatson <[hidden email]> wrote:

> Does anyone know the best, or easiest way to do this?
>
> --
>

Alex,
If you're talking about the macro function, try looking through the
function documentation at
http://imagej.nih.gov/ij/developer/macro/functions.html .  You'll find the
drawString() function description references the setFont() function.

If you're referring to a Java method, check the ImageJ Java API at
http://imagej.nih.gov/ij/developer/api/index.html?overview-summary.html
It lists drawString() and setFont() as ImageProcessor methods.  (hmmm. deja
vu.   ;-) )

At any rate, checking documentation first will help us help you.  You can
find all sorts of documentation at http://imagej.nih.gov/ij/developer/ .



*Jim Passmore*
Research Associate
Sealed Air Corporation

--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html
Reply | Threaded
Open this post in threaded view
|

Re: Easiest way to set font size of drawString()

alexwatson
In reply to this post by alexwatson
My apologies.  I meant that I am trying to do this inside of a plug-in, not using the drawString() macro function.
Reply | Threaded
Open this post in threaded view
|

Re: Easiest way to set font size of drawString()

alexwatson
This post was updated on .
In reply to this post by Jim Passmore-2
Yeah I was trying to find it in the API, however I never looked under the ImageProcessor class.  Anyways, I am using the code below now and I have solved the problem:

ImageProcessor resultProcessor = resultImg.getProcessor();
Font biggerFont = new Font("Arial", Font.PLAIN, 100);
resultProcessor.setFont(biggerFont);
Reply | Threaded
Open this post in threaded view
|

Re: Easiest way to set font size of drawString()

Jim Passmore-2
On Tue, Jun 12, 2012 at 2:28 PM, alexwatson <[hidden email]> wrote:

> Yeah I was trying to find it in the API, however I never looked under the
> ImageProcessor class.
>

It can be kind of difficult.  I still don't know my way around the API that
well.  I've had some luck on the main developer page by putting a good
guess of search term into the search box for "browsable source".  From a
section of code, you can possibly track the method back to it's class.  A
couple other things to try are the macro recorder, (in javascript mode),
and, at least in Fiji, the Command Finder (ctrl-L, type a command, and
check "show full information") if there are menu equivalents.


> Anyways, I am using the code below now, but it isn't
> changing the font size, strangely:
>
> ImageProcessor resultProcessor = resultImg.getProcessor();
> Font biggerFont = new Font("Arial", Font.PLAIN, 100);
> resultProcessor.setFont(biggerFont);
>

Well, it's only a snippet, but I'll speculate.  The drawString method
actually changes pixels in the image, so after resultProcessor.setFont(),
you have to do resultProcessor.drawString().  setFont() only changes the
font size of FUTURE strings.  Finally, don't forget to do
resultImg.updateAndDraw() to refresh the display.

In a Jython script, the following works for me to change font size.  (There
has to be an open image, and you'll obviously have to convert to pure Java
for a plugin.)

from java.awt import Font
image = IJ.getImage()
resultProcessor = image.getProcessor()

for fontSize in [10,20,30,40,50]:
    biggerFont = Font("Arial", Font.PLAIN, fontSize)
    resultProcessor.setFont(biggerFont)
    resultProcessor.drawString("font size = "+ str(fontSize), 50,
fontSize*10)
    image.updateAndDraw()

Hope this gets you going in the right direction.

*Jim Passmore*
Research Associate
Sealed Air Corporation

--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html
Reply | Threaded
Open this post in threaded view
|

Re: Easiest way to set font size of drawString()

alexwatson
Thanks I have solved the issue.  Because I have a lot of code and it was largely un-commented, I didn't realize that I was setting the font after I had called the drawString() method for the strings in question.  Thanks for your replies. Lots of very good information.