0

First off all, I made a "Game-Renderer".

My problem is that, when I need to draw the current Element: I need to know if it's a Rectangle, Circle, or an Image and so on.

My Classes (Rectangle, Circle,...) are extending from Graphic.

public class Rectangle extends Graphic {...}

And if I want to draw them I look in the List ArrayList<Graphic>

for(index = 0;index < graphicObjects.size();index++){
    currentElement = graphicObjects.get(index);

    if(currentElement instanceof Rectangle) { // Here is an error.
    Rectangle r = (Rectangle) currentElement;
    // here the drawing.
    }
}

Thanks for helping (Goggle wasn't helpful) :)

Edit:

Error is: "Incompatible conditional operand types Graphic and Rectangle"

And why I need to know the type: My code:

public static Image getImage(Graphics g,int width, int height) {
    int imgWidth = width;
    int imgHeight = height;
    BufferedImage bfImage = new BufferedImage(imgWidth, imgHeight, BufferedImage.TYPE_INT_ARGB);
    Graphics graphics = bfImage.getGraphics();

    for (int index = 0; index < grObjList.size(); index++) {
        Graphic gr = grObjList.get(index);
        if(gr instanceof Rectangle){
            graphics.setColor(gr.color);
            graphics.fillRect(gr.x, gr.y, gr.width, gr.height);
        }
    }
    return bufferedImagetoImage(bfImage);
}
4
  • 1
    And what is the error? Commented May 2, 2015 at 13:27
  • 3
    Why would you need to know the type? The point of virtual methods and inheritance is that you don't need to know the type. Just make a draw function and override it in subclasses... Commented May 2, 2015 at 13:28
  • In general you should follow the practice suggested by nneonneo above and pathfinderelite answer. But in order to solve the mystery in case you use eclipse have a look at the answer with the most votes here: stackoverflow.com/questions/2551337/… Commented May 2, 2015 at 14:47
  • Check your imports. Most probably you are importing the wrong Rectangle. Commented May 2, 2015 at 15:16

2 Answers 2

3

To avoid using instanceOf, have Graphic implement an abstract draw method. Then, override draw in your Rectangle, Circle, etc classes. Then you can do

for(index = 0;index < graphicObjects.size();index++){
    currentElement = graphicObjects.get(index);
    currentElement.draw();
}
Sign up to request clarification or add additional context in comments.

1 Comment

This version worked best, but didn't solved my question/problem as I needed. Because I don't understood the 'extends': Rectangle extends from Graphic, so Rectangle is a Graphic. And if I put it in the Graphic-List I should check if it is a Rectangle, Cirlce, etc... Maybe I need that for later.
0

You are getting an error because you are trying to say that the supertype Graphic is a Rectangle which is not a Rectangle is a Graphic.

So make sure you have a function in the super type and override it in the subtypes so you dont need to do any casting.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.