File Location with Java FX Image and ImageView [duplicate] - java

not just this, other codes have the same problem. just can't use ImageView.
Environment : macOS, IntelliJ
Caused by: java.lang.IllegalArgumentException: Invalid URL: Invalid URL or resource not found
public class ShowHboxVbox extends Application {
static String s = "/Users/fangyuan/Desktop/PIC.png";
#Override
public void start(Stage primaryStage) {
BorderPane borderPane = new BorderPane();
borderPane.setTop(getHbox());
Scene scene = new Scene(borderPane);
primaryStage.setTitle("title");
primaryStage.setScene(scene);
primaryStage.show();
}
private HBox getHbox() {
HBox hBox = new HBox(15);
hBox.setPadding(new Insets(15,15,15,15));
hBox.setStyle("-fx-background-color: gold");
hBox.getChildren().add(new Button("computer science"));
hBox.getChildren().add(new Button("chemist"));
ImageView imageView = new ImageView(new Image(s));
hBox.getChildren().add(imageView);
return hBox;
}
}

The Image constructor takes a url as a parameter. If you don't put a protocol in it, then it assumes that the item comes off of the classpath. Obviously, /Users/fangyuan/Desktop/PIC.png won't be in your classpath.
To read from a file instead of the classpath, then stick the file:// protocol in front of the path you want to read:
file:///Users/fangyuan/Desktop/PIC.png
Or
Paths.get("/Users/fangyuan/Desktop/PIC.png").toUri().toString()
which would output the same thing.

Related

JavaFx: ImageView disappears after moving through the scene

I have a code written using javafx and java 11. As I showed in the below code snippet, when I move my ImageView to right of the scene, The ImageView disappears.
At first:
and then after moving it using arrow keys with the help of scene event listener:
then:
and finally:
I don't use fxml. Here is my demo which has the problem too.
public class HelloApplication extends Application {
public String fetchResource(String path) {
return Objects.requireNonNull(getClass().getResource(path)).toString();
}
#Override
public void start(Stage stage) throws IOException {
Rectangle r = new Rectangle(100, 100);
ImageView spaceShip = new ImageView(fetchResource("spaceShip.png"));
spaceShip.setFitHeight(100);
spaceShip.setFitWidth(100);
spaceShip.setX(0);
spaceShip.setY(0);
EventHandler<KeyEvent> keyListener = event -> {
if (event.getCode() == KeyCode.RIGHT) {
spaceShip.setX(spaceShip.getX() + 20);
}
};
Group game = new Group(spaceShip);
Scene scene = new Scene(game, 1840, 1080);
scene.setOnKeyPressed(keyListener);
stage.setTitle("Hello!");
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {
launch();
}
}
I should mention that I create a rectangle and check the scenario with that, And there was not any problem. I think there is some problem with ImageView.
I couldn't reproduce the issue with your code, but I could with SceneBuilder. You may get the desired result if you wrap the Group in a Pane (at least it works in SceneBuilder).
Group game = new Group(spaceShip);
Pane pane = new Pane(game);
Scene scene = new Scene(pane, 1840, 1080);
stage.setScene(scene);
stage.show();

Problem with loading image in JavaFX in IntelliJ Idea

I have a Maven project in InteliJ Idea IDE and I can't load an image even though it's in the resources folder. I just get a blank window and "Image loading error? true". Any ideas?
public void start(Stage primaryStage) throws Exception {
StackPane sp = new StackPane();
Image img = new Image("file:pawn.png");
ImageView imgView = new ImageView(img);
System.out.println("Image loading error? " + img.isError());
sp.getChildren().add(imgView);
Scene scene = new Scene(sp);
primaryStage.setScene(scene);
primaryStage.show();
}

When I use SubScene my 3D models start to glitch. Any ideas why?

I am building an application that has 2 SubScenes, one for GUI and one for displaying 3D models (in my case OBJ).
For the import of the OBJ file I use the ObjModelImporterJFX library by InteractiveMesh.
Like the title says and as seen in this picture the models start to glitch when they are handled by a SubScene. It seems like some covered parts that should not be visible are rendered like they were. Just for comparison this is how it looks normally, when the models are handled direcly by the scene.
Here is my code:
public class Main extends Application{
#Override
public void start(Stage stage) throws Exception{
Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
PerspectiveCamera camera = new PerspectiveCamera(true);
Group model = loadOBJ("course.obj");
Group course = new Group(model);
BorderPane borderPane = new BorderPane();
Pane pane1 = new Pane(course);
Pane pane2 = new Pane(root);
SubScene subScene1 = new SubScene(pane1, 1000, 720);
subScene1.setCamera(camera);
SubScene subScene2 = new SubScene(pane2,500,500);
borderPane.setLeft(subScene1);
borderPane.setRight(subScene2);
Scene scene = new Scene(borderPane, 1280,720, true);
stage.setScene(scene);
stage.show();
}
private Group loadOBJ(String fileName){
URL url = getClass().getResource(fileName);
Group modelRoot = new Group();
ObjModelImporter importer = new ObjModelImporter();
importer.read(url);
for (MeshView view : importer.getImport()){
modelRoot.getChildren().add(view);
}
return modelRoot;
}
public static void main(String[] args){
launch(args);
}
}
I hope that someone has an idea why this is. Thanks in advance :)
UPDATE: To fix this enable depthBuffer by changing the constructor of the SubScene from
SubScene subScene1 = new SubScene(pane1, 1000, 720);
to
SubScene subScene1 = new SubScene(pane1, 1000, 720, true, null);

