error: incompatible types: Object can not be converted to Graph

1

I'm relatively new to programming on android with Android Studio and I was doing the tutorial that I show in the link.

However, I got stuck because it gives me an error when I make the following for loop:

for (Grafico asteroide: Asteroides) {
        asteroide.dibujaGrafico(canvas);
    }

The error I get is the following:

  

error: incompatible types: Object can not be converted to Graphic

Does anyone know why it does not allow me to go through that "Grafico" data vector?

The complete code of the program is as follows:

package asteroides.example.org.asteroides;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.View;

import java.util.Vector;


/**
 * Created by usuario on 18/04/2017.
 */

public class VistaJuego extends View {
    // //// ASTEROIDES //////
    private Vector Asteroides; // Vector con los Asteroides
    private int numAsteroides= 5; // Número inicial de asteroides
    private int numFragmentos= 3; // Fragmentos en que se divide
    private Grafico asteroide;
    public VistaJuego(Context context, AttributeSet attrs) {
        super(context, attrs);
        Drawable drawableNave, drawableAsteroide, drawableMisil;
        drawableAsteroide = context.getResources().getDrawable(
                R.drawable.asteroide1);
        Asteroides = new Vector();
        for (int i = 0; i < numAsteroides; i++) {
            asteroide = new Grafico(this, drawableAsteroide);
            asteroide.setIncY(Math.random() * 4 - 2);
            asteroide.setIncX(Math.random() * 4 - 2);
            asteroide.setAngulo((int) (Math.random() * 360));
            asteroide.setRotacion((int) (Math.random() * 8 - 4));
            Asteroides.add(asteroide);
        }
    }
    @Override protected void onSizeChanged(int ancho, int alto,
                                           int ancho_anter, int alto_anter) {
        super.onSizeChanged(ancho, alto, ancho_anter, alto_anter);
        // Una vez que conocemos nuestro ancho y alto.
        for (Grafico asteroide: Asteroides) {
            asteroide.setPosX(Math.random()*
                    (ancho-asteroide.getAncho()));
            asteroide.setPosY(Math.random()*
                    (alto-asteroide.getAlto()));
        }
    }
    @Override protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        for (Grafico asteroide: Asteroides) {
            asteroide.dibujaGrafico(canvas);
        }
    }
}

And the class Grafico is as follows:

public class Grafico {
    public Drawable getDrawable() {
        return drawable;
    }

    public void setDrawable(Drawable drawable) {
        this.drawable = drawable;
    }

    public double getPosX() {
        return posX;
    }

    public void setPosX(double posX) {
        this.posX = posX;
    }

    public double getPosY() {
        return posY;
    }

    public void setPosY(double posY) {
        this.posY = posY;
    }

    public double getIncX() {
        return incX;
    }

    public void setIncX(double incX) {
        this.incX = incX;
    }

    public double getIncY() {
        return incY;
    }

    public void setIncY(double incY) {
        this.incY = incY;
    }

    public int getAngulo() {
        return angulo;
    }

    public void setAngulo(int angulo) {
        this.angulo = angulo;
    }

    public int getRotacion() {
        return rotacion;
    }

    public void setRotacion(int rotacion) {
        this.rotacion = rotacion;
    }

    public int getAncho() {
        return ancho;
    }

    public void setAncho(int ancho) {
        this.ancho = ancho;
    }

    public int getAlto() {
        return alto;
    }

    public void setAlto(int alto) {
        this.alto = alto;
    }

    public int getRadioColision() {
        return radioColision;
    }

    public void setRadioColision(int radioColision) {
        this.radioColision = radioColision;
    }

    public View getView() {
        return view;
    }

    public void setView(View view) {
        this.view = view;
    }

    public static int getMaxVelocidad() {
        return MAX_VELOCIDAD;
    }

    private Drawable drawable;   //Imagen que dibujaremos
    private double posX, posY;   //Posición
    private double incX, incY;   //Velocidad desplazamiento
    private int angulo, rotacion;//Ángulo y velocidad rotación
    private int ancho, alto;     //Dimensiones de la imagen
    private int radioColision;   //Para determinar colisión
    //Donde dibujamos el gráfico (usada en view.ivalidate)
    private View view;
    // Para determinar el espacio a borrar (view.ivalidate)
    public static final int MAX_VELOCIDAD = 20;

