9
votes

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?

1
Can you try g2d.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_PURE); ? stackoverflow.com/questions/5018635/…I82Much
You should draw the edge pixels in a different color (subpixel) / graytone (non-subpixel antialiasing).ninjalj
@I82Much That seems to work! You should add this as an answer!Luigi Plinge
Done. Could you post an updated screenshot illustrating the difference?I82Much
@I82Much thanks, posted updated image belowLuigi Plinge

1 Answers

6
votes

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