JavaFX bulk edit images

I'm trying to edit five images I have in a folder at once (using javafx motion blur), rather than select them one after the other. This is my code, I'm not exactly sure what I'm doing wrong but when I run it, only the last image in the folder gets edited. The others remain as they are.
public class IterateThrough extends Application {
private Desktop desktop = Desktop.getDesktop();
#Override
public void start(Stage primaryStage) throws IOException {
File dir = new File("filepath");
File [] directoryListing = dir.listFiles();
if (directoryListing != null) {
for (File file : directoryListing) {
if(file.getName().toLowerCase().endsWith(".jpeg")){
//desktop.open(file);
Image image = new Image(new FileInputStream(file));
//Setting the image view
ImageView imageView = new ImageView(image);
//setting the fit height and width of the image view
imageView.setFitHeight(600);
imageView.setFitWidth(500);
//Setting the preserve ratio of the image view
imageView.setPreserveRatio(true);
// instantiate Motion Blur class
MotionBlur motionBlur = new MotionBlur();
// set blur radius and blur angle
motionBlur.setRadius(15.0);
motionBlur.setAngle(110.0);
//set imageView effect
imageView.setEffect(motionBlur);
//Creating a Group object
Group root = new Group(imageView);
//Creating a scene object
Scene scene = new Scene(root, 600, 500);
//Setting title to the Stage
primaryStage.setTitle("Loading an image");
//Adding scene to the stage
primaryStage.setScene(scene);
//Displaying the contents of the stage
primaryStage.show();
}
}
}
}
public static void main(String[] args) {
launch(args);
}
}
first, you shouldn't just let the primaryStage show() in the for-each loop, because when the primaryStage first show(), the fx thread pause on that instruction, the remaining pictures will not be read and when you close the window, the loop won't continue to go on for remaining, it represents the end of the application.
so it's better to let the primaryStage show() outside the each-loop after all image has been read.
second, you shouldn't use the "Group" container to contains all images, better use VBox/HBox/FlowPane, make sure above image won't covers below's.
here's modified code for reference.
public class IterateThrough2 extends Application{
private Desktop desktop = Desktop.getDesktop();
#Override
public void start(Stage primaryStage) throws IOException{
File dir = new File("filepath");
File[] directoryListing = dir.listFiles();
FlowPane root = new FlowPane();
if(directoryListing != null){
for(File file : directoryListing){
if(file.getName().toLowerCase().endsWith(".jpeg")){
Image image = new Image(new FileInputStream(file));
ImageView imageView = new ImageView(image);
imageView.setFitHeight(600);
imageView.setFitWidth(500);
imageView.setPreserveRatio(true);
MotionBlur motionBlur = new MotionBlur();
motionBlur.setRadius(15.0);
motionBlur.setAngle(110.0);
imageView.setEffect(motionBlur);
root.getChildren().add(imageView);
}
}
}
Scene scene = new Scene(root, 600, 500);
primaryStage.setTitle("Loading an image");
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args){
launch(args);
}
}

How to prevent javafx from causing illegalargumentexception in image?

I was trying to display a picture in a box with JavaFX. I followed methods documented on Oracle, but it still did not work, though it was extremely similar to the example shown on Oracle. My code is here:
public class TesterJavaFX extends Application {
#Override
public void start(Stage primaryStage) {
Image img = new Image("character.png");
ImageView imgview = new ImageView();
imgview.setImage(img);
imgview.setFitWidth(100);
imgview.setPreserveRatio(true);
imgview.setSmooth(true);
imgview.setCache(true);
HBox box = new HBox();
box.getChildren().add(imgview);
StackPane root = new StackPane();
root.getChildren().add(box);
Scene scene = new Scene(root, 300, 250);
primaryStage.setTitle("Hello World!");
primaryStage.setScene(scene);
primaryStage.show();
}
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
}
I have a file called rocket.png in the "src" directory. It even shows up on the IDE. But Java causes a illegal argument exception, and i don't know why. Can anybody help me? Thanks.
Note: The imports are all present.
The string passed to the Image constructor is a URL. If the image file is in the root of your classpath, the following should work:
Image img = new Image(getClass().getResource("/character.png").toExternalForm());

Categories

Resources