-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;
}

}
10
  • Attach the code of the blackPawn class Commented May 14, 2018 at 12:38
  • 1
    does your blackPawn extend the Node class? Commented May 14, 2018 at 12:38
  • 6
    Don't name your classes starting with lower case Commented May 14, 2018 at 12:39
  • 1
    stackoverflow.com/questions/9107895/… Commented May 14, 2018 at 12:40
  • 1
    Possible duplicate of What is the 'instanceof' operator used for in Java? Commented May 14, 2018 at 12:40

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

7 Comments

Figure is a package blackPawn class is not extended or doesent implement anything.
@xCrazy well it probably should :)
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 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.
@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.
|
-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

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.