-2

I have a node(Gridpane node) and i want to get instanceof from class but either a node or any object i can not get instanceof of my class, i need to add something to a class to do "instanceof" or am i doing it wrong?

public void Move(GridPane gridPane) {
    for (Node node : gridPane.getChildren()) {
        if (GridPane.getColumnIndex(node) == this.x && GridPane.getRowIndex(node) == this.y - 2) {
            if (node instanceof blackPawn) {
                gridPane.setRowIndex(node, this.y);
                gridPane.setColumnIndex(node, this.x);
            }
            break;
        }
    }
}

Inconvertible types; cannot cast 'javafx.scene.Node' to 'sample.Figures.blackPawn'

blackPawn class

package sample.Figures;

import javafx.event.EventHandler;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.input.MouseEvent;

public class blackPawn {
public int x;
public int y;
public ImageView IMG = createChestImage();
private int j = 0;

public blackPawn(){}

public blackPawn(int x,int y)
{
    this.x = x;
    this.y = y;
}

private ImageView createChestImage() {
    final ImageView iv = new ImageView(new Image("sample/Sprites/blackPawn.png"));

    iv.setOnMouseEntered(new EventHandler<MouseEvent>() {
        @Override
        public void handle(MouseEvent event) {
            iv.setImage(new Image("sample/Sprites/blackPawnStroke.png"));
            j = 0;
        }
    });

    iv.setOnMouseExited(new EventHandler<MouseEvent>() {
        @Override
        public void handle(MouseEvent event) {
            if(j == 0)
            iv.setImage(new Image("sample/Sprites/blackPawn.png"));
        }
    });

    iv.setOnMouseClicked(new EventHandler<MouseEvent>() {
        @Override
        public void handle(MouseEvent event) {
            iv.setImage(new Image("sample/Sprites/blackPawnStroke.png"));
            j = 1;
        }
    });

    return iv;
}

}
xCrazy
  • 3
  • 4
  • Attach the code of the blackPawn class – NiVeR May 14 '18 at 12:38
  • 1
    does your `blackPawn` extend the `Node` class? – Lino May 14 '18 at 12:38
  • what line is that exception being thrown? – dave May 14 '18 at 12:39
  • 6
    Don't name your classes starting with lower case – Thiyagu May 14 '18 at 12:39
  • no it doesent extend blackPawn class – xCrazy May 14 '18 at 12:39
  • What do you mean by "i want to get instanceof from node(Gridpane node)"? If you know that `node` is instance of class `javafx.scene.Node` and that class is not related to `sample.Figures.blackPawn` (they can't have same descendent type since class can extend only one parent class) then what is the point of using `instanceof` if we know it must always result in `false`? This is obvious programmer mistake so compiler is stopping such code from compiling. – Pshemo May 14 '18 at 12:39
  • 1
    https://stackoverflow.com/questions/9107895/instanceof-operator-in-java-for-comparing-different-classes?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa – Zeromus May 14 '18 at 12:40
  • 1
    Possible duplicate of [What is the 'instanceof' operator used for in Java?](https://stackoverflow.com/questions/7313559/what-is-the-instanceof-operator-used-for-in-java) – Lino May 14 '18 at 12:40
  • The instanceof is not the problem here, it's the calls to the setters that won't compile. – f1sh May 14 '18 at 12:44
  • setters compile perfectly – xCrazy May 14 '18 at 12:45

2 Answers2

3

You're trying a cast that will never be possible, and the compiler won't let you do that.

For example,

String s = "hello";
Integer i = (Integer) s;

is not allowed because String and Integer aren't in the same class hierarchy, so this cast will never be possible. The compiler will let you do this:

String s = "hello";
Object o = s;
Integer i = (Integer) o;

but this will throw a ClassCastException at runtime.

In your case, you probably need to extract the Figure from the Node in some way to determine if there's a black pawn at that node, as in

if (getFigureForNode(node) instanceof BlackPawn) {
     ...

with a method

Figure getFigureForNode(Node n)

assuming BlackPawn extends (or implements) Figure.

Code Smell

Note that using instanceof is usually not good design. You probably want to use a method isBlackPawn(Figure f) instead, along the lines of return f.getFigureType() == FigureType.PAWN && f.getPlayerColor() == PlayerColor.WHITE.

daniu
  • 14,137
  • 4
  • 32
  • 53
  • Figure is a package blackPawn class is not extended or doesent implement anything. – xCrazy May 14 '18 at 12:44
  • @xCrazy well it probably should :) – daniu May 14 '18 at 12:45
  • i think i described my problem really bad.So i have package called "sample" there are packages "Figures" "Field" and "Sprites" the Figures package contains the figures Pawns Rooks (white and black) etc etc. the nodes in my group are Images(ImageView) of The Figures and i just want to if node is blackpawn image then move the image to coordinates – xCrazy May 14 '18 at 12:50
  • @xCrazy Unrelated, but why are the white and black pawns different classes? If we're talking normal chess they have the exact same behavior, and a single different property, which doesn't even need to be in the class itself depending on how things are architected. – Dave Newton May 14 '18 at 12:55
  • @xCrazy I got that. The "cast" part of my anwer is your main problem, your class design notwithstanding. You do need a mapping between your node and the figure in some way; normally, you shouldn't operate on the images but have an underlying model containing the actual figures, which would be what's feeding the display rather than the other way around. But this really goes too far for this particular casting problem. – daniu May 14 '18 at 12:56
  • dunno i overthink it now i needed to create a bool for the classes like is_white.kinda failed here – xCrazy May 14 '18 at 12:58
  • i am a bit new to JavaFX wanted to create something interesting for experience for me the group with images solution was the only one – xCrazy May 14 '18 at 13:02
-1

The java instanceof operator is used to test whether the object is an instance of the specified type (class or subclass)

http://www.java2s.com/Tutorial/Java/0060__Operators/TheinstanceofKeyword.htm

KunLun
  • 3,109
  • 3
  • 18
  • 65