Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7760499
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T13:54:13+00:00 2026-06-01T13:54:13+00:00

import java.awt.*; import java.awt.image.ImageObserver; import javax.swing.*; public class CharacterMove { CharacterCollision CharacterCollision; public static

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

}

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T13:54:15+00:00Added an answer on June 1, 2026 at 1:54 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

package test; import java.awt.*; import java.awt.event.*; import java.awt.geom.Ellipse2D; import java.awt.image.BufferedImage; import javax.swing.*; public class
Here is my code: import javax.swing.*; import java.awt.*; public class PanelModel { public static
import java.awt.*; import java.awt.image.BufferedImage; import javax.swing.*; public class Main { JFrame jf; Main() {
This is called GoHomeAction.java import javax.swing.*; import java.awt.Image; import java.awt.event.ActionEvent; import static java.awt.event.KeyEvent.VK_H; public
import java.awt.Graphics; import javax.swing.*; public class Demo { JFrame jf; JLabel[] labels; JPanel panel;
import java.awt.Color; import java.awt.Graphics; import javax.swing.JFrame; public class Sheet extends JFrame{ private String[] line
Here I found this code: import java.awt.*; import javax.swing.*; public class FunWithPanels extends JFrame
When I try to compile this: import java.awt.* ; class obj { public static
If I have this code : import javax.swing.* import java.awt.image.* def xMap = [
import java.awt.*; import java.awt.image.*; import java.awt.event.*; import javax.imageio.*; import java.lang.*; import java.io.*; import javax.swing.*;

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.