尝试在java中构建小行星游戏时出错

时间:2016-02-25 23:54:28

标签: java

我一直试图在java中创建一个小行星游戏的第一部分,它只显示20个小行星在一个场地中移动。我什么时候开始它,我得到这些错误:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at blobzx.SandBox$1.actionPerformed(SandBox.java:82)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2348)
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:6535)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3324)
at java.awt.Component.processEvent(Component.java:6300)
at java.awt.Container.processEvent(Container.java:2236)
at java.awt.Component.dispatchEventImpl(Component.java:4891)
at java.awt.Container.dispatchEventImpl(Container.java:2294)
at java.awt.Component.dispatchEvent(Component.java:4713)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4888)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4525)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4466)
at java.awt.Container.dispatchEventImpl(Container.java:2280)
at java.awt.Window.dispatchEventImpl(Window.java:2750)
at java.awt.Component.dispatchEvent(Component.java:4713)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:758)
at java.awt.EventQueue.access$500(EventQueue.java:97)
at java.awt.EventQueue$3.run(EventQueue.java:709)
at java.awt.EventQueue$3.run(EventQueue.java:703)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:86)
at java.awt.EventQueue$4.run(EventQueue.java:731)
at java.awt.EventQueue$4.run(EventQueue.java:729)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:728)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)

我在这里看过其他文章,但似乎无法找到这种情况的解决方案。这是我写的代码:

    package AsteroidField;

import blobzx.*;
import java.awt.Point;
import java.util.Random;

public class AsteroidField implements BlobGUI{

    Random random;

    SandBox sand = new SandBox();

   public static void main( String[] args){


        new AsteroidField();
    }

    public AsteroidField(){

        this.random = new Random();

    }

    public void init(){

        sand.setSandBoxMode(SandBoxMode.FLOW);
        sand.setFrameRate(15);
        sand.init(this);

    }

    public void generate(){

        for(int i=0; i<20; i++){

           int idx = random.nextInt(6)-3;
           int idy = random.nextInt(6)-3;
           double rot = (random.nextInt(2)-1)*.1;

            PolyBlob a = new Asteroid(idx, idy, rot);
            sand.addBlob(a);
        }

        SandBox.simulate(sand);

    }
}

    package AsteroidField;

import blobzx.*;
import java.util.Random;
import java.awt.Point;

public class Asteroid extends PolyBlob {

    private Random random = new Random();

    public Asteroid(int idx, int jdx, double rot ){


        super( -100, -100, rot );


        setDelta(idx, jdx);

        int sides = random.nextInt(4)+5;
        int pix = random.nextInt(20)+10;

        int[] x = new int [-100];
        int[] y = new int [-100];
        double[] angle = new double [1];

        for(int i=0; i<sides; i++){

            double region = ((Math.PI*2)/sides);
            angle[i] = (i*region)+(Math.random()*region);

        }

        for(int j=0; j<sides; j++){

            int distance = random.nextInt(10)+5;
            Point point = BlobUtils.rotatePoint(distance, angle[j]);

            x[j] = point.x;
            y[j] = point.y;

        }

        setPolygon(x, y);

    }
}

非常感谢有关此问题的任何帮助。

1 个答案:

答案 0 :(得分:0)

我看到的一件事就是立即向我发出的一句话:

int[] x = new int [-100];
int[] y = new int [-100];

在编译java程序中嵌入的这些短语后得到的错误。

Exception in thread "main" java.lang.NegativeArraySizeException
    at HelloWorld.main(HelloWorld.java:5)  

我认为可能存在其他基本语法错误,因此您应该检查基本语法。