我使用 Graphics2D 绘制了三个箭头。
- 三秒
drawLine
_ draw(Shape)
fill(Shape)
这是放大后的样子:
我无法理解两件事:
- 为什么填充的更小并移动?
- 其次,为什么箭头 1. 和 3. 看起来不同?两者都包含 3 条抗锯齿线。他们(可能)不应该只在顶点上有所不同吗?
这是整个代码:
import javax.swing.*;
import java.awt.*;
public class ShapeTest extends JPanel
{
public static void main(String [] args)
{
JFrame frame = new JFrame();
frame.setSize(new Dimension(220, 200));
frame.add(new ShapeTest());
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.setVisible(true);
}
@Override
protected void paintComponent(Graphics graphics)
{
super.paintComponent(graphics);
Graphics2D graphics2D = (Graphics2D)graphics;
graphics.setColor(Color.white);
graphics.fillRect(0, 0, this.getWidth(), this.getHeight());
graphics2D.setColor(Color.black);
graphics2D.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
graphics2D.drawLine(100, 40, 103, 46);
graphics2D.drawLine(103, 46, 106, 40);
graphics2D.drawLine(100, 40, 106, 40);
graphics2D.fill(new Polygon(
new int[]{100, 103, 106},
new int[]{50, 56, 50},
3
));
graphics2D.draw(new Polygon(
new int[]{100, 103, 106},
new int[]{60, 66, 60},
3
));
}
}