java: Graphics2D version of polyline?

淺唱寂寞╮ 提交于 2019-12-04 04:13:43

问题


OK, so there's a Line2D and a Rectangle2D that can be used by Graphics2D.draw() instead of Graphics.drawLine() and Graphics.drawRectangle().

Is there a similar "upgrade" for Graphics.drawPolyLine()?


回答1:


Have a look at Path2D. It is a Shape and should thus be able to be drawn through Graphics2D.draw().

Example usage:

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);
    }
}



来源:https://stackoverflow.com/questions/6217821/java-graphics2d-version-of-polyline

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!