3

I'm working with a JEditorPane, and (of course) the text looks so much nicer when I paint the component using antialiasing and fractional widths. In other words:

JEditorPane pane = new JEditorPane() {
    public void paint(Graphics g) {
        Graphics2D g2d = (Graphics2D)g;
        g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
            RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
        g2d.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS,
            RenderingHints.VALUE_FRACTIONALMETRICS_ON);
        super.paint(g);
    }
}

If I do this with static, non-selectable text, it paints beautifully. However, as soon as I start dragging a selection over it, I get some weird jiggling between frames, and artifacts. Is there any way to paint with the fractional metrics and avoid that weirdness? I tried setting the attribute on the Caret as well with no luck.

mKorbel
  • 109,525
  • 20
  • 134
  • 319
AndyI
  • 133
  • 1
  • 5
  • 1
    OK, I found the answer here: http://java-sl.com/Scale_In_JEditorPane.html (That guy has some great articles, if you're really digging into text components in Java, btw...) The solution is to add this call: pane.getDocument().putProperty("i18n", Boolean.TRUE); which forces the glyphs to be drawn with GlyphPainter2 instead of the usual GlyphPainter1. The more you know! – AndyI Feb 07 '12 at 22:10
  • right I think nobody better for JTextComponents around as (@StanislavL) http://stackoverflow.com/users/301607/stanislavl – mKorbel Feb 07 '12 at 22:30
  • 1
    @StanislavL is a regular contributor, so I defer to him. In the meantime you might prepare an [sscce](http://sscce.org/) that exhibits the problem you describe. This [one](http://stackoverflow.com/a/8534162/230513), for example, does not. – trashgod Feb 07 '12 at 23:21
  • The Andyl answer is correct. If you set the "i18n" to TRUE the effect isn't visible. Just wrote a small test case. Alternatively you can use the http://java-sl.com/Scale_In_JEditorPane_GlyphPainter.html to add your own fractional based painter. – StanislavL Feb 08 '12 at 08:42
  • 3
    Nice work AndyI. Can you please post an answer to the question yourself and then accept that answer so that we can close this question? Also, you need to accept answers to previous questions if they fix your problem. – Zecas May 24 '12 at 09:40
  • Ugh, the stuff that's hidden in Swing..."i18n" sounds like it would affect other things too, does it? – Andy Jan 10 '14 at 04:43

1 Answers1

0

This question seems to be answered long ago by the author and StanislavL in the comments:

If you set the "i18n" to TRUE the effect isn't visible. Just wrote a small test case. Alternatively you can use the java-sl.com/Scale_In_JEditorPane_GlyphPainter.html to add your own fractional based painter.

SebastianH
  • 2,172
  • 1
  • 18
  • 29