2012-09-13 60 views
8

This question has been asked before,但答案沒有解決問題,所以我再問一次。有什麼辦法可以在Swing中繪製亞像素精度的線?

建議您不要使用g2.drawLine,而應使用g2.draw(line),其中lineLine2D.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); 
    } 
} 

non-parallel lines

因此,它是不可能在Swing正確渲染不完全結束在像素上線?

+1

你可以嘗試g2d.setRenderingHint(RenderingHints的.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_PURE); ? http://stackoverflow.com/questions/5018635/java-does-subpixel-line-accuracy-require-an-affinetransform – I82Much

+0

您應該繪製不同顏色(子像素)/灰度(非亞像素抗鋸齒)的邊緣像素。 – ninjalj

+0

@ I82Much這似乎工作!你應該添加這個答案! –

回答

相關問題