2

我试图了解 JavaFX 的东西......我的程序是一个基于 WindowBuilder 的 Gui,我想要一个 JavaFX 图形,以及一个 JavaFX 实时视频源显示在我的应用程序中。

如何在我的代码中实现它?我试过这个,但我无法让它运行。数据馈送不是问题。我只需要在我的 JFrame 中以小方块的形式查看它...

现在很困惑:(

这是我的代码:(对不起,它有点长,但我责怪JavaFX的示例代码:p

import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.UIManager;
import java.awt.Canvas;
import java.awt.SystemColor;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import javax.swing.UIManager.*;

/**
 * @author 
 * 
 */

public class MyClientApp extends JFrame {

/**
*
*/
private static final long serialVersionUID = 1L;
protected static final String BufferedWriter = null;
JFrame frame;
private JTextField textFieldUsername;

/**
 * Create the application.
 */
public MyClientApp(BufferedWriter serverDataOut, BufferedReader serverDataIn) {

    initialize();
}

/**
 * Initialize the contents of the frame.
 * 
 * @param serverDataOut
 * 
 */

private void initialize() {

    try {
        for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
            if ("Nimbus".equals(info.getName())) {
                UIManager.setLookAndFeel(info.getClassName());
                break;
            }
        }
    } catch (Exception e) {
        // Nimbus Theme not avaliable
}

    frame = new JFrame();
        frame.setResizable(false);
    frame.setTitle("*********** My Program ***********");
    frame.setBounds(320, 130, 730, 570); 
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.getContentPane().setLayout(null);

    Canvas canvasTemp = new Canvas();
    canvasTemp.setBackground(SystemColor.window);
    canvasTemp.setBounds(6, 277, 380, 255);
    frame.getContentPane().add(canvasTemp);

    Canvas canvasLevel = new Canvas();
    canvasLevel.setBackground(SystemColor.window);
    canvasLevel.setBounds(6, 10, 380, 255);
    frame.getContentPane().add(canvasLevel);

}

public JFrame frame() {
    return frame;
}


}

这是让它为你们运行的主文件......:

Client.java

import java.io.IOException;


public class Client {

public static void main(String[] args) throws IOException, InterruptedException {

    MyClientApp window = new MyClientApp(null, null);

    window.frame.setVisible(true);

}

}

我要实现的代码是:AdvancedLineChartSample.java(来自JavaFX)

/**
* Copyright (c) 2008, 2012 Oracle and/or its affiliates.
* All rights reserved. Use is subject to license terms.
*/


import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.stage.Stage;
import java.util.ArrayList;
import java.util.List;
import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.collections.FXCollections; 
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Side;
import javafx.scene.Node;
import javafx.scene.chart.LineChart;
import javafx.scene.chart.NumberAxis;
import javafx.scene.chart.XYChart;
import javafx.scene.chart.XYChart.Series;
import javafx.util.Duration;

/**
 * An advanced line chart with a variety of actions and settable properties.
 *
 * @see javafx.scene.chart.LineChart
 * @see javafx.scene.chart.Chart
 * @see javafx.scene.chart.NumberAxis
 * @see javafx.scene.chart.XYChart
 */
public class AdvancedLineChartSample extends Application {

private void init(Stage primaryStage) {
    Group root = new Group();
    primaryStage.setScene(new Scene(root));
    root.getChildren().add(createChart());
}

protected LineChart<Number, Number> createChart() {
    final NumberAxis xAxis = new NumberAxis();
    final NumberAxis yAxis = new NumberAxis();
    final LineChart<Number,Number> lc = new LineChart<Number,Number>(xAxis,yAxis);
    // setup chart
    lc.setTitle("Temp Chart");
    xAxis.setLabel("tid");
    yAxis.setLabel("temp");
    // add starting data
    XYChart.Series<Number,Number> series = new XYChart.Series<Number,Number>();
    series.setName("Dataset 1");
    series.getData().add(new XYChart.Data<Number,Number>(20d, 50d));
    series.getData().add(new XYChart.Data<Number,Number>(40d, 80d));
    series.getData().add(new XYChart.Data<Number,Number>(50d, 90d));
    series.getData().add(new XYChart.Data<Number,Number>(70d, 30d));
    series.getData().add(new XYChart.Data<Number,Number>(170d, 122d));
    lc.getData().add(series);
    return lc;
}

@Override public void start(Stage primaryStage) throws Exception {
    init(primaryStage);
    primaryStage.show();
}
public static void main(String[] args) { launch(args); 

}

}

和 StreamingMediaPlayer.java(来自 JavaFX):

/**

* Copyright (c) 2008, 2012 Oracle and/or its affiliates.
* All rights reserved. Use is subject to license terms.
*/
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.stage.Stage;
import javafx.application.Platform;
import javafx.beans.InvalidationListener;
import javafx.beans.Observable;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.control.Label;
import javafx.scene.control.Slider;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Pane;
import javafx.scene.layout.Priority;
import javafx.scene.layout.Region;
import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;
import javafx.scene.media.MediaPlayer.Status;
import javafx.scene.media.MediaView;
import javafx.util.Duration;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.animation.ParallelTransition;
import javafx.scene.control.*;
import javafx.scene.layout.*;
import javafx.scene.paint.Color;

/**
* A media player with controls for play, pause, stop, seek, and volume. This media player is playing media via HTTP Live Streaming, also known as HLS. 
*
* @see javafx.scene.media.MediaPlayer
* @see javafx.scene.media.Media
*/
public class StreamingMediaPlayer extends Application {
private static final String MEDIA_URL = "http://download.oracle.com/otndocs/products/javafx/JavaRap/prog_index.m3u8";

private MediaPlayer mediaPlayer;

private void init(Stage primaryStage) {
    Group root = new Group();
    primaryStage.setScene(new Scene(root));
    mediaPlayer = new MediaPlayer(new Media(MEDIA_URL));
    mediaPlayer.setAutoPlay(true);
    PlayerPane playerPane = new PlayerPane(mediaPlayer);
    playerPane.setMinSize(480, 360);  
    playerPane.setPrefSize(480, 360);
    playerPane.setMaxSize(480, 360);
   // getStylesheets().add("ensemble/samples/media/OverlayMediaPlayer.css");
    root.getChildren().add(playerPane);
}

public void play() {
    Status status = mediaPlayer.getStatus();
    if (status == Status.UNKNOWN || status == Status.HALTED) {
        return;
    }
    if (status == Status.PAUSED || status == Status.STOPPED || status == Status.READY)     {
        mediaPlayer.play();
    }
}

@Override public void stop() {
    mediaPlayer.stop();
}

static class PlayerPane extends BorderPane {
    private MediaPlayer mp;
    private MediaView mediaView;
    private final boolean repeat = false;
    private boolean stopRequested = false;
    private boolean atEndOfMedia = false;
    private Duration duration;
    private Slider timeSlider;
    private Label playTime;
    private Slider volumeSlider;
    private HBox mediaTopBar;
    private HBox mediaBottomBar;
    private ParallelTransition transition = null;

    @Override protected void layoutChildren() {
        if (mediaView != null && getBottom() != null) {
            mediaView.setFitWidth(getWidth());
            mediaView.setFitHeight(getHeight() - getBottom().prefHeight(-1));
        }
        super.layoutChildren();
        if (mediaView != null) {
            mediaView.setTranslateX((((Pane)getCenter()).getWidth() - mediaView.prefWidth(-1)) / 2);
            mediaView.setTranslateY((((Pane)getCenter()).getHeight() - mediaView.prefHeight(-1)) / 2);
        }
    }

    @Override protected double computeMinWidth(double height) {
        return mediaBottomBar.prefWidth(-1);
    }

    @Override protected double computeMinHeight(double width) {
        return 200;
    }

    @Override protected double computePrefWidth(double height) {
        return Math.max(mp.getMedia().getWidth(), mediaBottomBar.prefWidth(height));
    }

    @Override protected double computePrefHeight(double width) {
        return mp.getMedia().getHeight() + mediaBottomBar.prefHeight(width);
    }

    @Override protected double computeMaxWidth(double height) { return Double.MAX_VALUE; }

    @Override protected double computeMaxHeight(double width) { return Double.MAX_VALUE; }

    public PlayerPane(final MediaPlayer mp) {
        this.mp = mp;
        setId("player-pane");

        mediaView = new MediaView(mp);
        Pane mvPane = new Pane() { };
        mvPane.setId("media-pane");
        mvPane.getChildren().add(mediaView);
        setCenter(mvPane);

        mediaTopBar = HBoxBuilder.create()
                .padding(new Insets(5, 10, 5, 10))
                .alignment(Pos.CENTER)
                .opacity(1)
                .build();
        BorderPane.setAlignment(mediaTopBar, Pos.CENTER);

        mediaBottomBar = HBoxBuilder.create()
                .padding(new Insets(5, 10, 5, 10))
                .alignment(Pos.CENTER)
                .opacity(1)
                .build();
        BorderPane.setAlignment(mediaBottomBar, Pos.CENTER);

        mp.currentTimeProperty().addListener(new ChangeListener<Duration>() {
            @Override
            public void changed(ObservableValue<? extends Duration> observable, Duration oldValue, Duration newValue) {
                updateValues();
            }
        });
        mp.setOnPlaying(new Runnable() {
            public void run() {
                if (stopRequested) {
                    mp.pause();
                    stopRequested = false;
                } 
            }
        });
        mp.setOnReady(new Runnable() {
            public void run() {
                duration = mp.getMedia().getDuration();
                updateValues();
            }
        });
        mp.setOnEndOfMedia(new Runnable() {
            public void run() {
                if (!repeat) {
                    stopRequested = true;
                    atEndOfMedia = true;
                }
            }
        });
        mp.setCycleCount(repeat ? MediaPlayer.INDEFINITE : 1);

        // Time label
        Label timeLabel = LabelBuilder.create()
                .text("Time")
                .minWidth(Control.USE_PREF_SIZE)
                .textFill(Color.WHITE)
                .build();
        mediaTopBar.getChildren().add(timeLabel);

        // Time slider
        timeSlider = SliderBuilder.create()
                .id("media-slider")
                .minWidth(240)
                .maxWidth(Double.MAX_VALUE)
                .build();
        timeSlider.valueProperty().addListener(new InvalidationListener() {
            public void invalidated(Observable ov) {
                if (timeSlider.isValueChanging()) {
                    // multiply duration by percentage calculated by slider position
                    if (duration != null) {
                        mp.seek(duration.multiply(timeSlider.getValue() / 100.0));
                    }
                    updateValues();

                }
            }
        });
        HBox.setHgrow(timeSlider, Priority.ALWAYS);
        mediaTopBar.getChildren().add(timeSlider);

        // Play label
        playTime = LabelBuilder.create()
                .prefWidth(130)
                .minWidth(50)
                .textFill(Color.WHITE)
                .build();
        mediaTopBar.getChildren().add(playTime);

        // Volume label
        Label volumeLabel = LabelBuilder.create()
                .text("Vol")
                .textFill(Color.WHITE)
                .minWidth(Control.USE_PREF_SIZE)
                .build();
        mediaTopBar.getChildren().add(volumeLabel);

        // Volume slider
        volumeSlider = SliderBuilder.create()
                .id("media-slider")
                .prefWidth(120)
                .maxWidth(Region.USE_PREF_SIZE)
                .minWidth(30)
                .build();
        volumeSlider.valueProperty().addListener(new InvalidationListener() {
            public void invalidated(Observable ov) {
            }
        });
        volumeSlider.valueProperty().addListener(new ChangeListener<Number>() {
            @Override
            public void changed(ObservableValue<? extends Number> observable, Number oldValue, Number newValue) {
                if (volumeSlider.isValueChanging()) {
                    mp.setVolume(volumeSlider.getValue() / 100.0);
                }
            }
        });
        mediaTopBar.getChildren().add(volumeSlider);

        setTop(mediaTopBar);

        final EventHandler<ActionEvent> backAction = new EventHandler<ActionEvent>() {
            public void handle(ActionEvent e) {
                mp.seek(Duration.ZERO);
            }
        };
        final EventHandler<ActionEvent> stopAction = new EventHandler<ActionEvent>() {
            public void handle(ActionEvent e) {
                mp.stop();
            }
        };
        final EventHandler<ActionEvent> playAction = new EventHandler<ActionEvent>() {
            public void handle(ActionEvent e) {
                mp.play();
            }
        };
        final EventHandler<ActionEvent> pauseAction = new EventHandler<ActionEvent>() {
            public void handle(ActionEvent e) {
                mp.pause();
            }
        };
        final EventHandler<ActionEvent> forwardAction = new EventHandler<ActionEvent>() {
            public void handle(ActionEvent e) {
                Duration currentTime = mp.getCurrentTime();
                mp.seek(Duration.seconds(currentTime.toSeconds() + 5.0));
            }
        };

        mediaBottomBar = HBoxBuilder.create()
                .id("bottom")
                .spacing(0)
                .alignment(Pos.CENTER)
                .children(
                    ButtonBuilder.create()
                        .id("back-button")
                        .text("Back")
                        .onAction(backAction)
                        .build(),
                    ButtonBuilder.create()
                        .id("stop-button")
                        .text("Stop")
                        .onAction(stopAction)
                        .build(),
                    ButtonBuilder.create()
                        .id("play-button")
                        .text("Play")
                        .onAction(playAction)
                        .build(),
                    ButtonBuilder.create()
                        .id("pause-button")
                        .text("Pause")
                        .onAction(pauseAction)
                        .build(),
                    ButtonBuilder.create()
                        .id("forward-button")
                        .text("Forward")
                        .onAction(forwardAction)
                        .build()
                 )
                .build();

        setBottom(mediaBottomBar);
    }

    protected void updateValues() {
        if (playTime != null && timeSlider != null && volumeSlider != null && duration != null) {
            Platform.runLater(new Runnable() {
                public void run() {
                    Duration currentTime = mp.getCurrentTime();
                    playTime.setText(formatTime(currentTime, duration));
                    timeSlider.setDisable(duration.isUnknown());
                    if (!timeSlider.isDisabled() && duration.    greaterThan(Duration.ZERO) && !timeSlider.isValueChanging()) {
                        timeSlider.setValue(currentTime.divide(duration).toMillis() * 100.0);
                    }
                    if (!volumeSlider.isValueChanging()) {
                        volumeSlider.setValue((int) Math.round(mp.getVolume() * 100));
                    }
                }
            });
        }
    }

    private static String formatTime(Duration elapsed, Duration duration) {
        int intElapsed = (int)Math.floor(elapsed.toSeconds());
        int elapsedHours = intElapsed / (60 * 60);
        if (elapsedHours > 0) {
            intElapsed -= elapsedHours * 60 * 60;
        }
        int elapsedMinutes = intElapsed / 60;
        int elapsedSeconds = intElapsed - elapsedHours * 60 * 60 - elapsedMinutes * 60;

        if (duration.greaterThan(Duration.ZERO)) {
            int intDuration = (int)Math.floor(duration.toSeconds());
            int durationHours = intDuration / (60 * 60);
            if (durationHours > 0) {
                intDuration -= durationHours * 60 * 60;
            }
            int durationMinutes = intDuration / 60;
            int durationSeconds = intDuration - durationHours * 60 * 60 - durationMinutes * 60;

            if (durationHours > 0) {
                return String.format("%d:%02d:%02d",
                                     elapsedHours, elapsedMinutes, elapsedSeconds);
            } else {
                return String.format("%02d:%02d",
                                     elapsedMinutes, elapsedSeconds);
            }
        } else {
            if (elapsedHours > 0) {
                return String.format("%d:%02d:%02d",
                                     elapsedHours, elapsedMinutes, elapsedSeconds);
            } else {
                return String.format("%02d:%02d",
                                     elapsedMinutes, elapsedSeconds);
            }
        }
    }
}

@Override public void start(Stage primaryStage) throws Exception {
    init(primaryStage);
    primaryStage.show();
    play();
}
public static void main(String[] args) { launch(args); 
}
}

我对长代码感到抱歉。它只是来自 JavaFX 的示例代码。你在这里这里找到它。

4

1 回答 1

5

这应该很容易解决。让我用您展示的图表示例来解释它。

  1. 将 的实例添加JFXPanel到您的JFrame. 在您的示例中,所有组件都添加到 aStage中,这是表示窗口的 JavaFX 类。所以你在这里不需要它。相反,您将要使用的组件添加到 JFXPanel。另请参阅此处(功能initAndShowGUI)如何做到这一点。

  2. init示例的函数中,Scene创建了 a 以及图表本身。要让图表显示出来,您要做的仅此而已 - 创建一个Scene,用内容填充它并将其传递给JFXPanel您已经创建的。

  3. 只需最少的努力,您就可以运行图表示例:确保它AdvancedLineChartSample.java位于您的构建路径中,并且该函数createChart可以从您的 JFrame 以某种方式访问​​。然后使用类似于以下代码段的内容将图表添加到您的代码中。

    组根=新组();
    场景场景=新场景(根);
    myJFXPanel.setScene(场景);
    root.getChildren().add(createChart());

这只是一个非常快速且肮脏的解决方案,无需任何漂亮的代码即可运行您的示例,而且我也没有对其进行测试。但希望它能让你对正在发生的事情有一个基本的了解,以鼓励进一步的实验。根据我自己的经验,我可以告诉您,从这一步开始,JavaFX 2 会带来很多乐趣。

于 2012-11-03T21:34:40.323 回答