您是否将Graphics
对象转换为Graphics2D
并设置RenderHints
?IE
Graphics2D g2d=(Graphics2D)g;
g2d.setRenderingHint(RenderHints.KEY_ANTIALIASING,RenderHints.VALUE_ANTIALIASING _ON);
这将添加一些很好的抗锯齿效果,并可能使线条看起来更直。
通过增加笔画宽度Graphics2D#setStroke
也会使锯齿状边缘消失,因为它现在更厚了。
看这个例子(按下、拖动和释放鼠标来创建一条线):
Withg2d.setRenderingHint(..)
和g2d.setStroke(..)
insidepaintComponent(..)
注释掉:
带有g2d.setRenderingHint(..)
和g2d.setStroke(..)
未注释:
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.RenderingHints;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.geom.Line2D;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class Test {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
Test gui = new Test();
}
});
}
public Test() {
initComponents();
}
private void initComponents() {
JFrame frame = new JFrame("Line Demo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new MyPanel());
frame.pack();
frame.setVisible(true);
}
}
class MyPanel extends JPanel {
Point point1;
Point point2;
Line2D line2d;
public MyPanel() {
addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent me) {
super.mousePressed(me);
point1 = me.getPoint();
}
});
addMouseMotionListener(new MouseAdapter() {
@Override
public void mouseDragged(MouseEvent me) {
super.mouseDragged(me);
point2 = me.getPoint();
line2d = new Line2D.Double(point1, point2);
repaint();
}
});
}
@Override
public Dimension getPreferredSize() {
return new Dimension(300, 300);
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
//Set anti-alias!
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
if (point1 != null && point2 != null) {
g2d.setPaint(Color.RED);
g2d.setStroke(new BasicStroke(1.5f));//set stroke size
g2d.draw(line2d);
}
}
}
如果以上没有帮助,发布 SSCCE 将使我们能够测试并查看可能出现的问题/使其变得更好。