我正在尝试分形,但由于某种原因,我在第50和41行获得了一个超出界限的索引,但对我来说似乎完全没问题。我已经尝试了几件事,但只有在执行takken.get(0).draw(g2d)时才会运行,任何高于0的东西都会给我一个错误。如果有人能帮助我,我将不胜感激
package com.company;
import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.geom.*;
import java.util.ArrayList;
import java.util.Random;
public class FractalTree extends JPanel {
public static void main(String[] args) {
JFrame frame = new JFrame("Week 1");
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.setMinimumSize(new Dimension(800, 600));
frame.setExtendedState(frame.getExtendedState() | JFrame.MAXIMIZED_BOTH);
frame.setContentPane(new FractalTree());
frame.setVisible(true);
}
ArrayList<Tak> takken = new ArrayList<Tak>();
public FractalTree(){
}
public void clearTakken(){
for(int k = 0; k <=takken.size(); k++){
takken.remove(0);
}
}
public void addTak(Tak tak){
takken.add(tak);
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
draw(g2d);
}
public void draw(Graphics2D g2d){
Tak beginTak = new Tak(100,90, 600, 400, 600, 500);
takken.add(beginTak);
for(int j = 0; j <=3; j++) {
for(int i = 0; i <= takken.size(); i++) {
takken.get(0).draw(g2d);
takken.get(1).draw(g2d);
}
}
}
}
&#34;&#34;
package com.company;
import java.lang.Math;
import java.awt.*;
import java.awt.geom.Point2D;
public class Tak extends FractalTree{
double lengte;
double hoek;
int xEind;
int yEind;
double xBegin;
double yBegin;
final double factor = 0.67 / Math.sqrt(2);
public Tak(double l, double h, int xE, int yE, double xB, double yB){
lengte = l;
hoek = h;
xEind = xE;
yEind = yE;
xBegin = xB;
yBegin = yB;
}
public double getHoek(){
return hoek;
}
public double getLengte(){
return lengte;
}
public void draw(Graphics2D g2d){
g2d.drawLine((int)xBegin, (int)yBegin, xEind, yEind);
Tak tak1 = new Tak((lengte * 0.67), hoek / 2, xEind, yEind, xEind + (lengte*factor), yEind + (lengte*factor));
addTak(tak1);
Tak tak2 = new Tak((lengte * 0.67), hoek / 2, xEind, yEind, xEind - (lengte*factor), yEind + (lengte*factor));
addTak(tak2);
}
}
Exception in thread "AWT-EventQueue-0" java.lang.IndexOutOfBoundsException: Index 1 out-of-bounds for length 1
at java.base/jdk.internal.util.Preconditions.outOfBounds(Preconditions.java:64)
at java.base/jdk.internal.util.Preconditions.outOfBoundsCheckIndex(Preconditions.java:70)
at java.base/jdk.internal.util.Preconditions.checkIndex(Preconditions.java:248)
at java.base/java.util.Objects.checkIndex(Objects.java:372)
at java.base/java.util.ArrayList.get(ArrayList.java:440)
at com.company.FractalTree.draw(FractalTree.java:50)
at com.company.FractalTree.paintComponent(FractalTree.java:41)
at java.desktop/javax.swing.JComponent.paint(JComponent.java:1075)
at java.desktop/javax.swing.JComponent.paintChildren(JComponent.java:908)
at java.desktop/javax.swing.JComponent.paint(JComponent.java:1084)
at java.desktop/javax.swing.JLayeredPane.paint(JLayeredPane.java:590)
at java.desktop/javax.swing.JComponent.paintChildren(JComponent.java:908)
at java.desktop/javax.swing.JComponent.paintToOffscreen(JComponent.java:5263)
at java.desktop/javax.swing.RepaintManager$PaintManager.paintDoubleBufferedImpl(RepaintManager.java:1633)
at java.desktop/javax.swing.RepaintManager$PaintManager.paintDoubleBuffered(RepaintManager.java:1608)
at java.desktop/javax.swing.RepaintManager$PaintManager.paint(RepaintManager.java:1546)
at java.desktop/javax.swing.RepaintManager.paint(RepaintManager.java:1313)
at java.desktop/javax.swing.JComponent.paint(JComponent.java:1061)
at java.desktop/java.awt.GraphicsCallback$PaintCallback.run(GraphicsCallback.java:39)
at java.desktop/sun.awt.SunGraphicsCallback.runOneComponent(SunGraphicsCallback.java:78)
at java.desktop/sun.awt.SunGraphicsCallback.runComponents(SunGraphicsCallback.java:115)
at java.desktop/java.awt.Container.paint(Container.java:1998)
at java.desktop/java.awt.Window.paint(Window.java:3926)
at java.desktop/javax.swing.RepaintManager$4.run(RepaintManager.java:868)
at java.desktop/javax.swing.RepaintManager$4.run(RepaintManager.java:840)
at java.base/java.security.AccessController.doPrivileged(Native Method)
at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:89)
at java.desktop/javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:840)
at java.desktop/javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:815)
at java.desktop/javax.swing.RepaintManager.prePaintDirtyRegions(RepaintManager.java:764)
at java.desktop/javax.swing.RepaintManager.access$1200(RepaintManager.java:69)
at java.desktop/javax.swing.RepaintManager$ProcessingRunnable.run(RepaintManager.java:1880)
at java.desktop/java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:313)
at java.desktop/java.awt.EventQueue.dispatchEventImpl(EventQueue.java:764)
at java.desktop/java.awt.EventQueue.access$500(EventQueue.java:97)
at java.desktop/java.awt.EventQueue$3.run(EventQueue.java:717)
at java.desktop/java.awt.EventQueue$3.run(EventQueue.java:711)
at java.base/java.security.AccessController.doPrivileged(Native Method)
at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:89)
at java.desktop/java.awt.EventQueue.dispatchEvent(EventQueue.java:734)
at java.desktop/java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:199)
at java.desktop/java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:124)
at java.desktop/java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:113)
at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:109)
at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.desktop/java.awt.EventDispatchThread.run(EventDispatchThread.java:90)
处理完成,退出代码为0
答案 0 :(得分:0)
你试图在这个方法中只添加一个元素时获取数组中为null的第二个元素
public void draw(Graphics2D g2d){
Tak beginTak = new Tak(100,90, 600, 400, 600, 500);
takken.add(beginTak);
for(int j = 0; j <=3; j++) {
for(int i = 0; i <= takken.size(); i++) {
takken.get(0).draw(g2d);
takken.get(1).draw(g2d);
}
}
}
将另一个Tak元素添加到数组中,它将起作用。