Java Reference
In-Depth Information
// dragging the entire stage
scene.setOnMouseDragged((MouseEvent event) -> {
if (anchorPt != null && previousLocation != null) {
primaryStage.setX(previousLocation.getX()
+ event.getScreenX() - anchorPt.getX());
primaryStage.setY(previousLocation.getY()
+ event.getScreenY() - anchorPt.getY());
}
});
You will want to handle the mouse-release event. Once the mouse is released, the
event handler will update the previousLocation variable for subsequent mouse-
drag events to move the application window about the screen. The following code snip-
pet updates the previousLocation variable:
// set the current location
scene.setOnMouseReleased((MouseEvent event) -> {
previousLocation = new Point2D(primaryStage.getX(),
primaryStage.getY());
});
Next, you will be implementing the drag-and-drop scenario to load the audio file
from the file system (using the File Manager). When handling a drag-and-drop scen-
ario, it is similar to Recipe 15-1, in which you created an EventHandler to handle
DragEvent s. Instead of loading image files, you'll be loading audio files from the
host file system. For brevity, I simply mention the code lines of the drag-and-dropped
event handler. Once the audio file is available, you will create a Media object by
passing in the file as a URI . The following code snippet is how to create a Media ob-
ject:
Media media = new Media(new
File(filePath).toURI().toString());
Once you have created a Media object you will have to create an instance of a
MediaPlayer in order to play the sound file. Both the Media and MediaPlayer
objects are immutable, which is why new instances of each will be created every time
the user drags a file into the application. Next, you will check the instance variable
mediaPlayer for a previous instance to make sure it is stopped before creating a
Search WWH ::




Custom Search