Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
用java绘制三角形_Java_Geometry_Awt_Java 2d - Fatal编程技术网

用java绘制三角形

用java绘制三角形,java,geometry,awt,java-2d,Java,Geometry,Awt,Java 2d,因此,我的作业的一部分是创建一个三角形类来链接到各种按钮……但我不知道如何在eclipse中创建一个。具体说明如下: 创建一个三角形类 数据字段:点[]坐标 建设者 实现超类中定义的所有抽象方法 每个数据字段的getter和setter 替代公共空白绘制(图形arg0)方法 我在其他班上都安排好了,除了三角班。我对如何使用点数组创建三角形感到困惑…我需要使用点x,y还是在一个数组变量coords中存储3(x,y)个坐标对?我想你会用drawPolygon来创建它,但我不确定。有什么提示吗?使用g

因此,我的作业的一部分是创建一个三角形类来链接到各种按钮……但我不知道如何在eclipse中创建一个。具体说明如下:

创建一个三角形类

  • 数据字段:点[]坐标
  • 建设者
  • 实现超类中定义的所有抽象方法
  • 每个数据字段的getter和setter
  • 替代公共空白绘制(图形arg0)方法

  • 我在其他班上都安排好了,除了三角班。我对如何使用点数组创建三角形感到困惑…我需要使用点x,y还是在一个数组变量coords中存储3(x,y)个坐标对?我想你会用drawPolygon来创建它,但我不确定。有什么提示吗?

    使用
    g.drawPolygon
    ,它将
    点的数组作为参数。

    下面是一个三角形的示例类

    public class Triangle {
    
      private Point[] coords;
    
      // Null object constructor
      public Triangle() {
        this.coords = null;
      }
    
      // Constructor with point array
      public Triangle(Point[] coords) {
        this.coords = coords;
      }
    
      // Constructor with multiple points
      public Triangle(Point a, Point b, Point c) {
        this.coords = new Point[3];
        coords[0] = a;
        coords[1] = b;
        coords[2] = c;
      }
    
      // The actual paint method
      public void paint(Graphics arg0) {
        // Setup local variables to hold the coordinates
        int[] x = new int[3];
        int[] y = new int[3];
        // Loop through our points
        for (int i = 0; i < coords.length; i++) {
            Point point = coords[i];
            // Parse out the coordinates as integers and store to our local variables
            x[i] = Double.valueOf(point.getX()).intValue();
            y[i] = Double.valueOf(point.getY()).intValue();
        }
        // Actually commit to our polygon
        arg0.drawPolygon(x, y, 3);
      }
    }
    
    公共类三角形{
    专用点[]坐标;
    //空对象构造函数
    公共三角(){
    this.coords=null;
    }
    //带点数组的构造函数
    公共三角(点[]坐标){
    this.coords=coords;
    }
    //具有多个点的构造函数
    公共三角形(点a、点b、点c){
    this.coords=新点[3];
    coords[0]=a;
    coords[1]=b;
    coords[2]=c;
    }
    //实际绘制方法
    公共空白绘制(图形arg0){
    //设置局部变量以保存坐标
    int[]x=新的int[3];
    int[]y=新的int[3];
    //通过我们的点循环
    for(int i=0;i

    不确定这个类到底应该扩展什么,所以没有任何东西被标记为覆盖或任何东西,它缺少setter和accessor,但您应该能够使它工作。

    做了类似的事情,我画了一个三边的多边形。可能会有帮助

    for (int i = 0; i < 3; i++){
      polygon1.addPoint(
        (int) (40 + 50 * Math.cos(i * 2 * Math.PI / 3)),
        (int) (150 + 50 * Math.sin(i * 2 * Math.PI / 3))
      );
    }
    g.drawPolygon(polygon1);
    
    for(int i=0;i<3;i++){
    polygon1.addPoint(
    (int)(40+50*Math.cos(i*2*Math.PI/3)),
    (整数)(150+50*Math.sin(i*2*Math.PI/3))
    );
    }
    g、 多角形(polygon1);
    
    太棒了,这正是我需要的。我想我现在知道怎么做了。非常感谢:Dwhy
    x[i]=Double.valueOf(point.getX()).intValue()?为什么不
    x[i]=point.getX()