下面的程序在屏幕上畫出幾個球,它們應該是簡單的反彈,沒有摩擦力。 我在運行程序時不斷收到一個instantiationExeption,但無法糾正它(我還是Java的新手:D)。Moving Balls,instantiationExeption
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)
我支持我的回答中給出的建議 - Ball不應該延伸StartingPoint,但我的回答是不正確的,因爲它不是你的錯誤的來源。請告訴我們你如何試圖運行這個程序。 – 2014-10-12 14:00:21
我自己的一個側面問題:您的paint方法覆蓋內嵌套的for循環的目的是什麼? – 2014-10-12 14:02:29
ahaha,是的,我敢肯定它是一團糟(背景非常醜陋),該程序運行在eclipse上,這個概念就是讓10個bal appe隨機出現並繞y軸反彈,只是沒有摩擦,我似乎很容易,但仍然不能很好地工作 – 2014-10-12 14:03:04