Filled Background when drawing a string

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

Filled Background when drawing a string

Johannes Weissmann
Hi,
I was wondering if there is a possiblility to set a background color
when drawing a string using the functions of an image processor.
Currently I am using a line like

ip.drawString(string);

I have to update the text several times and tried to avoid creating a
TextRoi all the time. Does anyone know a quick solution or do I have to
use TextROIs?

Best regards,

Johannes

--
Johannes Weissmann

Nedre Ferstadveg 26 | 7023 Trondheim
mail: [hidden email]
fon: +47 45 124 191

"The truth, as always, will be far stranger." [Sir Arthur C. Clarke]
Reply | Threaded
Open this post in threaded view
|

Re: Filled Background when drawing a string

dscho
Hi Johannes,

On Mon, 13 Jun 2011, Johannes Weissmann wrote:

> I was wondering if there is a possiblility to set a background color
> when drawing a string using the functions of an image processor.
> Currently I am using a line like
>
> ip.drawString(string);
>
> I have to update the text several times and tried to avoid creating a
> TextRoi all the time. Does anyone know a quick solution or do I have to
> use TextROIs?

If you are already using Javascript or Java (as suggested by the snippet),
you could draw the string with (x +- 1, y +-1) and in the background color
to simulate an outline prior to drawing the string in the foreground
color.

If that is not what you meant, you can get the bounding box of a text ROI,
possibly extend it, and fill it before drawing the string.

In both cases, I would recommend making this functionality a method. Then
it is not _that_ bad if you have to do it a couple of times.

Ciao,
Johannes
Reply | Threaded
Open this post in threaded view
|

Re: Filled Background when drawing a string

Johannes Weissmann
On 13.06.2011 15:58, Johannes Schindelin wrote:

> Hi Johannes,
>
> On Mon, 13 Jun 2011, Johannes Weissmann wrote:
>
>> I was wondering if there is a possiblility to set a background color
>> when drawing a string using the functions of an image processor.
>> Currently I am using a line like
>>
>> ip.drawString(string);
>>
>> I have to update the text several times and tried to avoid creating a
>> TextRoi all the time. Does anyone know a quick solution or do I have to
>> use TextROIs?
>
> If you are already using Javascript or Java (as suggested by the snippet),
> you could draw the string with (x +- 1, y +-1) and in the background color
> to simulate an outline prior to drawing the string in the foreground
> color.
>
> If that is not what you meant, you can get the bounding box of a text ROI,
> possibly extend it, and fill it before drawing the string.
>
> In both cases, I would recommend making this functionality a method. Then
> it is not _that_ bad if you have to do it a couple of times.
>
> Ciao,
> Johannes

Hi Johannes,
thanks, I guess I'll have to switch to TextROIs. I wanted to avoid
creating TextROIs getting the bounding box because I have lots of
strings per picture and thought that it might slow the plugin down.

ciao Johannes

--
Johannes Weissmann

Nedre Ferstadveg 26 | 7023 Trondheim
mail: [hidden email]
fon: +47 45 124 191

"The truth, as always, will be far stranger." [Sir Arthur C. Clarke]
Reply | Threaded
Open this post in threaded view
|

Re: Filled Background when drawing a string

Gabriel Landini
On Monday 13 Jun 2011 15:06:15 you wrote:
> thanks, I guess I'll have to switch to TextROIs. I wanted to avoid
> creating TextROIs getting the bounding box because I have lots of
> strings per picture and thought that it might slow the plugin down.

For those willing to submit patches (hint hint!), it would be nice to have
logic operators in drawing mode. For instance, if you drew the string with
colour n in XOR mode, then you almost-always see it regardless of the
background colour.
Same for drawing lines and filling.
The nice extra is that when you paint it again exactly in the same place, then
what was drawn in the first instance gets erased and the background restored.

Cheers
Gabriel
Reply | Threaded
Open this post in threaded view
|

Re: Filled Background when drawing a string

Rasband, Wayne (NIH/NIMH) [E]
In reply to this post by Johannes Weissmann
On Jun 13, 2011, at 8:08 AM, Johannes Weissmann wrote:

> Hi,
> I was wondering if there is a possiblility to set a background color
> when drawing a string using the functions of an image processor.
> Currently I am using a line like
>
> ip.drawString(string);
>
> I have to update the text several times and tried to avoid creating a
> TextRoi all the time. Does anyone know a quick solution or do I have to
> use TextROIs?

There is an example macro at

   http://imagej.nih.gov/ij/macros/examples/DrawTextWithBackground.txt

that demonstrates how to draw text with a filled background.

-wayne
Reply | Threaded
Open this post in threaded view
|

Re: Filled Background when drawing a string

Johannes Weissmann
In reply to this post by Johannes Weissmann
On 14.06.2011 04:17, Rasband, Wayne (NIH/NIMH) [E] wrote:

>> Hi Wayne,
>> I have already a working version using a text ROI and getting the
>> bounding box, so don't waste time on that.
>> I was simply wondering if I there is a build-in function to set the
>> background color for a string. My impression is that creating a text
>> ROI, getting bounding boxes is maybe a little overhead for such a
>> simple task. Especially since I was working with the java.Font classes
>> which leads to some awkward lines of code (e.g. there's no method to
>> set the font that takes a java.Font as argument).
>
> There is no built-in function to draw a string with a background.
>
> I got the JavaScript example working. It's at
>
>     http://imagej.nih.gov/ij/macros/js/DrawTextWithBackground.js
>
> It should be easy to convert the function in the example to Java.
>
>> I am pretty new to ImageJ and not full professional software engineer,
>> but if there is some need I'd be happy to contribute some of my
>> developments.
>
> It would be helpful to have an example that draws a string with background using TextRois.
>
> Best regards,
>
> -wayne
>
>
>

Hi,
I implemented the following solution now. I run into some trouble even
with the TextROI method. The bounding rectangle (bounds() ) was always
only 1px wide and high.

        /**
         * Draws a string at the specified location with a filled
          * background.
         * @param ip
         * @param string
         * @param x
         * @param y
         * @param value Color value for the string.
         */
        private void drawString(ImageProcessor ip, String string, int
            x, int y, double value) {
                Font font = new Font(Font.MONOSPACED, Font.PLAIN, 13);

                // ip needs a function getJustification!
                ip.setJustification(ImageProcessor.CENTER_JUSTIFY);

                 //Rectangle bounds = text.getBounds();
                //ip.drawRect(x, y, bounds.width, bounds.height); // ->
                     // width&height == 1 ???
                Graphics g = ip.getBufferedImage().getGraphics();
                FontMetrics metrics = g.getFontMetrics(font);

               
                int width = ip.getStringWidth(string);
                int height = metrics.getAscent();
               
                Roi roi = new Roi((int) x-(width/2), y-height, width,
                    height);
                ip.setColor(Color.BLACK);
                ip.fill(roi);
               

                //ip.drawRect((int) x-(width/2), y-height, width,
                    height);
                ip.setValue(value);
                ip.drawString(string, x, y);
}

Cheers, Jo
--
Johannes Weissmann

Nedre Ferstadveg 26 | 7023 Trondheim
mail: [hidden email]
fon: +47 45 124 191

"The truth, as always, will be far stranger." [Sir Arthur C. Clarke]