有没有办法在Swing中用亚像素精度绘制线条? [英] Is there any way to draw lines with subpixel precision in Swing?

查看:214
本文介绍了有没有办法在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);
    }
}

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

解决方案

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:

这篇关于有没有办法在Swing中用亚像素精度绘制线条?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