【问题标题】:cannot be cast to java.awt.image.ImageObserver不能施放到java.awt.image.imageobserver
【发布时间】:2012-04-21 10:18:57
【问题描述】:
    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)

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

谢谢

【问题讨论】:

    标签: image graphics2d sprite-sheet


    【解决方案1】:

    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 中实现必要的方法,这是一个接口。然后在最后,移除强制转换,你不能强制转换到接口。应该可以的。

    【讨论】:

    • 那么有什么方法可以做到这一点?
    • 您为什么要这样做?我以前从未使用图形编程,但我对 Java 基础知识了解得足够多,可以看出您根本无法将类实例转换为另一个随机类的另一个实例...您需要输入 public class CharacterMove extends ImageObserver 甚至尝试将其转换为图像观察者。你想做什么? drawImage 方法最后要求什么样的参数?
    • 不客气,但您知道出了什么问题吗?顺便看看我的编辑。
    • ... 它应该是“实现 ImageObserver”,而不是扩展。起初我不知道这是一个界面,因为我主要关心的是找出问题所在。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-07-15
    • 1970-01-01
    • 2017-10-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多