Java – 绘制一个三角形

参见英文答案 > Triangle Draw Method                                    7个
嘿,我知道绘制椭圆/矩形并使用填充它很简单

g.fillOval(30, 40, 20, 20);

但如何绘制三角形?如果它有随机坐标,那将是最好的.

解决方法:

根据您的需求,至少有两种基本方法可以实现这一目标.

您可以使用Polygon,也可以使用2D Graphics Shape API

您可以选择哪种产品可以满足您的要求. Polygon要求您事先知道3D空间中点的位置,Shape API可让您更*地定义形状,而无需提前关注位置.

这使得Shape API更加灵活,因为您可以定义一次形状,并根据需要简单地翻译Graphics上下文并重新绘制它.

例如…

红色是多边形,绿色是形状,它被翻译成位置……

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Polygon;
import java.awt.geom.Path2D;
import java.awt.geom.Point2D;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class TriangleTest {

    public static void main(String[] args) {
        new TriangleTest();
    }

    public TriangleTest() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new BorderLayout());
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        private TriangleShape triangleShape;
        private Polygon poly;

        public TestPane() {
            triangleShape = new TriangleShape(
                    new Point2D.Double(50, 0),
                    new Point2D.Double(100, 100),
                    new Point2D.Double(0, 100)
            );

            poly = new Polygon(
                    new int[]{50, 100, 0},
                    new int[]{0, 100, 100},
                    3);
        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(200, 200);
        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2d = (Graphics2D) g.create();
            g2d.setColor(Color.RED);
            g2d.fill(poly);

            g2d.setColor(Color.GREEN);
            g2d.translate(50, 100);
            g2d.fill(triangleShape);
            g2d.dispose();
        }
    }

    public class TriangleShape extends Path2D.Double {

        public TriangleShape(Point2D... points) {
            moveTo(points[0].getX(), points[0].getY());
            lineTo(points[1].getX(), points[1].getY());
            lineTo(points[2].getX(), points[2].getY());
            closePath();
        }

    }

}
上一篇:java – 如何计算两个角度测量值的差异?


下一篇:Java:基于距离绘制点