PeGiannOS
PeGiannOS

Reputation: 247

JavaFx Video Dimension OUT of screen

I recently found javafx 2.1 very useful for my project of making a video player but after a success I faced a problem with the video size Dimensions. In other words, when I run the program and video is playing normally I can't see the whole video because it's dimensions are bigger than my screen resolution .What Can I do in the following code to resize the actual size of video in windows7 64bit:

public class HelloFx extends Application {

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

    @Override
    public void start(final Stage stage) throws Exception {
         stage.setTitle("Movie Player");
         final BorderPane root = new BorderPane();

         final Media media = new Media("file:///Users//user//Videos//Sintel.mp4");
         final MediaPlayer player = new MediaPlayer(media);
         final MediaView view = new MediaView(player);

         // System.out.println("media.width: "+media.getWidth());
         root.getChildren().add(view);

         final Scene scene = new Scene(root, 400, 400, Color.BLACK);


         stage.setScene(scene);
         stage.show();
         player.play();
         player.setOnReady(new Runnable() {
             @Override
             public void run() {
                 int w = player.getMedia().getWidth();
                 int h = player.getMedia().getHeight();

                 stage.setMinWidth(w);
                 stage.setMinHeight(h);


             }
         });
          //player.play();

    }
}

Upvotes: 4

Views: 6107

Answers (1)

Adrian
Adrian

Reputation: 106

The JavaFX 2 MediaView class has 2 functions which can help. They are .setFitHeight() and .setFitWidth() .

So, you could, instead of letting the media dictate the size of screen, let your stage set the size of the screen...

 public void run() {
                 int w = stage.getWidth(); // player.getMedia().getWidth();
                 int h = stage.getHeight(); // player.getMedia().getHeight();

                 // stage.setMinWidth(w);
                 // stage.setMinHeight(h);
                 // make the video conform to the size of the stage now...
                 player.setFitWidth(w);
                 player.setFitHeight(h);


             }

Then the video should fit inside of the stage. That above code is pretty crude, and you may want to "Scale" the video better, ie: find the ratio of the media width VS the stage width & media height VS stage height ... But that code above should get you started.

Upvotes: 9

Related Questions