Reputation: 670
The following program draws a couple of balls on the screen which are supposed to simple bounce around, with no friction force. I keep getting an instantiationExeption when running the program, without being able to correct it (I'm still new to 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)
Upvotes: 1
Views: 289
Reputation: 285403
Ball should most definitely not extend StartingPoint as this gives you a circular reference issue:
StartingPoint contains a List of Ball objects which extend StartingPoint.
Edit: your exception is coming from your trying to run the Ball class and not the StartingPoint class.
Upvotes: 1