使用事件侦听器添加到数组列表[Java]

时间:2019-03-27 18:32:49

标签: java arraylist event-listener

我有一个带有三角形的gameloop,它在屏幕上随机移动。我想添加一个按钮,单击该按钮将添加另一个随机移动的三角形。我正在使用事件侦听器,无论放置在什么地方,我仍然会遇到异常

这是我的代码

import javax.swing.*;
import drawing.Canvas;
import tools.Utils;
import triangle.DynamicTriangle;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

public class TriangleProgram {
    private JFrame frame; 
    private Canvas canvas; 
    private JPanel lowerPanel; 
    private JButton addTriangleButton; 
    private ArrayList<DynamicTriangle> triangles;

    public void gameLoop() {
        int deltaTime = 20;

        triangles.add(new RandomTriangleA(canvas, 150, 150));

        for (DynamicTriangle dynamicTriangle : triangles) {
            dynamicTriangle.drawTriangle();
        }
        while (true)
        {
            for (DynamicTriangle dynamicTriangle : triangles) {
                dynamicTriangle.undrawTriangle();
            }

            for (DynamicTriangle dynamicTriangle : triangles) {
                dynamicTriangle.update(100);
                dynamicTriangle.wrapPosition();
            }

            for (DynamicTriangle dynamicTriangle : triangles) {
                dynamicTriangle.drawTriangle();
            }

            Utils.pause(deltaTime);
            canvas.repaint();
        }

    }

    public TriangleProgram() {
        frame = new JFrame();
        frame.setTitle("Canvas");
        frame.setSize(800, 600);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);

        canvas = new Canvas();
        frame.add(canvas, BorderLayout.CENTER);

        lowerPanel = new JPanel();
        lowerPanel.setLayout(new FlowLayout());
        frame.add(lowerPanel, BorderLayout.SOUTH);

        triangles = new ArrayList<DynamicTriangle>();

        addTriangleButton = new JButton("Add Triangle"); 
        lowerPanel.add(addTriangleButton);
        frame.revalidate();

        addTriangleButton.addActionListener(new ButtonListener());

        gameLoop();

    }

    public static void main(String[] args) {
        System.out.println("Running TriangleProgram...");
        new TriangleProgram();
    }

    class ButtonListener implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent ae) {
            System.out.println(triangles.size());
            triangles.add(new RandomTriangleA(canvas, 150, 150));
        }

    }

}

我希望每次按下按钮时都会在画布上添加一个新的三角形,但是我会得到堆栈跟踪:

Running TriangleProgram...
1
Exception in thread "main" java.lang.IndexOutOfBoundsException: Index -1 out-of-bounds for length 0
    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.remove(ArrayList.java:517)
    at java.base/java.util.Collections$SynchronizedList.remove(Collections.java:2424)
    at drawing.Canvas.removeMostRecentLine(Canvas.java:162)
    at triangle.Triangle.undrawTriangle(Triangle.java:131)
    at TriangleProgram.gameLoop(TriangleProgram.java:29)
    at TriangleProgram.<init>(TriangleProgram.java:69)
    at TriangleProgram.main(TriangleProgram.java:75)

1 个答案:

答案 0 :(得分:0)

        triangle.add(new Triangle(canvas, 150, 150));

在这里,您甚至都不会声明triangle,因此不确定该代码如何运行甚至会给您带来异常。我假设您的意思是triangles 同样,您可以(?)压缩循环。

编辑:同样,当您声明列表三角形时,为什么它是DynamicTriangle的列表,但是当您初始化并添加到列表中时,它就是Triangle

不确定DynamicTriangleTriangle的子类还是错别字,但是如果是这样,就不应该编译。

如果是错字,请改用ArrayList<DynamicTriangle> trianglesTriangle

如果DyanmicTriangleTriangle的子类,那么您想将三角形​​声明为ArrayList<Triangle>()并将其初始化为.add(new DynamicTriangle(...