Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any way to draw lines with subpixel precision in Swing?

This question has been asked before, but the answers don't solve the problem, so I ask it again.

It was suggested that instead of using g2.drawLine, you could use g2.draw(line), where line is a Line2D.Double. However, as you can see from the screenshot, the lines are still drawn as if they ended on an integer pixel (each set of 10 lines is exactly parallel).

import javax.swing.*;
import java.awt.*;
import java.awt.geom.Line2D;

public class FrameTestBase extends JFrame {

    public static void main(String args[]) {
        FrameTestBase t = new FrameTestBase();
        t.add(new JComponent() {
            public void paintComponent(Graphics g) {
                Graphics2D g2 = (Graphics2D) g;
                g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                        RenderingHints.VALUE_ANTIALIAS_ON);
                for (int i = 0; i < 50; i++) {
                    double delta = i / 10.0;
                    double y = 5 + 5*i;
                    Shape l = new Line2D.Double(5, y, 200, y + delta);
                    g2.draw(l);
    // Base case, with ints, looks exactly the same:
    //              g2.drawLine(5, (int)y, 200, (int)(y + delta));
                }
            }
        });

        t.setDefaultCloseOperation(EXIT_ON_CLOSE);
        t.setSize(220, 300);
        t.setVisible(true);
    }
}

non-parallel lines

So is it impossible in Swing to correctly render lines that don't end exactly on a pixel?

like image 392
Luigi Plinge Avatar asked Sep 13 '12 22:09

Luigi Plinge


1 Answers

Try setting the following:

g2d.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_PURE); 

Taken from this answer: Java - Does subpixel line accuracy require an AffineTransform?

Here is the result for comparison:

enter image description here

like image 119
I82Much Avatar answered Oct 01 '22 19:10

I82Much