Reputation: 55
Trying to change the Output stream by using the scroll on the mouse, so when I click inside a pane, it should place an image and after using the scroll, it should use a different image. I've played a bit with it not nothing quite works and I think the easiest option is just to have a drop menu and select the size, but that would be a bit inconvenient.
private void addPane(int colIndex, int rowIndex) {
Pane pane = new Pane();
pane.setOnMouseClicked(e -> {
System.out.printf("You placed a ring on cell [%d, %d]%n", colIndex, rowIndex);
Image image = new Image(Main.class.getResourceAsStream("R_bigRing.png"));
//Change the output of image
// Image image = new Image(Main.class.getResourceAsStream("B_bigRing.png"));
// Image image = new Image(Main.class.getResourceAsStream("Y_bigRing.png"));
pane.getChildren().add(new ImageView(image));
});
grid.add(pane, colIndex, rowIndex);
}
Upvotes: 2
Views: 88
Reputation: 18630
you can listen to ScrollEvent
for a Node
checkout this code
int selectedImagePosition = 1;
ImageView imageView;
Label label;
@Override
public void start(Stage primaryStage)
{
VBox root = new VBox();
Scene scene = new Scene(root, 400, 400);
primaryStage.setScene(scene);
this.imageView = new ImageView();
imageView.setFitHeight(200);
imageView.setFitWidth(200);
File file = new File((selectedImagePosition+1) + ".png");
imageView.setImage(new Image(file.toURI().toString()));
this.label = new Label("Image : " + selectedImagePosition);
setScrollEvent(imageView);
root.getChildren().add(label);
root.getChildren().add(imageView);
primaryStage.show();
}
public void setScrollEvent(Node node)
{
node.setOnScroll((ScrollEvent event) ->
{
if (event.getDeltaY() < 0)
selectedImagePosition = selectedImagePosition+1 > 2 ? 0 : ++selectedImagePosition;
else
selectedImagePosition = selectedImagePosition-1 < 0 ? 2 : --selectedImagePosition;
System.out.println("scrollEvent : " + selectedImagePosition);
label.setText("Image : " + selectedImagePosition);
File file = new File((selectedImagePosition+1) + ".png");
imageView.setImage(new Image(file.toURI().toString()));
});
}
this code use three image of type png named (1.png , 2.png , 3.png) added in root of project
Upvotes: 1