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);
}
  • 1
    And what is the error? – Edwin Dalorzo May 02 '15 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... – nneonneo May 02 '15 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: http://stackoverflow.com/questions/2551337/instanceof-incompatible-conditional-operand-types – gregdim May 02 '15 at 14:47
  • Check your imports. Most probably you are importing the wrong Rectangle. – isnot2bad May 02 '15 at 15:16

2 Answers2

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();
}
pathfinderelite
  • 3,047
  • 1
  • 27
  • 30
  • 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. – OetziOfficial May 03 '15 at 12:31
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.

Hussein Zawawi
  • 2,907
  • 2
  • 26
  • 44