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 6928793
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T11:14:05+00:00 2026-05-27T11:14:05+00:00

I’m currently working on a game using the Component Pattern , and have always

  • 0

I’m currently working on a game using the Component Pattern, and have always wondered how this is done.
I have an entity which is practically just a bag of components. Each components extends the Component class, which just has some basic functionality.

Extending the component class, new components are created, for handeling input, graphics etc. Now comes the problem; When I’m trying to get a specific component from the entity, it always returns the basic Component class, which prevents me from using the specific component functions.

    public class GameEntity
{
    private ArrayList<Component> components;

    public GameEntity()
    {
        components = new ArrayList<Component>();
    }

    public void addComponent(Component component)
    {
        components.add(component);
    }

    public void update()
    {

    }

   public Component getComponent(Class type)
   {
       for (Component component : components)
       {
            if(component.getClass() == type)
            {
                //return component as Class;

            }
       }
       return null;
   }


    public void draw(Canvas canvas)
    {
        for (Component component : components)
        {
            component.update();
            component.draw(canvas);
        }
    }

}

Some example components:

public class GraphicsComponent extends Component {

public Bitmap bitmap;
public Rect currentFrameRect;
private ArrayList spriteAnimations;
public SpriteAnimation currentAnimation; public int x = 0; public int y = 50; public
GraphicsComponent() {
spriteAnimations = new ArrayList(); }

/**
 * Adds image [converts to spriteanimation]
 * @param image
 */
public void addImage(Bitmap image, String label)
{
    Rect[] tmpRects = {new Rect(0, 0, image.getWidth(), image.getHeight())} ;
    addAnimation(new SpriteAnimation(
            image, tmpRects, label
    ));
}

public void addAnimation(SpriteAnimation spriteAnimation)
{
    spriteAnimations.add(spriteAnimation);

   if(currentAnimation == null)
   {
       currentAnimation = spriteAnimation;
   }
}


@Override
public void update()
{
   currentFrameRect = currentAnimation.frames[currentAnimation.currentFrame];
}

@Override public void draw(Canvas canvas) {

    if(currentAnimation != null)
    {
        currentAnimation.draw(x, y, canvas);
    }     }
public int getWidth()
{
    return currentAnimation.frames[currentAnimation.currentFrame].width();
}

public int getHeight()
{
    return currentAnimation.frames[currentAnimation.currentFrame].height();
}
}


public class InteractiveComponent extends Component
{
    public GraphicsComponent graphics;

    public InteractiveComponent(GraphicsComponent graphics)
    {
        this.graphics = graphics;
    }

    public boolean isOver(int tapX, int tapY)
    {
        //left top right bottom
        if(tapX > graphics.x && tapX < graphics.x + graphics.getWidth() &&
           tapY > graphics.y && tapY < graphics.y + graphics.getHeight()
        )
        {
            return true;
        }
        return false;
    }

}

There seem to be some problems with the code formatting, but it should be clear.
I can’t access any specific functions like getHeight() in the graphicComponent or isOver() from the interactiveComponent because I just get a basic Component returned.

I would like to return a GraphicsComponent or InteractiveComponent based on the Class that I pass into getComponent().

  • 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-05-27T11:14:06+00:00Added an answer on May 27, 2026 at 11:14 am

    What you’re trying to do seems architecturally suspect to me, but nonetheless it can be done in a “clean” way using generics, and you’re already passing in the class object as a type token:

    class Animals {
        List<Animal> animals = new ArrayList<Animal>();
    
        public <T extends Animal> T getAnimal(Class<T> type) {
            for (Animal animal : animals) {
                if (type.isAssignableFrom(animal.getClass())) {
                    return type.cast(animal);
                }
            }
            return null;
        }
    }
    
    class Main {
        public static void main(String[] args) {
            Animals animals = new Animals();
            animals.getAnimal(Cat.class).meow();
            animals.getAnimal(Dog.class).bark();
        }
    }
    
    interface Animal {}
    
    class Cat implements Animal {
        public void meow() {}
    }
    
    class Dog implements Animal {
        public void bark() {}
    }
    

    You can == or equals() instead of the isAssignableFrom() check depending on the exact behaviour you want.

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
this is what i have right now Drawing an RSS feed into the php,
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I have a text area in my form which accepts all possible characters from
I have thousands of HTML files to process using Groovy/Java and I need to
I have some data like this: 1 2 3 4 5 9 2 6
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and

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.