好的,所以 Line2D 可以使用Rectangle2DGraphics2D.draw()代替Graphics.drawLine()Graphics.drawRectangle()
Graphics.drawPolyLine()是否有类似的“升级”?

最佳答案

看看 Path2D 。它是一个Shape,因此应该可以通过Graphics2D.draw()进行绘制。

用法示例:

import java.awt.*;
import java.awt.geom.Path2D;

import javax.swing.*;

public class FrameTestBase extends JFrame {

    public static void main(String args[]) {
        FrameTestBase t = new FrameTestBase();
        t.add(new JComponent() {
            public void paintComponent(Graphics g) {
                Path2D p = new Path2D.Double();
                p.moveTo(15, 15);
                p.lineTo(150, 75);
                p.lineTo(100, 10);
                p.lineTo(10, 100);

                ((Graphics2D) g).draw(p);
            }
        });

        t.setDefaultCloseOperation(EXIT_ON_CLOSE);
        t.setSize(200, 200);
        t.setVisible(true);
    }
}

07-28 13:30