<legend id='PvCRn'><style id='PvCRn'><dir id='PvCRn'><q id='PvCRn'></q></dir></style></legend>

      <i id='PvCRn'><tr id='PvCRn'><dt id='PvCRn'><q id='PvCRn'><span id='PvCRn'><b id='PvCRn'><form id='PvCRn'><ins id='PvCRn'></ins><ul id='PvCRn'></ul><sub id='PvCRn'></sub></form><legend id='PvCRn'></legend><bdo id='PvCRn'><pre id='PvCRn'><center id='PvCRn'></center></pre></bdo></b><th id='PvCRn'></th></span></q></dt></tr></i><div id='PvCRn'><tfoot id='PvCRn'></tfoot><dl id='PvCRn'><fieldset id='PvCRn'></fieldset></dl></div>
    1. <small id='PvCRn'></small><noframes id='PvCRn'>

        <tfoot id='PvCRn'></tfoot>

        • <bdo id='PvCRn'></bdo><ul id='PvCRn'></ul>

        Java - 画一个三角形

        时间:2023-10-01
        <tfoot id='mBe9m'></tfoot>
          <i id='mBe9m'><tr id='mBe9m'><dt id='mBe9m'><q id='mBe9m'><span id='mBe9m'><b id='mBe9m'><form id='mBe9m'><ins id='mBe9m'></ins><ul id='mBe9m'></ul><sub id='mBe9m'></sub></form><legend id='mBe9m'></legend><bdo id='mBe9m'><pre id='mBe9m'><center id='mBe9m'></center></pre></bdo></b><th id='mBe9m'></th></span></q></dt></tr></i><div id='mBe9m'><tfoot id='mBe9m'></tfoot><dl id='mBe9m'><fieldset id='mBe9m'></fieldset></dl></div>

        • <legend id='mBe9m'><style id='mBe9m'><dir id='mBe9m'><q id='mBe9m'></q></dir></style></legend>
            <tbody id='mBe9m'></tbody>

              • <small id='mBe9m'></small><noframes id='mBe9m'>

                  <bdo id='mBe9m'></bdo><ul id='mBe9m'></ul>
                • 本文介绍了Java - 画一个三角形的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  嘿,我知道绘制椭圆/矩形并使用填充它很简单

                  Hey I know that it is simple to draw oval/rectangle and fill it using

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

                  但是如何画一个三角形呢?最好有随机坐标.

                  but how to draw a triangle? It would be the best if it would have random coordinates.

                  推荐答案

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

                  There are at least two basics ways you can achieve this, based on your needs.

                  您可以使用 Polygon 或者你可以使用 2D Graphics Shape API

                  您可能会选择哪一种取决于您的要求.Polygon 要求您提前知道点在 3D 空间中的位置,而 Shape API 让您可以更自由地定义形状,而无需事先关心位置.

                  Which you might choose comes down to your requirements. Polygon requires you to know, in advance the position of the points within 3D space, where the Shape API gives you more freedom to define the shape without caring about the position in advance.

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

                  This makes the Shape API more flexible, in that you can define the shape once and simply translate the Graphics context as needed and repaint it.

                  例如...

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

                  Red is a Polygon, green is a Shape, which is translated into position...

                  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 - 画一个三角形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:动圈与非动圈的JAVA弹性碰撞 下一篇:动态规划与背包应用

                  相关文章

                  最新文章

                  <legend id='uTKzB'><style id='uTKzB'><dir id='uTKzB'><q id='uTKzB'></q></dir></style></legend>
                • <i id='uTKzB'><tr id='uTKzB'><dt id='uTKzB'><q id='uTKzB'><span id='uTKzB'><b id='uTKzB'><form id='uTKzB'><ins id='uTKzB'></ins><ul id='uTKzB'></ul><sub id='uTKzB'></sub></form><legend id='uTKzB'></legend><bdo id='uTKzB'><pre id='uTKzB'><center id='uTKzB'></center></pre></bdo></b><th id='uTKzB'></th></span></q></dt></tr></i><div id='uTKzB'><tfoot id='uTKzB'></tfoot><dl id='uTKzB'><fieldset id='uTKzB'></fieldset></dl></div>

                  <small id='uTKzB'></small><noframes id='uTKzB'>

                    <bdo id='uTKzB'></bdo><ul id='uTKzB'></ul>
                  <tfoot id='uTKzB'></tfoot>