Search code examples
imagegraphics2dsprite-sheet

cannot be cast to java.awt.image.ImageObserver


    import java.awt.*;
import java.awt.image.ImageObserver;

import javax.swing.*;

public class CharacterMove {

CharacterCollision CharacterCollision;

public static int vel = 2;
public final int baseVel = 2;
public int spriteHeight = 23;
public int spriteWidth  =16;
public int x_pos =  400/2;
public int y_pos = 400/2;
public int count0 = 0,count1 = 0,count2 = 0,count3=0,count4 = 0,count5 = 0;
public ImageIcon Characterobj;
public Image Character;
public boolean MovingUp = false;
public boolean MovingDown = false;
public boolean MovingRight = false;
public boolean MovingLeft = false;
public boolean MovingStill = false;
public boolean UpRight = false;
public boolean UpLeft = false;
public boolean DownRight = false;
public boolean DownLeft = false;




public CharacterMove(){
    Characterobj = new ImageIcon(this.getClass().getResource("Character.png"));
    Character = Characterobj.getImage();


}

public void move(boolean moving) {
    if (MovingLeft) {

        x_pos-=vel;
    }
    if (MovingRight) {
        x_pos+=vel;
    }
    if (MovingUp) {
        y_pos-=vel;

    }
    if (MovingDown) {
        y_pos+=vel;
    }
    if(MovingDown&&MovingLeft){
        DownLeft = true;
        vel = 1;
    }else{
        DownLeft = false;
    }
    if(MovingDown&&MovingRight){
        DownRight = true;
        vel = 1;
    }else{
        DownRight = false;
    }
    if(MovingUp&&MovingLeft){
        UpLeft = true;
        vel = 1;
    }else{
        UpLeft = false;
    }
    if(MovingUp&&MovingRight){
        UpRight = true;
        vel = 1;
    }else{
        UpRight = false;
    }

    if(UpRight||UpLeft||DownLeft||DownRight){

        vel = 1;
    }else{
        vel = baseVel;
    }

}
public void draw(Graphics g){

    Graphics2D g2d = (Graphics2D) g;

    if(MovingUp){

        drawSpriteFrame(Character, g2d, x_pos, y_pos, 0, count0, 16, 23);
        if(count0 <3){
            count0++;
        }else{
            count0=0;
        }
    }else if(MovingDown){

        drawSpriteFrame(Character, g2d, x_pos, y_pos, 2, count1, 16, 23);
        if(count1 <3){
            count1++;
        }else{
            count1=0;
        }
    }else if(MovingLeft){

        drawSpriteFrame(Character, g2d, x_pos, y_pos, 3, count2, 16, 23);
        if(count2 <3){
            count2++;
        }else{
            count2=0;
        }
    }else if(MovingRight){

        drawSpriteFrame(Character, g2d, x_pos, y_pos, 1, count3, 16, 23);
        if(count3 <3){
            count3++;
        }else{
            count3=0;
        }
    }


}
public int characterRectX(){
    return 16;

}
public int characterRectY(){
    return 23;

}
public void drawSpriteFrame(Image source, Graphics2D g2d, int x, int y,
        int columns, int row, int width, int height) {
    int frameX = (row) * width;
    int frameY = (columns) * height;
    g2d.drawImage(source, x, y, x + width, y + height, frameX, frameY,frameX + width, frameY+height ,(ImageObserver) this);
}

}

Hello, Every time I run this code, I get the error:

Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: CharacterMove cannot be cast to java.awt.image.ImageObserver
    at CharacterMove.draw(CharacterMove.java:91)

I am trying to animate a character moving, when arrow keys are pressed. but I am getting this error when the image trys to be painted. I am using a method, that draws a certain section of the image, there I tested this method out, using the same picture in a different project, and it worked fine. I do not know what is wrong. Any help would be greatly appreciated!

Thanks


Solution

  • g2d.drawImage(source, x, y, x + width, y + height, frameX, frameY,frameX + width, frameY+height ,(ImageObserver) this);

    Last parameter, you try to convert "this" (an instance of the CharacterMove class) to an instance of the ImageObserver class... It doesn't seem to me this is possible, as CharacterMove does not extend ImageObserver.

    Edit: I've had a look at the JavaDoc now. Simply make:

    public class CharacterMove implements ImageObserver
    

    and implement the necessary methods from ImageObserver, which is an interface. Then at the end, remove the cast, you can't cast to an Interface. That should work.