r/JavaFX • • Jun 18 '21

beginner need help

hello, i just started my java classes, and i have a small homework in javaFX that i don't understand, can some one help me with it?

here is the question: Write a program that displays five images in a Border Pane

0 Upvotes

15 comments sorted by

3

u/your_mom_lied Jun 19 '21

That’s a pretty simple task.

Put your images in a project level directory.

Create your border pane and attach it to the scene.

Create your image objects and then add them as children of your border pane.

Google searches.

How to add a border pane to a scene

How to create an image object in javafx

How to add children to a border pane.

How to show a scene on the stage in javafx

2

u/Pussyslayerl Jun 19 '21

Thnx a lot

1

u/Pussyslayerl Jun 19 '21

How to create an image object in javafx

sorry to bother you again, i did as you say and i have no error in the code but when i run it i Have this:

Exception in Application start methodjava.lang.reflect.InvocationTargetException at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.base/java.lang.reflect.Method.invoke(Method.java:566) at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:464) at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:363) at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.base/java.lang.reflect.Method.invoke(Method.java:566) at java.base/sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:1051)Caused by: java.lang.RuntimeException: Exception in Application start method at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:900) at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication$2(LauncherImpl.java:195) at java.base/java.lang.Thread.run(Thread.java:829)Caused by: java.lang.IllegalArgumentException: Invalid URL: unknown protocol: c at javafx.graphics/javafx.scene.image.Image.validateUrl(Image.java:1128) at javafx.graphics/javafx.scene.image.Image.<init>(Image.java:618) at App.start(App.java:14) at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$9(LauncherImpl.java:846) at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runAndWait$12(PlatformImpl.java:474) at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:447) at java.base/java.security.AccessController.doPrivileged(Native Method) at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$11(PlatformImpl.java:446) at javafx.graphics/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:96) at javafx.graphics/com.sun.glass.ui.win.WinApplication._runLoop(Native Method) at javafx.graphics/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:174) ... 1 moreCaused by: java.net.MalformedURLException: unknown protocol: c at java.base/java.net.URL.<init>(URL.java:652) at java.base/java.net.URL.<init>(URL.java:541) at java.base/java.net.URL.<init>(URL.java:488) at javafx.graphics/javafx.scene.image.Image.validateUrl(Image.java:1122) ... 11 moreException running application App

thnx for the help

3

u/mimoguz Jun 19 '21

Are you trying to load your images from c drive on Windows? Put "file:" before the path: Image("file:c:/path/to/the/image.png"). There is an example for that in the documents 😀

1

u/Pussyslayerl Jun 19 '21

woaaaaah, i'am so dumb, thnx a lot bro

2

u/your_mom_lied Jun 19 '21

You aren’t dumb. I had trouble writing paths to my assets the first time as well. I almost said something in my comment but I didn’t want to jinx you. I guess it’s common.

1

u/Birdasaur Jun 26 '21

Another way which is platform independent is to add a resources directory to your project structure. Then you can reference the image filenames relative to the package path of the class you are trying to load from. This is preferable if your images do not need to by dynamically identified at runtime... like icons, logos, backgrounds etc. If your requirements are to allow the user to load an arbitrary set of images then you will need to implement a filechooser. or drag and drop.

1

u/Birdasaur Jun 26 '21

example maven no modular project structure here https://github.com/openjfx/samples/tree/master/IDE/Eclipse/Non-Modular/Maven/hellofx/src/main

note the resources folder...

2

u/mimoguz Jun 19 '21 edited Jun 19 '21

Adding to u/your_mom_lied said, also don't forget the JavaFX docs. If you go to https://openjfx.io/javadoc/16/ and type "Image" in the search box on the upper-right, you will see javafx.scene.image.Image on the top of the results. Click that, and you find info about what it is, how to use it (with code samples), and what to use to display an image (don't forget to click on that link too). Then do same thing with BorderPane.

1

u/[deleted] Jun 24 '21

Maybe less pussy slaying and more studying..

1

u/Pussyslayerl Jun 24 '21

Unfortunately, it's just a nickname I'm still a virgin.

1

u/Birdasaur Jun 26 '21

Don't worry... JavaFX is your new harsh mistress. You will be making love to it in no time.

1

u/[deleted] Jun 24 '21

Don't say still. That's something to be proud of. Don't worry, once you get married, the tap gets turned off anyway. Sad face....