in this JavaFX project
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);
}
}
How can I make the Rectangle indicates to the program MediaView instead of the desktop screen (or) if there a way to make the robot directly indicates to the program MediaView and thanks
Do not really know your needs, but it does not make really sense to make an image in the initialize method to me.
One quick solution could be the following one :
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"));
}
}
It takes a snapshot of the VBox
containing the MediaView
cause I have no media to display, but it should also work with MediaView
as this component inherits from Node
.
As you did not provide it, this is the FXML file I used :
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.layout.BorderPane?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.layout.ColumnConstraints?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.media.MediaView?>
<?import javafx.scene.layout.VBox?>
<fx:root xmlns:fx="http://javafx.com/fxml/1" type="BorderPane">
<top>
<GridPane >
<children>
<Label GridPane.rowIndex="0" GridPane.columnIndex="0" text="Snap frame" />
<HBox GridPane.rowIndex="0" GridPane.columnIndex="1" alignment="CENTER">
<children>
<Button text="Take snap.." onAction="#handleSnap" />
</children>
</HBox>
</children>
<columnConstraints>
<ColumnConstraints percentWidth="90.0" />
<ColumnConstraints percentWidth="10.0" />
</columnConstraints>
</GridPane>
</top>
<center>
<VBox fx:id="container" style="-fx-background-color:rgb(200,200,200); -fx-border-color:DARKBLUE">
<children>
<Label text="Media Player"/>
<MediaView fx:id="mediaView"/>
</children>
</VBox>
</center>
<bottom>
<Label text="Not in the snap scope (NITSS)"/>
</bottom>
<left>
<Label text="NITSS"/>
</left>
<right>
<Label text="NITSS"/>
</right>
</fx:root>