0
    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);
}

}

您好,每次运行此代码时,都会出现以下错误:

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

当按下箭头键时,我正在尝试为移动的角色设置动画。但是当尝试绘制图像时出现此错误。我正在使用一种方法,绘制图像的某个部分,在那里我测试了这种方法,在不同的项目中使用相同的图片,它工作正常。我不知道出了什么问题。任何帮助将不胜感激!

谢谢

4

1 回答 1

0

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

最后一个参数,您尝试将“this”(CharacterMove 类的实例)转换为 ImageObserver 类的实例……在我看来,这是不可能的,因为 CharacterMove 没有扩展 ImageObserver。

编辑:我现在看过 JavaDoc。只需制作:

public class CharacterMove implements ImageObserver

并从 ImageObserver 中实现必要的方法,这是一个接口。然后在最后,移除强制转换,你不能强制转换到接口。那应该行得通。

于 2012-04-08T21:10:40.363 回答