这个问题之前已经被问过了,但是答案并没有解决问题,所以我再次提出。
有人建议使用 g2.draw(line)
替代 g2.drawLine
,其中 line
是一个 Line2D.Double
。但是,正如您从截图中可以看到的那样,线条仍然被绘制成像素的整数部分(每组10条线完全平行)。
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);
}
}
在Swing中,无法正确渲染未精确对齐到像素的直线吗?