Java 使用drawpolygon方法绘制等边三角形?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20020799/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 22:22:59  来源:igfitidea点击:

Drawing equilateral triangle using drawpolygon method?

java

提问by user2999690

How can I draw an equilateral triangle using the graphics method especially the draw polygon method. My issue is that to make an equilateral triangle I need the square root 3/2 and the drawPolygon method I can only use int[],int[],int, the compiler will not let me double because it differ in lengh.

如何使用图形方法尤其是绘制多边形方法绘制等边三角形。我的问题是,要制作一个等边三角形,我需要平方根 3/2 和 drawPolygon 方法,我只能使用 int[],int[],int,编译器不会让我加倍,因为它的长度不同。

Any help is appreciated.

任何帮助表示赞赏。

         import java.awt.*;

          public class Triangle extends Shape {
         // Instance variables
           private int leng;


       // Constructor
       public Triangle(int x, int y, Color color,
               int leng) {
               super(x, y, color);
               this.leng=leng;
         }
        // Instance methods
           public void draw(Graphics g) {
               double[] Xcoord = { getX(), getX() + leng, getX() + leng / 2};
             double[] Ycoord = { getY(), getY(), getY()*(1.0+ Math.sqrt(3) / (2.0))};
              g.drawPolygon(Xcoord,Ycoord,3);

       }

        public int getHeight() {
              return leng;
        }

         public int getWidth() {
             return leng;
           }
           }

回答by Peter_James

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Polygon;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class JRisk {

    private JFrame mainMap;
    private Polygon poly;

    public JRisk() {

        initComponents();

    }

    private void initComponents() {

        mainMap = new JFrame();
        mainMap.setResizable(false);

        mainMap.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

        int xPoly[] = {150, 250, 325, 375, 450, 275, 100};
        int yPoly[] = {150, 100, 125, 225, 250, 375, 300};

        poly = new Polygon(xPoly, yPoly, xPoly.length);
        JPanel p = new JPanel() {
            @Override
            protected void paintComponent(Graphics g) {
                super.paintComponent(g);
                g.setColor(Color.BLUE);
                g.drawPolygon(poly);
            }

            @Override
            public Dimension getPreferredSize() {
                return new Dimension(800, 600);
            }
        };
        mainMap.add(p);
        mainMap.pack();
        mainMap.setVisible(true);

    }

    /**
     * @param args
     */
    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new JRisk();
            }
        });
    }
}

This was found from about drawing a Polygon in javaafter searching in google.

这是在谷歌搜索后从关于在java中绘制多边形中发现的。