Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Drawing text with outline in java

I'm working with graphcis2d in Java and am currently using this to draw text into a bufferedImage

Font font1 = new Font("Arial", Font.PLAIN, 120);
g2d.setFont(font1);
FontMetrics fm1 = g2d.getFontMetrics(font1);     
g2d.drawString(s[1], width/2-fm1.stringWidth(s[1])/2, height/4-70);

I want to draw this text with an different color outline.

GlyphVector gv = font1.createGlyphVector(g2d.getFontRenderContext(), s[1]);
Shape shape = gv.getOutline();
g2d.setStroke(new BasicStroke(4.0f));
g2d.translate(width/2-fm1.stringWidth(s[1])/2, height/4-70);
g2d.draw(shape);        

The problem with using this method, which works, is that I am working with arabic characters and using GlyphVector reverses the order and doesn't make the letters flow with one another.

Can someone please explain to me how to render arabic text in one color and have an outline with another?

Heres a sample of the text I would be using: الرحمن

like image 524
Kam Avatar asked Sep 19 '11 02:09

Kam


2 Answers

One trick is to draw the text several times in the outline color, varying the position by the outline width in +/- x and +/- y directions, then draw in the foreground color at the nominal position. It isn't perfect, but it tends to look pretty good provided the outline isn't too thick with respect to the stroke width of the letters.

like image 105
Ted Hopp Avatar answered Oct 21 '22 06:10

Ted Hopp


You may be able to use the method createStrokedShape() on the glyph's Shape returned by getOutline(). See also CompositeStroke, demonstrated here.

like image 2
trashgod Avatar answered Oct 21 '22 08:10

trashgod