简体   繁体   English

在 Java 中使用 GeneralPath 和 lang.Math(GUI) 创建多边形

[英]Creating a multiple sided shape using GeneralPath and lang.Math(GUI) in Java

I want to create a shape with mulitple sides using Lang.Math and GeneralPath in Java GUI.我想在 Java GUI 中使用 Lang.Math 和 GeneralPath 创建一个多边形状。 I was able to transfer the value of ne(the number of sides) from Fenster to my class but the code isnt working.我能够将 ne(边数)的值从 Fenster 转移到我的 class,但代码无法正常工作。 ik the code for the shape is right because it was same code i used for hexagon. ik 形状代码是正确的,因为它与我用于六边形的代码相同。 i think the problem might be that the number sides is in variable form(because if i change the variable with a number it seems to work) but idk how to fix it.我认为问题可能是数字面是可变形式的(因为如果我用数字更改变量似乎有效)但我不知道如何修复它。 it is showing the following error Exception in thread "AWT-EventQueue-0" "java.lang.ArrayIndexOutOfBoundsException: Index 0 out of bounds for length 0 at Neck.genneck(Neck.java:30) at Zeichnung.paintComponent(Zeichnung.java:104)" Please help me它在线程 "AWT-EventQueue-0" "java.lang.ArrayIndexOutOfBoundsException: Index 0 out of bounds for length 0 at Neck.genneck(Neck.java:30) at Zeichnung.paintComponent(Zeichnung.java) 中显示以下错误异常:104)" 请帮助我

**this is the code ** **这是代码**

import javax.swing.*;
import java.awt.geom.GeneralPath;

public class Neck extends Form {
    int r;
    private int ne ;
    private double[] px, py;
    
    

    public Neck(int x, int y, int r) {
        super(x,y);
        this.r=r;
        px=new double[ne];
        py=new double[ne];


    }
    
    public void seiten (int ne) {
        this.ne=ne;
    }
    

    public Shape genneck() {
        for(int i = 0; i<ne; i++) {
             
            px[i]=x+(r*Math.sin(Math.toRadians(i*360/ne)));
            py[i]=y-(r*Math.sin(Math.toRadians(i*360/ne)));
        }


        GeneralPath path = new GeneralPath(GeneralPath.WIND_NON_ZERO);
        path.moveTo(px[0],py[0]);
        for(int i = 1; i<ne; i++) {
            path.lineTo(px[i],py[i]);
        }
        path.closePath();
        return path;
    }

   }

To stop the compiler complaining, I had to provide this Form class:为了阻止编译器抱怨,我必须提供此Form class:

public class Form {
    protected int x;
    protected int y;
    protected Form(int x, int y) {
        this.x = x;
        this.y = y;
    }
}

Also an import was missing in your code:您的代码中还缺少导入:

import java.awt.Shape;

The problem with your ne variable is that it is not initialized in the constructor, so the default will be 0 , and your px and py arrays will always be of length 0. Also the method seiten is problematic, because you will modify the length for the loop but not recreate the arrays px and py , so it will for sure raise an IndexOutOfBoundsException in the genneck method.您的ne变量的问题在于它未在构造函数中初始化,因此默认0 ,并且您的pxpy arrays 的长度始终为 0。方法seiten也有问题,因为您将修改长度循环但不重新创建 arrays pxpy ,因此它肯定会在genneck方法中引发IndexOutOfBoundsException

Additionally I would recommend that you remove the unnecessary import:此外,我建议您删除不必要的导入:

import java.swing.*;

You also should split the declarations of px and py for better readability, and make the 360 in the calculations of px and py into explicit double values ( 360.0 ) to ensure that the division does not result in an integer and is casted then (this will let you loose all digits after the decimal separator):您还应该拆分pxpy的声明以获得更好的可读性,并将pxpy的计算中的360转换为显式double精度值 ( 360.0 ),以确保该除法不会导致 integer 并随后进行转换(这将让你松开小数点后的所有数字):

px[i] = x + (r * Math.sin(Math.toRadians(i * 360.0 / ne)));
py[i] = y - (r * Math.sin(Math.toRadians(i * 360.0 / ne)));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM