3

I'm having a problem with graphics 2d stroke, it seems no one has this problem since I have searched for something alike and no results. Here is the image.

enter image description here

As you can see, there are spikes on my stroke, I don't want those. Here is my code.

The class is extended to JButton and the method is paintComponent.

    Graphics2D g2d = (Graphics2D)g2.create();
    TextLayout tl = new TextLayout(getText(), getFont(), g2d.getFontRenderContext());
    Shape to = tl.getOutline(null);
    int x = (getSize().width-to.getBounds().width)/2;
    int y = (getSize().height+(to.getBounds().height-8))/2;
    System.out.println(to.getBounds().height);
    g2d.translate(x, y);
    g2d.setStroke(new BasicStroke(15.0f));
    g2d.setColor(new Color(155,155,155));
    g2d.draw(to);
    g2d.dispose();

When lowering the stroke thickness, the spike goes smaller too.

Peter O.
  • 32,158
  • 14
  • 82
  • 96
xchan
  • 37
  • 5
  • 2
    This looks more like an issue with the fonts design than a problem of Graphics2D. First thing I would check is use different font - I presume the font is badly designed. – Durandal Apr 26 '14 at 16:27
  • Haha it would be bad to use malfunctions for design but it looks cool thou. Never though of that Durandal, i will try it – xchan Apr 26 '14 at 17:11
  • 1
    Almost all fonts have spikes too, most with no spikes I dont really like. *sigh* – xchan Apr 26 '14 at 17:46
  • @xchan Is there a notable difference between serif and sans-serif fonts? – user1803551 Apr 26 '14 at 18:20
  • I change my codes a bit, and the stroke i left it as 1. It looks decent enuf. – xchan Apr 26 '14 at 20:11

2 Answers2

1

Have you tried using a BasicStroke with JOIN_BEVEL or JOIN_ROUND? The default join you are using is JOIN_MITER, which may be responsible for those ugly (cool?) spikes.

Franz D.
  • 1,061
  • 10
  • 23
0

Try this

Stroke stroke = new BasicStroke(1,
    BasicStroke.CAP_BUTT, BasicStroke.JOIN_BEVEL, 0,
    null, 0);
g2.setStroke(stroke);

Like Franz just said this is one of those unruly behaviors of JOIN_MITER. a mitered join will extend far beyond the actual intersection of the lines.

katwekibs
  • 1,342
  • 14
  • 17