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)
矢印キーを押したときにキャラクターが動くアニメーションを作成しようとしています。しかし、画像をペイントしようとすると、このエラーが発生します。画像の特定のセクションを描画する方法を使用しています。別のプロジェクトで同じ画像を使用してこの方法をテストしましたが、うまくいきました。何が悪いのかわかりません。どんな助けでも大歓迎です!
ありがとう