-2

I want to create a shape with mulitple sides using Lang.Math and GeneralPath in Java GUI. I was able to transfer the value of ne(the number of sides) from Fenster to my class but the code isnt working. ik the code for the shape is right because it was same code i used for hexagon. 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

**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;
    }

   }
Giga
  • 1
  • 1
  • 1
    Do some debugging. In the for loop, print out the values held by your array. Are they as expected? – Hovercraft Full Of Eels Dec 26 '22 at 14:09
  • @HovercraftFullOfEels it is not letting me debug it , it is showing that the problem lies at line px[i]=x+(r*Math.sin(Math.toRadians(i*360/ne))); – Giga Dec 26 '22 at 14:23
  • 1
    What do you mean "it's saying..."? does it not compile? If so, your question should show the error message – Hovercraft Full Of Eels Dec 26 '22 at 14:42
  • Does your super class have private fields for x and y? If so you'll need to change them to protected or else call a getter method. Regardless, please ask a more complete question – Hovercraft Full Of Eels Dec 26 '22 at 14:45
  • @HovercraftFullOfEels sorry I meant it is not compiling and showing error, and no the x and y fields are protected and they work with other shapes I coded. – Giga Dec 26 '22 at 15:19
  • and my question is what can i do to fix it – Giga Dec 26 '22 at 15:23
  • Edit your question and show the compiler's error message. Again, it is very important – Hovercraft Full Of Eels Dec 26 '22 at 15:38
  • 1
    Is the problem "cannot find symbol" due to not importing Shape? Best not for us to guess though. Please show us the message – DontKnowMuchBut Getting Better Dec 26 '22 at 15:54
  • @DontKnowMuchButGettingBetter this was the error message(the first 3 lines the error message was very long) **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)** (Zeichnung is the class where I export the shape to) – Giga Dec 26 '22 at 16:49
  • took me a while but the naming is in German, right? 'Neck' is for n-Eck (a polygon with n sides), 'seiten' to set the number of sides. (Not that it makes any difference for the problem, but often helps me to understand the goal and the problems reaching it) – cyberbrain Dec 27 '22 at 21:57

1 Answers1

0

To stop the compiler complaining, I had to provide this 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.

To solve this you should

  • initialize the field ne, either with another constructor parameter like you do it for x, y and r or with a sensible default (maybe 3 for a triangle?). Do the initialization of ne before you initialize px and py.
  • re-initialize px and py whenever you give ne another value (so in the method seiten)

In general I would recommend that you just add a constructor parameter and remove the seiten method completely, if possible, it makes things more complicated.

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):

px[i] = x + (r * Math.sin(Math.toRadians(i * 360.0 / ne)));
py[i] = y - (r * Math.sin(Math.toRadians(i * 360.0 / ne)));
cyberbrain
  • 3,433
  • 1
  • 12
  • 22
  • thank you for the explanation and you were right the error was "Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: Index 0 out of bounds for length )" but can you please help me fix it. – Giga Dec 26 '22 at 16:51
  • I added some hints how to fix it, but please do me a favour and add the exception and a description to your question. – cyberbrain Dec 27 '22 at 21:54