Results 1 to 10 of 10
  1. #1
    Deathslice is offline Member
    Join Date
    Jan 2015
    Location
    Miami, FL
    Posts
    86
    Rep Power
    0

    Default Can't load an image to he background with Javafx

    Revelant Code
    Java FX Code:
    package whowantstobeamillionairetriviagame;
    
    import javafx.application.Application;
    import javafx.scene.Scene;
    import javafx.scene.layout.StackPane;
    import javafx.stage.Stage;
    
    public class WhoWantsToBeAMillionaireTriviaGame extends Application 
    {   
        @Override
        public void start(Stage startingStage) 
        {
            StackPane root = new StackPane();
            root.setId("MillionaireLogo");
            
            Scene menuScene = new Scene(root, 1366, 768);
            menuScene.getStylesheets().addAll(this.getClass().getResource("BackgroundImage.css").toExternalForm());
            startingStage.setScene(menuScene);
            startingStage.show();
        }
    
        public static void main(String[] args) 
        {
            launch(args);
        }
    }
    CSS File
    Java FX Code:
    #MillionaireLogo{
        -fx-background-image: url("LOGO9.png");
        -fx-background-size: 220 220;
        -fx-background-position: center, center;
        -fx-effect: dropshadow(three-pass-box, black, 30, 0.5, 0, 0);
    }
    Error that I'm getting

    Exception in Application start method
    java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Nativ e Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Native MethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(De legatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at com.sun.javafx.application.LauncherImpl.launchAppl icationWithArgs(LauncherImpl.java:389)
    at com.sun.javafx.application.LauncherImpl.launchAppl ication(LauncherImpl.java:328)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Nativ e Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Native MethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(De legatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at sun.launcher.LauncherHelper$FXHelper.main(Launcher Helper.java:767)
    Caused by: java.lang.RuntimeException: Exception in Application start method
    at com.sun.javafx.application.LauncherImpl.launchAppl ication1(LauncherImpl.java:917)
    at com.sun.javafx.application.LauncherImpl.lambda$lau nchApplication$152(LauncherImpl.java:182)
    at com.sun.javafx.application.LauncherImpl$$Lambda$50/1642360923.run(Unknown Source)
    at java.lang.Thread.run(Thread.java:745)
    Caused by: java.lang.NullPointerException
    at whowantstobeamillionairetriviagame.WhoWantsToBeAMi llionaireTriviaGame.start(WhoWantsToBeAMillionaire TriviaGame.java:17)
    at com.sun.javafx.application.LauncherImpl.lambda$lau nchApplication1$159(LauncherImpl.java:863)
    at com.sun.javafx.application.LauncherImpl$$Lambda$53/424011618.run(Unknown Source)
    at com.sun.javafx.application.PlatformImpl.lambda$run AndWait$172(PlatformImpl.java:326)
    at com.sun.javafx.application.PlatformImpl$$Lambda$46/355629945.run(Unknown Source)
    at com.sun.javafx.application.PlatformImpl.lambda$nul l$170(PlatformImpl.java:295)
    at com.sun.javafx.application.PlatformImpl$$Lambda$48/327580968.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl.lambda$run Later$171(PlatformImpl.java:294)
    at com.sun.javafx.application.PlatformImpl$$Lambda$47/1915503092.run(Unknown Source)
    at com.sun.glass.ui.InvokeLaterDispatcher$Future.run( InvokeLaterDispatcher.java:95)
    at com.sun.glass.ui.win.WinApplication._runLoop(Nativ e Method)
    at com.sun.glass.ui.win.WinApplication.lambda$null$14 5(WinApplication.java:101)
    at com.sun.glass.ui.win.WinApplication$$Lambda$36/1963387170.run(Unknown Source)
    ... 1 more
    Exception running application whowantstobeamillionairetriviagame.WhoWantsToBeAMi llionaireTriviaGame
    Java Result: 1

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    12,059
    Rep Power
    25

    Default Re: Can't load an image to he background with Javafx

    Moved from New to Java to JavaFX.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    Deathslice is offline Member
    Join Date
    Jan 2015
    Location
    Miami, FL
    Posts
    86
    Rep Power
    0

    Default Re: Can't load an image to he background with Javafx

    Could it be that I'm missing a library in my program? If so, what is it because I can't find it.
    Last edited by Deathslice; 04-04-2015 at 03:15 PM.

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    19,965
    Rep Power
    31

    Default Re: Can't load an image to he background with Javafx

    Caused by: java.lang.NullPointerException
    at whowantstobeamillionairetriviagame.WhoWantsToBeAMi llionaireTriviaGame.start(WhoWantsToBeAMillionaire TriviaGame.java:17)
    What value is null on line 17?
    If you can not tell, Break the statement up into simple statements and print their values.
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    Deathslice is offline Member
    Join Date
    Jan 2015
    Location
    Miami, FL
    Posts
    86
    Rep Power
    0

    Default Re: Can't load an image to he background with Javafx

    Well if edit line 17 to this menuScene.getStylesheets().add("C:/Users/Owner/Documents/NetBeansProjects/WhoWantsToBeAMillionaireTriviaGame/BackgroundImage.css");
    the program runs but the image doesn't show.
    Last edited by Deathslice; 04-04-2015 at 04:10 PM.

  6. #6
    Deathslice is offline Member
    Join Date
    Jan 2015
    Location
    Miami, FL
    Posts
    86
    Rep Power
    0

    Default Re: Can't load an image to he background with Javafx

    Well I've found in the javadoc that getresources returns a url or null if it doesn't exist. Since I am getting a nullpointerexception on line 17, the program thinks that the backgroundImage.css doesn't exist in my project folder.

  7. #7
    Deathslice is offline Member
    Join Date
    Jan 2015
    Location
    Miami, FL
    Posts
    86
    Rep Power
    0

    Default Re: Can't load an image to he background with Javafx

    Alright now Where we are getting somewhere. Here is the solution: Notice that I'm saying getClass(). This means that I have to go in my project folder then build, then class, then go in the folder that is the name of my class and put my BackgroundImage.css in there. That is what I did and it fixed all of my errors but now the image doesn't show so that is another problem that I have to fix.
    Last edited by Deathslice; 04-04-2015 at 04:59 PM.

  8. #8
    Deathslice is offline Member
    Join Date
    Jan 2015
    Location
    Miami, FL
    Posts
    86
    Rep Power
    0

    Default Re: Can't load an image to he background with Javafx

    Alright now I got the image to show. Solution: Make sure the id you set has no spaces. Works:MillionaireLogo; doesn't work: Millionaire Logo. Now there is a new problem. The picture is showing multiple times and I don't know why(I'll figure it out sooner or later).
    Last edited by Deathslice; 04-04-2015 at 04:59 PM.

  9. #9
    Deathslice is offline Member
    Join Date
    Jan 2015
    Location
    Miami, FL
    Posts
    86
    Rep Power
    0

    Default Re: Can't load an image to he background with Javafx

    Alright now I'm getting another error
    Apr 04, 2015 11:14:22 AM com.sun.javafx.css.StyleManager getCachedImage
    WARNING: Error loading image: jar:file:/C:/Users/Owner/Documents/NetBeansProjects/WhoWantsToBeAMillionaireTriviaGame/dist/run1717697751/WhoWantsToBeAMillionaireTriviaGame.jar!/whowantstobeamillionairetriviagame/LOGO9.png

    Revelant code
    Java FX Code:
    package whowantstobeamillionairetriviagame;
    
    import javafx.application.Application;
    import javafx.scene.Scene;
    import javafx.scene.layout.StackPane;
    import javafx.stage.Stage;
    
    public class WhoWantsToBeAMillionaireTriviaGame extends Application 
    {   
        @Override
        public void start(Stage startingStage) throws Exception
        {
            StackPane root = new StackPane();
            root.setId("MillionaireLogo");
            
            Scene menuScene = new Scene(root);
            menuScene.getStylesheets().add(this.getClass().getResource("BackgroundImage.css").toExternalForm());
            startingStage.setScene(menuScene);
            startingStage.show();
        }
    
        public static void main(String[] args) 
        {
            launch(args);
        }
    }
    BackgroundImage css File
    Java FX Code:
    #MillionaireLogo
    {
        -fx-background-image: url("LOGO9.png");
        -fx-background-size: 640 480;
    }
    Notice that I have shortened the css File because I want to see how it would look before I make any other changes.
    Besides that everything is exactly the same.

  10. #10
    Deathslice is offline Member
    Join Date
    Jan 2015
    Location
    Miami, FL
    Posts
    86
    Rep Power
    0

    Default Re: Can't load an image to he background with Javafx

    Alright I figured it out. I changed the url to this http://www.shescribes.com/wp-content...naire_logo.png and it processed the image(It's was alot of fun trying to figure this out). That takes care of the first step in my project. The next step is to create 3 buttons aligned at the bottom center of the image. Which layout would good for this? VBox or gridpane?

Similar Threads

  1. JTabbed pane: How to load in background?
    By Paul279 in forum AWT / Swing
    Replies: 9
    Last Post: 04-24-2014, 03:04 PM
  2. Background Image
    By Flopet17 in forum Advanced Java
    Replies: 5
    Last Post: 03-07-2014, 08:24 PM
  3. JavaFX Book | Learn JavaFX More In Depth
    By SnakeDoc in forum JavaFX
    Replies: 0
    Last Post: 05-17-2013, 11:10 PM
  4. Background image
    By leiferouis in forum New To Java
    Replies: 9
    Last Post: 03-08-2009, 05:49 PM
  5. Image as background
    By Java.child in forum AWT / Swing
    Replies: 2
    Last Post: 10-02-2008, 11:37 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •