我正在尝试创建一个程序,它绘制一个具有指定边数的形状,但我一直收到错误,我不确定问题是什么。
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)发布于 2013-08-10 05:09:38
您正在使用add在索引1处插入对象,但是ArrayList中还没有任何内容,因此您不能将其插入到该位置。1比大小0大,所以你会得到一个IndexOutOfBoundsException。
我不确定哪一行是58行,但可能是这些行中的一行:
xcoordinate.add(1,Apothem);
ycoordinate.add(1,(double)0);我不确定为什么需要在这一点上插入;通常add with just one argument会追加到列表的末尾,这应该可以很好地工作。
发布于 2013-08-10 05:10:29
您正尝试在索引1处添加,但开始时数组列表为空--没有索引1。
只需使用常规的add即可。或索引0。
发布于 2013-08-10 05:13:07
进行以下更改:
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++){尝试设置计数器=1(因为ArrayList从0变为长度-1)
https://stackoverflow.com/questions/18155654
复制相似问题