FlyRanger FlyRanger - 4 months ago 35
CSS Question

JavaFX show RGB value for pixel in ImageView

I am trying to make an application which holds many elements in its GUI and I am using JavaFX to make one. I've stumbled upon a problem, that I can't really detect when a pixel in ImageView is being hovered over with a mouse.

I've tried with CSS for hbox that contains:

#hbox:hover {
-fx-background-color: #FFFFFF;
}


and... I know it's stupid, but at least tried that:

if (hboxImage != null /*&& hboxImage.isHover()*/) {
hboxImage.setOnMouseClicked(event -> {
double i = hboxImage.getX();
double j = hboxImage.getY();
System.out.println("i: " + i + " j: " + j);
});
}


I know that doesn't do closely what I do I need to do, but that's the only good I've found for hovering.

I am thinking of creating some sort of table or grid on top of the ImageView to show me RGB color from array, but that doesn't seem to be right.

Is there any way in JavaFX to detect mouse hovering event on every pixel of an ImageView (or any other view for it matters)?

Answer

You can use onMouseMoved event handler:

.setOnMouseMoved(event->{
    System.out.println("x: "+event.getX()+"; y: "+event.getY());
});

It is triggered when mouse is moving over the node and event contains current mouse coordinates.

Comments