Problem with loading image in JavaFX in IntelliJ Idea - java

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

Related

File Location with Java FX Image and ImageView [duplicate]

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.

javaFX jar application

I have a problem with javaFX jar application.
When I compile my project in IDEA it looks like this:
enter image description here
But when I build jar application, after pressing AUTHORIZATION in first window,
it doesn't open next window. I use FXML scenes.
First window is calling from start() method in Main class, others from controllers classes.
Method in main class.
public void start(Stage primaryStage) throws Exception {
Parent parent = FXMLLoader.load(getClass().getResource("Interface.fxml"));
Scene scene = new Scene(parent);
primaryStage.setScene(scene);
primaryStage.setTitle("Hello Client");
primaryStage.show();
}
And one of the controllers class:
#FXML
public void Autentification(ActionEvent actionEvent) throws Exception {
UserInform.AccauntName = name;
UserInform.UserName = name;
int intPort = Integer.parseInt(port);
messages.PrintMessage("Authentication started\n", Out);
ConnectionForUI.session.tryConnect(name, pass, host, intPort);
//Run next window
FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("../ClientField.fxml"));
Parent root = (Parent) fxmlLoader.load();
Stage stage = new Stage();
stage.setTitle("Client");
stage.setScene(new Scene(root));
stage.show();
}
Autentification method runs when I press Authorization button, and here runs next window.
I'm new in Java FX so if you know how to repaid this problem, I will be very grateful.
Exception:
enter image description here
Problem was in the way to me FXML files. You have to set absolute way to files, like"/file_name.fxml". It helped to me.

Java FX crash when changing scene

I'm trying to make a POS using javaFX but whenever I try to change the scene it keeps crasing. Any ideas of how to fix?
Main class: https://pastebin.com/4bspSL9N
SceneManager: https://pastebin.com/SU99DVgf
OutputHelper.log() is just a system.out.println()
output log: https://pastebin.com/GZTPRgNp
You have not initialized your primaryStage in the SceneManager. Try adding this.primaryStage = primaryStage; in your setup method.
Try this in your SceneManager class:
public static void loadScreen(Parent parent) throws IOException {
Platform.runLater(() -> {
Stage stage = new Stage(StageStyle.UNDECORATED);
// stage.setMaximized(true);
stage.setFullScreen(true);
// stage.setFullScreenExitKeyCombination(KeyCombination.NO_MATCH);
Scene scene = new Scene(parent);
stage.setScene(scene);
stage.show();
});
}
Then to Load new screen use it like :
String Screen = "YourScreen.fxml";
Parent root = FXMLLoader.load(getClass().getResource(Screen));
SceneManager.loadScreen(root);
hope this will help
This a bit late to the party but we will post for posterity
We name all our Anchor Pane's so we know who they are ie mynamePane
Then when you click a Button to go somewhere you only need to know where you are and where you want to go
private void doSEARCH() throws IOException{
stage = (Stage)searchPane.getScene().getWindow();// pane you are ON
viewtxPane = FXMLLoader.load(getClass().getResource("tableview.fxml"));// pane you are GOING TO
Scene scene = new Scene(viewtxPane);// pane you are GOING TO
scene.getStylesheets().add(getClass().getResource("checkbook.css").toExternalForm());
stage.setScene(scene);
stage.setTitle("Check Book Transactions");
stage.show();
stage.sizeToScene();
stage.centerOnScreen();
}

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