Java - 绘制一个三角形

时间:2014-02-04 22:25:12

标签: java random geometry draw

嘿,我知道绘制椭圆/矩形并使用

填充它很简单
g.fillOval(30, 40, 20, 20);

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

2 个答案:

答案 0 :(得分:8)

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

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

您可以选择哪种产品可以满足您的要求。 Polygon要求您提前了解3D空间内各点的位置,Shape API可让您更自由地定义形状,而无需提前关注位置。

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

例如......

Triangle

红色是Polygon,绿色是Shape,它被翻译成位置......

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

    }

}

答案 1 :(得分:6)

使用遗留Graphics类,这可以通过使用遗留方法来完成 drawPolygon(int[] x, int[] y, int pointCount)

较新的类Graphics2D使用Path2D支持更好的实现。您需要使用draw(Shape)fill(Shape)方法。鉴于几乎在Java中的任何地方Graphics对象实际上都是Graphics2D对象,你可以这样做,这就是你要走的路,恕我直言。

Graphics g = ...;
Graphics2D g2d = (Graphics2D) g;
Path2D.Double triangle = new Path2D.Double();
triangle.moveTo(x1, y1);
triangle.pathTo(x2, y2);
triangle.pathTo(x3, y3);
triangle.closePath();
g2d.fill(triangle);