2014-10-12 76 views
1

下面的程序在屏幕上畫出幾個球,它們應該是簡單的反彈,沒有摩擦力。 我在運行程序時不斷收到一個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) 
+0

我支持我的回答中給出的建議 - Ball不應該延伸StartingPoint,但我的回答是不正確的,因爲它不是你的錯誤的來源。請告訴我們你如何試圖運行這個程序。 – 2014-10-12 14:00:21

+0

我自己的一個側面問題:您的paint方法覆蓋內嵌套的for循環的目的是什麼? – 2014-10-12 14:02:29

+0

ahaha,是的,我敢肯定它是一團糟(背景非常醜陋),該程序運行在eclipse上,這個概念就是讓10個bal appe隨機出現並繞y軸反彈,只是沒有摩擦,我似乎很容易,但仍然不能很好地工作 – 2014-10-12 14:03:04

回答

1

球應該最肯定延長起點,因爲這給你一個循環引用問題:

起點包含延長球對象名單初始點。

編輯:您的異常來自您嘗試運行Ball類而不是StartingPoint類。

+0

感謝您的評論,但錯誤依然存在:( – 2014-10-12 13:54:57

+0

@ SwissPhil02:我沒有收到這個異常,但是當我運行您的程序,我得到你目不暇接你是如何試圖運行小程序 – 2014-10-12 13:57:37

+0

@? SwissPhil02:好的,我已經解決了異常問題 - 再次,沒有Ball擴展StartingPoint,最重要的是,不要嘗試運行Ball類,而是運行StartingPoint類 – 2014-10-12 14:05:43