开发者

Drawing text with outline in java

开发者 https://www.devze.com 2023-04-05 23:14 出处:网络
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);

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 Gl开发者_如何学运维yphVector 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: الرحمن


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.


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


Try to use

layoutGlyphVector(FontRenderContext frc, char[] text, int start, int limit, int flags) 

instead of the createGlyphVector

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号