2016-08-08 13 views
2

JavaFX 8のマルチメディアクラスを使用して自分のマルチメディアアプリケーションを作成しています。私は自分のプログラムを実行したときに、幅と高さを自分のStageと同じにします私がしたいメディア(ビデオ)の幅と高さ。JavaFX MediaPlayer - ステージのサイズ

mp.setOnReady(new Runnable() { 
     @Override 
     public void run() { 
      Timeline slideIn = new Timeline(); 
      Timeline slideOut = new Timeline(); 

      stage.getScene().setOnMouseEntered(e -> { 
       slideIn.play(); 
      }); 

      stage.getScene().setOnMouseExited(e -> { 
       slideOut.play(); 
      }); 
      // here I get the width and heigth of the media 
      int w = mp.getMedia().getWidth(); 
      int h = mp.getMedia().getHeight(); 
      // width is 1280 and heigth is 720 (both values are correct) 
      if (w != 0 && h != 0) { 
       //Here Im setting the width and the height of stage 
       //But when I run my app, stage is a little bit smaller than 
       //MediaView and I have to resize the stage manually by cca 20px to get the whole view 
       stage.setWidth(w); 
       stage.setHeight(h); 
       stage.centerOnScreen(); 

       //On the other hand, animation works pretty well and 
       //correctly with the "w" and "h" 
       mediaBar.setMinSize(w - 100, 100); 
       mediaBar.setTranslateY(h - 100); 
       mediaBar.setTranslateX(50); 
       mediaBar.setOpacity(0); 

       slideIn.getKeyFrames().addAll(new KeyFrame(new Duration(0), new KeyValue(mediaBar.translateYProperty(), h), new KeyValue(mediaBar.opacityProperty(), 0.0)), 
         new KeyFrame(new Duration(300), new KeyValue(mediaBar.translateYProperty(), h - 100), new KeyValue(mediaBar.opacityProperty(), 0.9)) 
       ); 

       slideOut.getKeyFrames().addAll(new KeyFrame(new Duration(0), new KeyValue(mediaBar.translateYProperty(), h - 100), new KeyValue(mediaBar.opacityProperty(), 0.9)), 
         new KeyFrame(new Duration(300), new KeyValue(mediaBar.translateYProperty(), h), new KeyValue(mediaBar.opacityProperty(), 0.0)) 
       ); 

       background.setWidth(w - 100); 
       background.setHeight(100); 
       background.setTranslateY(h - 100); 
       background.setTranslateX(50); 
       background.setOpacity(0); 

       slideIn.getKeyFrames().addAll(new KeyFrame(new Duration(0), new KeyValue(background.translateYProperty(), h), new KeyValue(background.opacityProperty(), 0.0)), 
         new KeyFrame(new Duration(300), new KeyValue(background.translateYProperty(), h - 100), new KeyValue(background.opacityProperty(), 1)) 
       ); 

       slideOut.getKeyFrames().addAll(new KeyFrame(new Duration(0), new KeyValue(background.translateYProperty(), h - 100), new KeyValue(background.opacityProperty(), 1)), 
         new KeyFrame(new Duration(300), new KeyValue(background.translateYProperty(), h), new KeyValue(background.opacityProperty(), 0.0)) 
       ); 
      } else { 
       //music 
      } 
      duration = mp.getMedia().getDuration(); 
      updateValues(); 
     } 
    }); 

私はまだわからないが、Stageクラスについて重要なことがあると思う。

initStyleStageからStageStyle.UNDECORATEDに設定すると、MediaViewの全体が表示されます。しかし、私はこのスタイルを望んでいません。

誰でもこの問題を解決するのに役立つでしょうか?これは私の最初のヘルプのための質問であるため、私は私の問題を正しく記述することを願っています。ありがとう。

答えて

0

これは、ステージの高さと幅に装飾が含まれているためです。 javadoc of widthPropertyから:

この値は、サイズ変更可能なフレームハンドルとして オペレーティングシステムによって添加することができる任意のおよびすべての装飾を含みます。通常のアプリケーションの では、代わりにシーンの幅が設定されます。

解決策はドキュメントにも記載されています。代わりにSceneのサイズを設定できます。

この例では、サイズが400x400のSceneが埋め込まれたStageが開きます。あなたがSceneStageの大きさが原因で装飾の大きな可能に表示されます設定されている場合

public class Main extends Application { 
    @Override 
    public void start(Stage primaryStage) { 
     try { 
      BorderPane root = new BorderPane(); 
      Scene scene = new Scene(root,400,400); 
      primaryStage.setTitle("Stage with 400x400 Scene"); 
      primaryStage.setScene(scene); 

      primaryStage.setOnShown(event -> System.out.println("Size of the Stage is " + primaryStage.getHeight() + "x" + primaryStage.getWidth())); 

      primaryStage.show(); 
     } catch(Exception e) { 
      e.printStackTrace(); 
     } 
    } 

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

出力は、

Size of the Stage is 438.0x416.0 

だから、あなたが見ることができるようです。

更新:ちょうど私がここで本当に似たような答えを見つけました:JavaFX: Set Scene min size excluding decorations

関連する問題