2016-12-04 8 views
-1

Ich habe ein Projekt fällig in meiner (OOP) -Klasse. Dies war die Aufgabe: Ändern Sie die Bounce-Methode, um die Bälle zufällig irgendwo in der oberen Hälfte des Bildschirms zu platzieren. DieseCompiler Fehler inkompatible Typen: java.lang Objekt kann nicht konvertiert werden

ist, was ich habe,

import java.awt.Color; 
import java.util.ArrayList;     
import java.util.Random; 
/** 
* Class BallDemo - a short demonstration showing animation with the 
* Canvas class. 
* 
* @author Michael Kölling and David J. Barnes 
* @version 2016.02.29 
*/ 

public class BallDemo 
{ 
    private Canvas myCanvas; 
    private ArrayList ball; 
    private BouncingBall bounceBall; 
    private Random randomGenerator; 

    /** 
     * Create a BallDemo object. Creates a fresh canvas and makes it visible. 
     */ 
    public BallDemo() 
    { 
     myCanvas = new Canvas("Ball Demo", 600, 500); 
    } 

    /** 
     * Simulate two bouncing balls 
     */ 
    public void bounce() 
    { 
     int ground = 400; // position of the ground line 
     ball = new ArrayList(); 
     randomGenerator = new Random(); 

     myCanvas.setVisible(true); 
     myCanvas.erase(); 

     // draw the ground 
     myCanvas.drawLine(50, ground, 550, ground); 

     // create and show the balls 
     BouncingBall ball1 = new BouncingBall(position(),position(),16,Color.BLUE,ground,myCanvas); 
     ball1.draw(); 
     BouncingBall ball2 = new BouncingBall(position(),position(),20,Color.RED,ground,myCanvas); 
     ball2.draw(); 
     BouncingBall ball3 = new BouncingBall(position(),position(),20,Color.BLACK,ground,myCanvas); 
     ball3.draw(); 
     //Add balls to ArrayList 
     ball.add(ball1); 
     ball.add(ball2); 
     ball.add(ball3); 

     This is where I get the error: 

     // make them bounce 
     boolean finished = false; 
     while (!finished) { 
      myCanvas.wait(50);   // small delay 
      for(int i = 0; i < ball.size(); i++) { 
       **bounceBall = ball.get(i);**   
       bounceBall.move(); 

      // stop once ball has travelled a certain distance on x axis 
      if(bounceBall.getXPosition() >= 550) { 
       finished = true; 
      } 
     } 
    } 
} 

    /** 
    * Randomly generates the position 
    * Pick the number between 0 to 200 
    */ 

    public int position() { 
     return randomGenerator.nextInt(200); 
    } 
} 

Diese von einem Experten Programmierer geholfen wurde, aber ich immer noch den Compiler-Fehler, dann ist es um die bounceBall = ball.get(i); hauptsächlich um die (i).

+0

Share-Kompilierungsfehler zuzuweisen – Shashank

Antwort

1

Dies passiert, weil Sie eine Liste von Objekten mit dem Typ Objekt haben, keine Liste von BouncingBall-Objekten, so dass der Compiler nicht einfach ein Objekt in einen BouncingBall konvertieren kann. Wenn Sie Java-Version> = 1.5, getippt Verwendung Liste verwenden: ArrayList<BouncingBall> ball, oder Guss Objekt BouncingBall: bounceBall = (BouncingBall)ball.get(i);

0

ändern

ArrayList ball; 

zu

ArrayList<BouncingBall> ball; 

lösen Ihr Problem.

ArrayList ist standardmäßig ArrayList < Objekt>. Daher erhalten Sie einen Fehler in der Zeile bounceBall = ball.get (i); da Sie versuchen, Object zu BouncingBall

Verwandte Themen