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