矩形表示特定的媒体播放器javafx

时间:2018-04-10 14:50:24

标签: javafx screenshot

在这个JavaFX项目中

public class FXMLDocumentController implements Initializable {

    @FXML
    private MediaView media_view;

    private Media me;
    private MediaPlayer mp;

    @Override
    public void initialize(URL url, ResourceBundle rb) 
    {
        String s="/home/mustafa987/Videos/sampels/video10.mp4";
        me=new Media(new File(s).toURI().toString());
        mp=new MediaPlayer(me);
        media_view.setMediaPlayer(mp);
        mp.play();
        mp.setRate(1);

        Robot robot=new Robot();

        Rectangle rectangle=new Rectangle(Toolkit.getDefaultToolkit().getScreenSize());
        //in the line above the Rectangle indicate to the desktop screen so the robot can capture the screenshot  

        BufferedImage buffer=robot.createScreenCapture(rectangle);
        //take screenshot
        Image image=SwingFXUtils.toFXImage(buffer, null);
    }
}

如果有办法让机器人直接向程序MediaView指示,如何让Rectangle指示程序MediaView而不是桌面屏幕(或) 并谢谢

1 个答案:

答案 0 :(得分:0)

真的不知道你的需求,但在初始化方法中为我制作一个图像并不合理。

一个快速解决方案可能是以下一个:

public class FXMLDocumentController extends BorderPane {

   @FXML
   private MediaView mediaView;

   @FXML
   private VBox container;

   private Media me;

   private MediaPlayer mp;

   /**
    * Constructor.
    */
   public FXMLDocumentController() {
      FXMLLoader loader = new FXMLLoader(getClass().getResource("Media.fxml"));
      loader.setRoot(this);
      loader.setController(this);

      try {
         loader.load();
      } catch(IOException e) {
         System.out.println("An error occurs while trying to load the media component." + e);
      }
   }

   @FXML
   private void initialize() {
      // String s = "/home/mustafa987/Videos/sampels/video10.mp4";
      // me = new Media(new File(s).toURI().toString());
      // mp = new MediaPlayer(me);
      // mediaView.setMediaPlayer(mp);
      // mp.play();
      // mp.setRate(1);
   }

   @FXML
   private void handleSnap() throws IOException {
      System.out.println("User asks snap.");
      WritableImage img = container.snapshot(new SnapshotParameters(), null);
      BufferedImage bufImg = SwingFXUtils.fromFXImage(img, null);
      ImageIO.write(bufImg, "png", new File("./save/snap.png"));
   }
}

它包含VBox的{​​{1}}快照,因为我没有要显示的媒体,但它也应该与MediaView一起使用,因为此组件继承自MediaView

由于您没有提供,这是我使用的FXML文件:

Node