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

  • Home
  • SEARCH
  • 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 8833219
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T08:43:40+00:00 2026-06-14T08:43:40+00:00

I am extending the BufferedImage class, to add some method like getRed, getBlue, getGreen

  • 0

I am extending the BufferedImage class, to add some method like getRed, getBlue, getGreen for getting pixel color. The problem is that my original image is BufferedImage object not my extended object. When I try to cast to extended datatype, it isnt working.
Sorry for my English

I get this error

Exception in thread "main" java.lang.ClassCastException: java.awt.image.BufferedImage cannot be cast to asciiart.EBufferedImage

Code where I am trying to cast from parent class

EBufferedImage character = (EBufferedImage)ImageClass.charToImage(letter, this.matrix_x, this.matrix_y);

My extended class

public class EBufferedImage extends BufferedImage 
{
public EBufferedImage(int width, int height, int imageType)
{
    super(width,height,imageType); 
}

/**
* Returns the red component in the range 0-255 in the default sRGB
* space.
* @return the red component.
*/
public int getRed(int x, int y) {
    return (getRGB(x, y) >> 16) & 0xFF;
}

/**
* Returns the green component in the range 0-255 in the default sRGB
* space.
* @return the green component.
*/
public int getGreen(int x, int y) {
    return (getRGB(x, y) >> 8) & 0xFF;
}

/**
* Returns the blue component in the range 0-255 in the default sRGB
* space.
* @return the blue component.
*/
public int getBlue(int x, int y) {
    return (getRGB(x, y) >> 0) & 0xFF;
}
}
  • 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-14T08:43:42+00:00Added an answer on June 14, 2026 at 8:43 am

    You have a couple options:

    1. Add a constructor to the extended class that accepts a BufferedImage and sets everything appropriately.

      public class ExtendedBufferedImage extends BufferedImage{
      
        public ExtendedBufferedImage(BufferedImage image){
            //set all the values here
        }
      
        //add your methods below
      }
      

      This one seems like a lot of work and potential for issues. If you forget to set some variable you could introduce some weird bugs, or lose information you need.

    2. Create a wrapper class that has an instance of a BufferedImage, and then add your methods in.

      public class ExtendedBufferedImage{
        private BufferedImage image;  
      
        public ExtendedBufferedImage(BufferedImage image){
           this.image = image;
        }
      
        //add your methods below
      }
      

      This is pretty reasonable, and is not to diffcult. Make the BufferedImage public or add a getter method and you can get the actual BufferedImage out of it if you need that.

    3. Create a Utility class that has your methods as being static and pass in the BufferedImage as a parameter.

      public class BufferedImageUtil{
      
        public static int getRed(BufferedImage image, int x, int y) {
          return (image.getRGB(x, y) >> 16) & 0xFF;
        }
      
        //add your other methods
      }
      

      Some people don’t like utility Classes, but I kind of like them for things like this. If you are going to use these methods all over the place, I think this is a good option.

    Personally I would go the utility class route, but if you don’t like those then wrapping it as done in option 2 is just as functional.

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

Sidebar

Related Questions

Im extending the UIButton Class to be able to set the font and color
I'm extending a constructor class' functionality via the prototype method, but I'm having trouble
I am extending the ActionResult class. In the ExecuteResult method I want to check
I'm extending a class and overriding a method. All I want to do is
when extending a class, is it impossible to override a method without also matching
I'm currently extending some existing code that utilizes a lookup table. As it is
While extending a sample Android activity that fires some other activities from its menu,
I have a class extending JPanel that I want to embed into a JFrame
I'm extending a class (ArrayBlockingQueue) that implements the Serializable interface . Sun's documentation (and
I'm extending an entities' partial class to have a method. How do I get

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.