tags:

views:

20

answers:

1

Hello,

I paint a rounded rectangle with a spezified stroke and then i try to paint a single line around this rectangle. But at the edges it never really matches each other.

    g.setStroke(new BasicStroke(radius + .5f));
    g.drawRoundRect(x + radius/2, y + radius/2, width - radius, height - radius, radius, radius);

    // Outer border
    g.setColor(outer);
    g.setStroke(new BasicStroke(1));
    g.drawRoundRect(x, y, width - 1, height - 1, radius, radius);

Do you know how to calculate the outer border radius?

OK I tried a little bit and I got a better one, but still not perfekt;

    float scale = radius / 2.0f;
    g.setPaint( p );
    g.setStroke(new BasicStroke(2 * scale ));
    g.drawRoundRect(x + radius/2, y + radius/2, width - radius, height - radius, radius, radius);

    // Outer border
    g.setColor(outer);
    g.setStroke(new BasicStroke(1));
    g.drawRoundRect(x, y, width - 1, height - 1, Math.round(4*scale), Math.round(4*scale) );

Does anybody have a better one?

A: 

I don't think there is an easy way. (Not even if you implement your own rounded-rect drawing-routine.)

I would suggest you draw a slightly thicker outer-border first, then draw the inner "border" on top of that.

aioobe
In this case I still have the problem, that the border has different tickness at the edges. I just added a new piece of code, which works much better but not perfekt.
Christian
Yes. Thats what I would have expected. I'm not sure how to solve this. You could do it manually by filling in an outer border next to each inner-border-pixel. But such approach involves messing with raster data which is not exactly a one-liner...
aioobe