2017-11-24 26 views
1

初心者からjavafxになりました。現在、自分の画像が自分のバックグラウンドになることはできません。ここにコードがあります。どんな助けもありがたい。背景画像が動作していませんjavaFX

package game; 
import javafx.application.Application; 
import javafx.scene.Scene; 
import javafx.scene.control.Button; 
import javafx.scene.layout.StackPane; 
import javafx.stage.Stage; 
import javafx.scene.shape.Circle; 
import javafx.scene.paint.Color; 
import javafx.scene.layout.Pane; 
import javafx.scene.layout.BackgroundImage; 
import javafx.scene.layout.BackgroundSize; 
import javafx.scene.layout.Background; 
import javafx.scene.image.Image; 
import javafx.scene.image.ImageView; 
import javafx.scene.layout.HBox; 
import javafx.scene.layout.BackgroundRepeat; 
import javafx.scene.layout.BackgroundPosition; 
public class appgame extends Application { 

    Button button; 

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

    @Override 
    public void start(Stage primaryStage) throws Exception { 
     primaryStage.setTitle("Title of the Window"); 
     Pane p = new HBox(); 
     p.setPadding(new javafx.geometry.Insets(5,5,5,5)); 
     Image image = new Image("file:/home/rex/Documents/codes/java/bg1.jpg"); 

BackgroundImage backgroundImage = new BackgroundImage(image,BackgroundRepeat.REPEAT, BackgroundRepeat.NO_REPEAT, BackgroundPosition.CENTER, backgroundSize); 

Background background = new Background(backgroundImage); 

     Scene scene = new Scene(p, 306, 460); 

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

自分の背景を自分のシーンに接続できません。自分の画像を自分のシーンの背景として設定してください。現在のところ、コードは背景画像なしの空白のステージのみを表示します。 ありがとうございます。

答えて

1

インスタンス化されたペイン(p)に背景を追加するのを忘れました。

package appgame; 

import javafx.application.Application; 
import javafx.scene.Scene; 
import javafx.scene.image.Image; 
import javafx.scene.layout.*; 
import javafx.stage.Stage; 

import static javafx.scene.layout.BackgroundPosition.CENTER; 
import static javafx.scene.layout.BackgroundRepeat.NO_REPEAT; 
import static javafx.scene.layout.BackgroundRepeat.REPEAT; 
import static javafx.scene.layout.BackgroundSize.*; 

public class AppGame extends Application { 

    private static final String BACKGROUND_PATH = "<path to background>"; 

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

    @Override 
    public void start(Stage primaryStage) throws Exception { 
     Pane p = new HBox(); 
     p.setPadding(new javafx.geometry.Insets(5,5,5,5)); 
     //Set your background! 
     p.setBackground(new Background(new BackgroundImage(new Image(BACKGROUND_PATH), REPEAT, NO_REPEAT, CENTER, DEFAULT))); 

     primaryStage.setTitle("Title of the Window"); 
     primaryStage.setScene(new Scene(p, 306, 460)); 
     primaryStage.show(); 
    } 
} 

これは、(私のファイルシステム上のPNGを使用して)、その結果:あなたはまた、JavaFXのを学ぶために私にいくつかのリソースを与えることができれば

enter image description here

+0

おかげ@skubski は、私は本当に感謝、すべてのリソースI悪臭があります。 ありがとうございます –

+0

個人的には、[はじめに](https://docs.oracle.com/javafx/2/get_started/jfxpub-get_started.htm)を読んでいます。[Mastering-JavaFX] OracleのWebサイト(https://www.amazon.com/Mastering-JavaFX-Controls-Oracle-Press/dp/0071833773/ref=pd_lpo_sbs_14_img_0?_encoding=UTF8&psc=1&refRID=WEYJ9N4TBTRB4VRJR68N)を参照してください。しかし完璧にするのは実践です。 ;) – skubski

+0

私がしようとしているのは、パラシュートパニックと呼ばれるゲームを作ることです。オラクルのドキュメントには、JavaFXのアニメーション部分が含まれていますか?これらすべての質問には申し訳ありません。 ありがとうございます。 –

関連する問題