This question has been asked before, ma le risposte non risolvono il problema, quindi lo chiedo nuovamente.C'è un modo per disegnare linee con precisione subpixel in Swing?
È stato suggerito che invece di utilizzare g2.drawLine
, è possibile utilizzare g2.draw(line)
, dove line
è un Line2D.Double
. Tuttavia, come puoi vedere dallo screenshot, le linee sono ancora disegnate come se fossero finite su un pixel intero (ogni serie di 10 linee è esattamente parallela).
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);
}
}
Quindi è impossibile in Swing per rendere correttamente le linee che non terminano esattamente su un pixel?
Puoi provare g2d.setRenderingHint (RenderingHints .KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_PURE); ? http://stackoverflow.com/questions/5018635/java-does-subpixel-line-accuracy-require-an-affinetransform – I82Much
Dovresti disegnare i pixel del bordo in un colore diverso (subpixel)/grigio (antialiasing non sottopixel). – ninjalj
@ I82Much Sembra funzionare! Dovresti aggiungere questo come risposta! –