1

我正在尝试创建一个程序来绘制具有指定边数的形状,但我不断收到错误,我不确定问题是什么。

public double tempx = 0;
    public double tempy = 0;
    public int Angle = 0;
    public double Length = 0;
    public int Nodes = 0;
    public int Height = 170;

    //public double xcoordinate = 0;
    //public double ycoordinate = 0;
    //Unused Variables

    /**
     * Creates new form Shape
     */
    public void drawShape(int Nodes,int Height){
        double Apothem=0;
        double Circumradius=0;
        double x = 180/Nodes;
        ArrayList<Double> xcoordinate = new ArrayList(Nodes);
        ArrayList<Double> ycoordinate = new ArrayList(Nodes);
        //Angle = (180*(Nodes-2))/2*Nodes;
        //Angle is half the interior angle of the shape(Currently Unused)
        Angle = 360/Nodes;


        if(Nodes%2==1){
            Length = (2*Height*Math.tan(Math.toRadians((x))))/(1+Math.cos(Math.toRadians((x))));
            Apothem = Length/(2*Math.tan(Math.toRadians((x))));
            Circumradius = Apothem/(Math.cos(Math.toRadians((180/Nodes))));
            System.out.println(Length);
            System.out.println(Apothem);
            System.out.println(Circumradius);
            //Calculating properties of an odd number of sides shape
        }
        if(Nodes%2==0){
            Length = Height*Math.tan(Math.toRadians((x)));
            Apothem = Length/(2*Math.tan(Math.toRadians((x))));
            //Calculating properties of an even number of sides shape
        }


        //double xcoordinate[] = new double[Nodes];
        //double ycoordinate[] = new double[Nodes];
        //Unused Variables

        if(Nodes%2 == 1){
                xcoordinate.add(1,Apothem);
                System.out.println(xcoordinate.get(1));
                ycoordinate.add(1,(double)0);
                for(int counter =2;counter<(Nodes+1);counter++){




                    xcoordinate.add((xcoordinate.get(counter-1)*Math.cos(Math.toRadians(Angle))-(ycoordinate.get(counter-1)*Math.sin(Math.toRadians(Angle)))));
                    ycoordinate.add((xcoordinate.get(counter-1)*Math.sin(Math.toRadians(Angle))+(ycoordinate.get(counter-1)*Math.cos(Math.toRadians(Angle)))));

                    //Uses Matrix Rotation to calculate all the coordinates of the point on the shape

        }
            }




    }

我目前正在尝试让奇数边的形状工作,这是我在节点等于 5 的情况下测试时得到的输出:

136.55176280263456
Exception in thread "AWT-EventQueue-0" java.lang.IndexOutOfBoundsException: Index: 1, Size: 0
93.97368876500715
116.15786740995709
    at java.util.ArrayList.rangeCheckForAdd(ArrayList.java:612)
    at java.util.ArrayList.add(ArrayList.java:426)
    at graphs.Shape.drawShape(Shape.java:58)
    at graphs.Shape.jButton1ActionPerformed(Shape.java:189)
    at graphs.Shape.access$100(Shape.java:10)
    at graphs.Shape$2.actionPerformed(Shape.java:140)
    at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018)
    at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341)
    at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
    at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
    at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
    at java.awt.Component.processMouseEvent(Component.java:6505)
    at javax.swing.JComponent.processMouseEvent(JComponent.java:3321)
    at java.awt.Component.processEvent(Component.java:6270)
    at java.awt.Container.processEvent(Container.java:2229)
    at java.awt.Component.dispatchEventImpl(Component.java:4861)
    at java.awt.Container.dispatchEventImpl(Container.java:2287)
    at java.awt.Component.dispatchEvent(Component.java:4687)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832)
    at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4492)
    at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422)
    at java.awt.Container.dispatchEventImpl(Container.java:2273)
    at java.awt.Window.dispatchEventImpl(Window.java:2719)
    at java.awt.Component.dispatchEvent(Component.java:4687)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:723)
    at java.awt.EventQueue.access$200(EventQueue.java:103)
    at java.awt.EventQueue$3.run(EventQueue.java:682)
    at java.awt.EventQueue$3.run(EventQueue.java:680)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
    at java.awt.EventQueue$4.run(EventQueue.java:696)
    at java.awt.EventQueue$4.run(EventQueue.java:694)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:693)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:244)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:163)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:151)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:147)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:139)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:97)
4

4 回答 4

1

您正在使用add在 index 处插入对象1,但目前还没有任何内容ArrayList,因此您无法将其插入那里。 1大于大小0,所以你得到一个IndexOutOfBoundsException.

我不确定哪一行是第 58 行,但可能是以下行之一:

xcoordinate.add(1,Apothem);

ycoordinate.add(1,(double)0);

我不确定你为什么需要在那个时候插入;通常add只有一个参数会附加到列表的末尾,这应该可以正常工作。

于 2013-08-09T21:09:37.967 回答
0

进行此更改:

            xcoordinate.add(0,Apothem);
            System.out.println(xcoordinate.get(0));
            ycoordinate.add(0,(double)0);

第一个位置是 0(不是 1!!)。或者,您可以使用普通的 add() 方法:

            xcoordinate.add(Apothem);
            System.out.println(xcoordinate.get(0));
            ycoordinate.add(double)0);

这可能会导致将来出现问题:

            for(int counter =2;counter<(Nodes+1);counter++){

尝试设置 counter = 1(因为 ArrayList 从 0 变为 Length-1)

于 2013-08-09T21:13:07.303 回答
0

您尝试在索引 1 处添加,但您的数组列表开始时为空 - 没有索引 1。

只需使用常规添加。或索引 0。

于 2013-08-09T21:10:28.587 回答
0

我认为问题出在这里:

xcoordinate.add(1,Apothem);

在索引 0 处添加任何内容之前,在索引 1 处添加此值。

于 2013-08-09T21:17:57.273 回答