Otar Jibladze
Otar Jibladze

Reputation: 95

Background and button issue

Having trouble running this code:

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;
import javafx.scene.control.Label;
import javafx.scene.control.Button;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.scene.text.Font;
import javafx.scene.text.Text;
import javafx.scene.Group;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.BackgroundSize;
import javafx.scene.layout.Background;
import javafx.scene.layout.BackgroundImage;
import javafx.scene.layout.BackgroundRepeat;
import javafx.scene.layout.BackgroundPosition;
import javafx.event.ActionEvent;

import java.io.FileInputStream;

public class Main extends Application  {

@Override
public void start(Stage primaryStage) throws Exception {
    BackgroundImage backgroundImage = new BackgroundImage( new Image( getClass().getResource("src/sample/Image/backg.jpg").toExternalForm()), BackgroundRepeat.NO_REPEAT, BackgroundRepeat.NO_REPEAT, BackgroundPosition.DEFAULT, BackgroundSize.DEFAULT);
    Background background = new Background(backgroundImage);

    Button button = new Button( "Click me!");
    button.setBackground(background);

    Scene scene = new Scene(backgroundImage, 600, 400);

    primaryStage.setScene(scene);
    primaryStage.show();
}



public static void main(String[] args) {
    Application.launch(args);
}

I'm trying to display button over a background but despite that I'm facing a lot of issues. I don't quite really understand how "Scene" class works so I'm feeling I made mistake there. I'm getting this error message:

Error:(38, 33) java: incompatible types: javafx.scene.layout.BackgroundImage cannot be converted to javafx.scene.Parent

38 and 33 which is:

Background background = new Background(backgroundImage);
Scene scene = new Scene(backgroundImage, 600, 400);

Thanks

UPDATE

After Matt's respond I tried his code and got this error message:

    Exception in Application start method
java.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:564)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:473)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:372)
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:564)
at java.base/sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:941)
Caused by: java.lang.RuntimeException: Exception in Application start method
at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:973)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication$2(LauncherImpl.java:198)
at java.base/java.lang.Thread.run(Thread.java:844)
Caused by: java.lang.NullPointerException
at sample.Main.start(Main.java:34)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$9(LauncherImpl.java:919)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runAndWait$11(PlatformImpl.java:449)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$9(PlatformImpl.java:418)
at java.base/java.security.AccessController.doPrivileged(Native Method)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:417)
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:175)
... 1 more
Exception running application sample.Main

Process finished with exit code 1

Upvotes: 1

Views: 363

Answers (2)

Matt
Matt

Reputation: 3187

Give this a try in javafx you want to hold all of your nodes in a container usually. Check out the comments I added

public class Main extends Application {

    public static void main(String[] args) { launch(args); }

    @Override
    public void start(Stage primaryStage) {//Removed throws Exception
        BackgroundImage backgroundImage = new BackgroundImage( 
                new Image( getClass().getResource("src/sample/Image/backg.jpg").toExternalForm()),
                BackgroundRepeat.NO_REPEAT, 
                BackgroundRepeat.NO_REPEAT, 
                BackgroundPosition.DEFAULT, 
                BackgroundSize.DEFAULT);
        Background background = new Background(backgroundImage);

        Button button = new Button( "Click me!");
        //button.setBackground(background);Not needed

        VBox vBox = new VBox();//A container which holds all the nodes
        vBox.setBackground(background);//Set the Container Background
        vBox.getChildren().add(button);//Add Nodes

        Scene scene = new Scene(vBox, 600, 400);//Parameters are Parent and width and height of your scene

        primaryStage.setScene(scene);
        primaryStage.show();
    }
}

Upvotes: 1

Anant
Anant

Reputation: 11

Hope below explaination will help you.

backgroundImage is not a parent but button is, i.e. button.setBackground(background);.

Hence to display button with its background you need to pass button as parameter in the scene constructor: Scene scene = new Scene(button, 600, 400);. If you want to display button over a background image then you need to set button over some Pane and put image you have on the background of pane, so that effectively your objective will be accomplished.

Upvotes: 1

Related Questions