예제 #1
0
  public final void addDragListeners(final Node n) {

    n.setOnMousePressed(
        new EventHandler<MouseEvent>() {

          @Override
          public void handle(MouseEvent me) {

            startDragX = me.getSceneX();
            startDragY = me.getSceneY();
            root.setStyle("-fx-opacity:.7;");
          }
        });
    n.setOnMouseReleased(
        new EventHandler<MouseEvent>() {

          @Override
          public void handle(MouseEvent me) {

            root.setStyle("-fx-opacity:1;");
          }
        });
    n.setOnMouseDragged(
        new EventHandler<MouseEvent>() {

          @Override
          public void handle(MouseEvent me) {

            stage.setX(me.getScreenX() - startDragX);
            stage.setY(me.getScreenY() - startDragY);
          }
        });
  }
예제 #2
0
  @Override
  public void start(final Stage primaryStage) {
    primaryStage.setTitle("Chapter 3-2 Playing Video");
    primaryStage.centerOnScreen();
    primaryStage.initStyle(StageStyle.TRANSPARENT);

    final Group root = new Group();
    final Scene scene = new Scene(root, 540, 300, Color.rgb(0, 0, 0, 0));

    // rounded rectangle with slightly transparent
    Node applicationArea = createBackground(scene);
    root.getChildren().add(applicationArea);

    // allow the user to drag window on the desktop
    attachMouseEvents(scene, primaryStage);

    // allows the user to see the progress of the video playing
    progressSlider = createSlider(scene);
    root.getChildren().add(progressSlider);

    // Dragging over surface
    scene.setOnDragOver(
        new EventHandler<DragEvent>() {
          @Override
          public void handle(DragEvent event) {
            Dragboard db = event.getDragboard();
            if (db.hasFiles() || db.hasUrl() || db.hasString()) {
              event.acceptTransferModes(TransferMode.COPY);
              if (mediaPlayer != null) {
                mediaPlayer.stop();
              }
            } else {
              event.consume();
            }
          }
        });

    // update slider as video is progressing (later removal)
    progressListener =
        new ChangeListener<Duration>() {
          public void changed(
              ObservableValue<? extends Duration> observable,
              Duration oldValue,
              Duration newValue) {
            progressSlider.setValue(newValue.toSeconds());
          }
        };

    // Dropping over surface
    scene.setOnDragDropped(
        new EventHandler<DragEvent>() {

          @Override
          public void handle(DragEvent event) {
            Dragboard db = event.getDragboard();
            boolean success = false;
            URI resourceUrlOrFile = null;

            // dragged from web browser address line?
            if (db.hasContent(DataFormat.URL)) {
              try {
                resourceUrlOrFile = new URI(db.getUrl().toString());
              } catch (URISyntaxException ex) {
                ex.printStackTrace();
              }
            } else if (db.hasFiles()) {
              // dragged from the file system
              String filePath = null;
              for (File file : db.getFiles()) {
                filePath = file.getAbsolutePath();
              }
              resourceUrlOrFile = new File(filePath).toURI();
              success = true;
            }
            // load media
            Media media = new Media(resourceUrlOrFile.toString());

            // stop previous media player and clean up
            if (mediaPlayer != null) {
              mediaPlayer.stop();
              mediaPlayer.currentTimeProperty().removeListener(progressListener);
              mediaPlayer.setOnPaused(null);
              mediaPlayer.setOnPlaying(null);
              mediaPlayer.setOnReady(null);
            }

            // create a new media player
            mediaPlayer = MediaPlayerBuilder.create().media(media).build();

            // as the media is playing move the slider for progress
            mediaPlayer.currentTimeProperty().addListener(progressListener);

            // play video when ready status
            mediaPlayer.setOnReady(
                new Runnable() {
                  @Override
                  public void run() {
                    progressSlider.setValue(1);
                    progressSlider.setMax(mediaPlayer.getMedia().getDuration().toMillis() / 1000);
                    mediaPlayer.play();
                  }
                });

            // Lazy init media viewer
            if (mediaView == null) {
              mediaView =
                  MediaViewBuilder.create()
                      .mediaPlayer(mediaPlayer)
                      .x(4)
                      .y(4)
                      .preserveRatio(true)
                      .opacity(.85)
                      .smooth(true)
                      .build();

              mediaView.fitWidthProperty().bind(scene.widthProperty().subtract(220));
              mediaView.fitHeightProperty().bind(scene.heightProperty().subtract(30));

              // make media view as the second node on the scene.
              root.getChildren().add(1, mediaView);
            }

            // sometimes loading errors occur
            mediaView.setOnError(
                new EventHandler<MediaErrorEvent>() {
                  public void handle(MediaErrorEvent event) {
                    event.getMediaError().printStackTrace();
                  }
                });

            mediaView.setMediaPlayer(mediaPlayer);

            event.setDropCompleted(success);
            event.consume();
          }
        });

    // rectangular area holding buttons
    final Group buttonArea = createButtonArea(scene);

    // stop button will stop and rewind the media
    Node stopButton = createStopControl();

    // play button can resume or start a media
    final Node playButton = createPlayControl();

    // pauses media play
    final Node pauseButton = createPauseControl();

    stopButton.setOnMousePressed(
        new EventHandler<MouseEvent>() {
          public void handle(MouseEvent me) {
            if (mediaPlayer != null) {
              buttonArea.getChildren().removeAll(pauseButton, playButton);
              buttonArea.getChildren().add(playButton);
              mediaPlayer.stop();
            }
          }
        });
    // pause media and swap button with play button
    pauseButton.setOnMousePressed(
        new EventHandler<MouseEvent>() {
          public void handle(MouseEvent me) {
            if (mediaPlayer != null) {
              buttonArea.getChildren().removeAll(pauseButton, playButton);
              buttonArea.getChildren().add(playButton);
              mediaPlayer.pause();
              paused = true;
            }
          }
        });

    // play media and swap button with pause button
    playButton.setOnMousePressed(
        new EventHandler<MouseEvent>() {
          public void handle(MouseEvent me) {
            if (mediaPlayer != null) {
              buttonArea.getChildren().removeAll(pauseButton, playButton);
              buttonArea.getChildren().add(pauseButton);
              paused = false;
              mediaPlayer.play();
            }
          }
        });

    // add stop button to button area
    buttonArea.getChildren().add(stopButton);

    // set pause button as default
    buttonArea.getChildren().add(pauseButton);

    // add buttons
    root.getChildren().add(buttonArea);

    // create a close button
    Node closeButton = createCloseButton(scene);
    root.getChildren().add(closeButton);

    primaryStage.setOnShown(
        new EventHandler<WindowEvent>() {
          public void handle(WindowEvent we) {
            previousLocation = new Point2D(primaryStage.getX(), primaryStage.getY());
          }
        });

    primaryStage.setScene(scene);
    primaryStage.show();
  }