    public Grafico(View view, Drawable drawable){
        this.view = view;
        this.drawable = drawable;
        ancho = drawable.getIntrinsicWidth();
        alto = drawable.getIntrinsicHeight();
        radioColision = (alto+ancho)/4;
    }
    public void dibujaGrafico(Canvas canvas){
        canvas.save();
        int x=(int) (posX+ancho/2);
        int y=(int) (posY+alto/2);
        canvas.rotate((float) angulo,(float) x,(float) y);
        drawable.setBounds((int)posX, (int)posY,
                (int)posX+ancho, (int)posY+alto);
        drawable.draw(canvas);
        canvas.restore();
        int rInval = (int) Math.hypot(ancho,alto)/2 + MAX_VELOCIDAD;
        view.invalidate(x-rInval, y-rInval, x+rInval, y+rInval);
    }
    public void incrementaPos(double factor){
        posX+=incX * factor;
        // Si salimos de la pantalla, corregimos posición
        if(posX<-ancho/2) {posX=view.getWidth()-ancho/2;}
        if(posX>view.getWidth()-ancho/2) {posX=-ancho/2;}
        posY+=incY * factor;
        if(posY<-alto/2) {posY=view.getHeight()-alto/2;}
        if(posY>view.getHeight()-alto/2) {posY=-alto/2;}
        angulo += rotacion * factor; //Actualizamos ángulo
    }
    public double distancia(Grafico g) {
        return Math.hypot(posX-g.posX, posY-g.posY);
    }
    public boolean verificaColision(Grafico g) {
        return(distancia(g) < (radioColision+g.radioColision));
    }
}
    
asked by Leo_Gz 18.04.2017 в 17:45
source

3 answers

0

When you do this, mark incompatible types, a non-graphic Object is required

 for (Grafico asteroide: Asteroides) {
  ...
  ...
 }

You must change to obtain each element of type Grafico, making a cast inside the Vector, and later change its properties:

  Grafico asteroide;
  for (Object asteroide: Asteroides) {
        asteroide = (Grafico) asteroide;
      ...
      ...
  }

Another option:

    Grafico asteroide;
    for (int i=0;i<Asteroides.size();i++) {
        asteroide = (Grafico) Asteroides.get(i);
      ...
      ...
    }

Example a):

 @Override protected void onSizeChanged(int ancho, int alto,
                                           int ancho_anter, int alto_anter) {
        super.onSizeChanged(ancho, alto, ancho_anter, alto_anter);
        // Una vez que conocemos nuestro ancho y alto.

      // for (Grafico asteroide: Asteroides) {
        Grafico asteroide;
      for (Object asteroide: Asteroides) {
            asteroide = (Grafico) asteroide;

            asteroide.setPosX(Math.random()*
                    (ancho-asteroide.getAncho()));
            asteroide.setPosY(Math.random()*
                    (alto-asteroide.getAlto()));
        }
    }

Example b)

 @Override protected void onSizeChanged(int ancho, int alto,
                                           int ancho_anter, int alto_anter) {
        super.onSizeChanged(ancho, alto, ancho_anter, alto_anter);
        // Una vez que conocemos nuestro ancho y alto.

      // for (Grafico asteroide: Asteroides) {
        Grafico asteroide;
        for (int i=0;i<Asteroides.size();i++) {
            asteroide = (Grafico) Asteroides.get(i);

            asteroide.setPosX(Math.random()*
                    (ancho-asteroide.getAncho()));
            asteroide.setPosY(Math.random()*
                    (alto-asteroide.getAlto()));
        }
    }
    
answered by 18.04.2017 / 18:10
source
0

Fails because Asteroides is an array of class Vector , so it is composed of objects of class Vector . You are trying to traverse the array, and convert them to Grafico .

Your loop should be something like this:

for (Vector asteroide: Asteroides) {
    //lo que sea
}
    
answered by 18.04.2017 в 18:02
0

Your array Asteroides is returning Object instead of objects Grafico . Doing a cast , probably fix your error:

for (Object asteroide: Asteroides) {
    Grafico grf = (Grafico) asteroide;
    grf.dibujaGrafico(canvas);
}
    
answered by 18.04.2017 в 18:06