Here is my following code:
public void start(Stage primaryStage) throws Exception {
Pane root = new Pane();
Scene scene = new Scene(root, 500, 500, Color.RED);
ImageView dice = new ImageView(new Image(getClass().getResourceAsStream("dice.jpeg")));
dice.setX(0);
dice.setY(300);
root.getChildren().add(dice);
scene.setOnKeyPressed(e -> {
if (dice.getX() >= 0 && dice.getX() <= 500 ) {
switch (e.getCode()) {
case RIGHT:
dice.setX(dice.getX() + KEYBOARD_MOVEMENT_DELTA);
break;
case LEFT:
dice.setX(dice.getX() - KEYBOARD_MOVEMENT_DELTA);
break;
}
}
});
primaryStage.setScene(scene);
primaryStage.show();
}
In my code, my image dice
can move left and right, but I don't want it to go outside the scene. I want it to not move once it reaches to end of the scene in both left and right. I tried doing it with if
statement, but it doesn't work. Is there any way I can stop my image dice
to not move out of scene? Any help is appreciated!
You have a couple of problems in your answer. Firstly, the way you check the boundaries. If the condition is met, your keys no longer control the ImageView
. Secondly, using dice.getX()
in your test condition. It's best if you use dice.getLayoutBounds().getMaxX()
and dice.getLayoutBounds().getMinX()
. Also, I recommend using scene.getWidth()
instead of hard coding the width of the Scene
, because the Scene
width can be changed. <-(in your posted code).
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
/**
*
* @author blj0011
*/
public class JavaFxTestingGround extends Application {
double KEYBOARD_MOVEMENT_DELTA = 5;
@Override
public void start(Stage primaryStage) throws IOException {
Pane root = new Pane();
Scene scene = new Scene(root, 500, 500, Color.RED);
ImageView dice = new ImageView(createImage("https://cdn.discordapp.com/attachments/250163910454280192/296377451599364107/Untitled.png"));
dice.setFitHeight(100);
dice.setFitWidth(100);
dice.setX(0);
dice.setY(300);
root.getChildren().add(dice);
scene.setOnKeyPressed(e -> {
System.out.println(dice.getLayoutBounds().getMinX() + " : " + dice.getLayoutBounds().getMaxX() + " : " + scene.getWidth());
switch (e.getCode()) {
case RIGHT:
dice.setX(dice.getX() + KEYBOARD_MOVEMENT_DELTA);
break;
case LEFT:
dice.setX(dice.getX() - KEYBOARD_MOVEMENT_DELTA);
break;
}
if (dice.getLayoutBounds().getMinX() < 0)
{
dice.setX(0);
}
else if(dice.getLayoutBounds().getMaxX() > scene.getWidth() )
{
dice.setX(dice.getX() - KEYBOARD_MOVEMENT_DELTA);
}
});
primaryStage.setScene(scene);
primaryStage.show();
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
Image createImage(String url)
throws IOException {
URLConnection conn = new URL(url).openConnection();
conn.setRequestProperty("User-Agent", "Wget/1.13.4 (linux-gnu)");
try (InputStream stream = conn.getInputStream()) {
return new Image(stream);
}
}
}