移动球,instantiationException

时间:2014-10-12 13:52:28

标签: java applet

以下程序在屏幕上绘制了几个球,这些球应该简单地反弹,没有摩擦力。 我在运行程序时不断获得instantiationExeption,而无法纠正它(我还是java的新手:D)。

package movement;

import java.awt.Color;
import java.awt.Graphics;
import java.util.Random;

public class Ball extends StartingPoint {
 /**
     * 
     */
    private static final long serialVersionUID = 1L;

int x;
int y;
int radius;
Color color;
// dy will be the velocity of the ball
double dx,dy = 0;
private static final double dt = .2;
private static final double gravity = 9.81;

 Ball(int x, int y, int radius, Color color){
    this.x = x;
    this.y = y;
    this.radius = radius;
    this.color = color;
}

 public Color getCOLOR(Ball ball){
    return ball.color;
 }

public void move(){


        x = (int) (x + dx);

        // 600 being the window height
        if (y > 600 -radius - 1){
            y = 600 - radius - 1;
            dy *= -1;
        }
        else{

            //physics formula for velocity
            dy += gravity * dt;
            //physics formula for displacement with earth-like gravity
            y += (int) .5 * gravity * dt*dt + dy*dt;}

}

public void paintPLUS(Graphics g, Ball ball){

     g.setColor(getCOLOR(ball));
     g.fillOval(x, y, radius, radius);
}


}

// This program creates nbBalles balls which move down in parallel 

package movement;

import java.applet.Applet;
import java.awt.Color;
import java.awt.Graphics;
import java.util.ArrayList;
import java.util.Random;


public class StartingPoint extends Applet {

    public int startX,startY,startRADIUS, nbColor, n;
    private static final int nbBalles = 50;
    private static final long serialVersionUID = 1L;
    public Color[] setCOLOR = {Color.black, Color.DARK_GRAY, Color.magenta, Color.white, Color.RED, Color.ORANGE};
    ArrayList<Ball> BallList = new ArrayList<Ball>();


    @Override
    public void init() {

        setSize(800,600);

        for (int i = 0; i<nbBalles; i++){
        Random randX = new Random();
        Random randY = new Random();
        Random randRADIUS = new Random();
        startX = randX.nextInt(2000);
        startY = randY.nextInt(700);
        startRADIUS = randRADIUS.nextInt(50)+10;
         Random random = new Random();
         nbColor = random.nextInt(setCOLOR.length);
        BallList.add(new Ball(startX, startY, startRADIUS, setCOLOR[nbColor]));

        }}



    @Override
    public void start() {

            for(Ball ball :BallList){
            Thread thread = new Thread(new BallMovement(ball));
            thread.start();
            }
        }

    class BallMovement implements Runnable{
        private final Ball ball;
        BallMovement(Ball ball){
            this.ball = ball;
        }

        @Override
        public void run() {
            while(true){
                ball.move();
                try {
                    Thread.sleep(8);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

                repaint();
                }

        }
    }

    @Override
    public void stop() {
        // TODO Auto-generated method stub

    }

    @Override
    public void paint(Graphics g) {
        g.setColor(Color.cyan);
        g.fillRect(0, 0, 2000, 1000);

        for(Ball ball : BallList)
        {

            ball.paintPLUS(g, ball);
        }
    }

}

charger : movement.Ball.class ne peut pas être instancié.java.lang.InstantiationException: movement.Ball
    at java.lang.Class.newInstance0(Class.java:342)
    at java.lang.Class.newInstance(Class.java:310)
    at sun.applet.AppletPanel.createApplet(AppletPanel.java:806)
    at sun.applet.AppletPanel.runLoader(AppletPanel.java:713)
    at sun.applet.AppletPanel.run(AppletPanel.java:369)
    at java.lang.Thread.run(Thread.java:695)

1 个答案:

答案 0 :(得分:1)

Ball绝对应该扩展StartingPoint,因为这会给你一个循环引用问题:

StartingPoint包含一个Ball对象列表,它扩展了StartingPoint。

编辑:您的异常来自您尝试运行Ball类而不是StartingPoint类。