Graphics2D を使用して 3 つの矢印を描画しました。
- 3
drawLine
秒 draw(Shape)
fill(Shape)
拡大するとこんな感じ。
私は2つのことを理解できません:
- 塗りつぶされたものが小さく、ずれているのはなぜですか?
- 次に、矢印 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
));
}
}