package javafx2introbyexample.chapter3.recipe3_05;
import java.io.File;
import java.net.URI;
import java.net.URISyntaxException;
import javafx.animation.Interpolator;
import javafx.animation.TranslateTransition;
import javafx.animation.TranslateTransitionBuilder;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.event.EventHandler;
import javafx.geometry.Point2D;
import javafx.scene.Group;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.control.Slider;
import javafx.scene.control.SliderBuilder;
import javafx.scene.effect.DropShadow;
import javafx.scene.effect.DropShadowBuilder;
import javafx.scene.input.DataFormat;
import javafx.scene.input.DragEvent;
import javafx.scene.input.Dragboard;
import javafx.scene.input.MouseEvent;
import javafx.scene.input.TransferMode;
import javafx.scene.layout.VBox;
import javafx.scene.media.Media;
import javafx.scene.media.MediaErrorEvent;
import javafx.scene.media.MediaMarkerEvent;
import javafx.scene.media.MediaPlayer;
import javafx.scene.media.MediaPlayerBuilder;
import javafx.scene.media.MediaView;
import javafx.scene.media.MediaViewBuilder;
import javafx.scene.paint.Color;
import javafx.scene.shape.Arc;
import javafx.scene.shape.ArcBuilder;
import javafx.scene.shape.ArcType;
import javafx.scene.shape.Circle;
import javafx.scene.shape.CircleBuilder;
import javafx.scene.shape.Line;
import javafx.scene.shape.LineBuilder;
import javafx.scene.shape.Rectangle;
import javafx.scene.shape.RectangleBuilder;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;
import javafx.scene.text.Text;
import javafx.scene.text.TextBuilder;
import javafx.stage.Stage;
import javafx.stage.StageStyle;
import javafx.stage.WindowEvent;
import javafx.util.Duration;
/**
* Synchronize Animation and Media
* @author cdea
*/
public class SynchAnimationAndMedia extends Application {
private MediaPlayer mediaPlayer;
private MediaView mediaView;
private Point2D anchorPt;
private Point2D previousLocation;
private Slider progressSlider;
private ChangeListener<Duration> progressListener;
private boolean paused = false;
private Node pauseMessage;
private Text closedCaption;
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
Application.launch(args);
}
@Override
public void start(final Stage primaryStage) {
primaryStage.setTitle("Chapter 3-5 Synchronize Animation and Media");
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());
}
};
// toggle the message pause on the video
pauseMessage = createPauseMessage("Pause");
pauseMessage.setOpacity(0);
root.getChildren().add(pauseMessage);
final VBox messageArea = createClosedCaptionArea(scene);
root.getChildren().add(messageArea);
final TranslateTransition animateTheEnd = createTheEnd(scene);
root.getChildren().add(animateTheEnd.getNode());
// 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);
mediaPlayer.setOnEndOfMedia(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);
// when paused event display pause message
mediaPlayer.setOnPaused(new Runnable() {
@Override
public void run() {
pauseMessage.setOpacity(.90);
}
});
// when playing make pause text invisible
mediaPlayer.setOnPlaying(new Runnable() {
@Override
public void run() {
animateTheEnd.stop();
animateTheEnd.getNode().setTranslateY(scene.getHeight() + 40);
animateTheEnd.getNode().setOpacity(0);
pauseMessage.setOpacity(0);
}
});
// 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.cr
没有合适的资源?快使用搜索试试~ 我知道了~
[JavaFX.2.0:Introduction.by.Example] 源代码.zip
共43个文件
java:38个
css:3个
svg:1个
5星 · 超过95%的资源 需积分: 10 6 下载量 147 浏览量
2013-03-28
09:52:52
上传
评论
收藏 244KB ZIP 举报
温馨提示
[JavaFX.2.0:Introduction.by.Example] 源代码.zip 文字版PDF下载地址: http://download.csdn.net/detail/puziguo/5190176
资源推荐
资源详情
资源评论
收起资源包目录
[JavaFX.2.0:Introduction.by.Example] 源代码.zip (43个子文件)
978-1-4302-4257-4_Dea_src
javafx2introbyexample
chapter4
recipe4_03
ManipulatingHtmlContent.java 6KB
recipe4_05
DBUtils.java 7KB
DisplayContentsFromDatabase.java 10KB
recipe4_02
DisplayHtml5Content.java 851B
clock3.svg 40KB
recipe4_01
MyJavaFXApp.java 1KB
EmbeddingJavaFXInAWebPage.java 7KB
recipe4_04
RespondingToHtmlEvents.java 7KB
chapter2
recipe2_01
CreatingImages.java 7KB
recipe2_03
WorkingWithTheSceneGraph.java 4KB
recipe2_04
MyForm.java 2KB
GridPaneControlPanel.java 10KB
ManipulatingLayoutViaGrids.java 2KB
recipe2_05
sky.css 1KB
MyForm.java 2KB
controlStyle2.css 2KB
EnhancingWithCss.java 4KB
GridPaneControlPanel.java 10KB
controlStyle1.css 2KB
recipe2_02
GeneratingAnAnimation.java 11KB
chapter1
recipe1_03
DrawingText.java 1KB
recipe1_15
OrganizingUIWithSplitViews.java 4KB
recipe1_05
CreatingShapes.java 4KB
recipe1_09
smoke_glass_buttons1.png 159KB
GeneratingBorders.java 3KB
recipe1_04
ChangingTextFonts.java 2KB
recipe1_10
BindingExpressions.java 4KB
recipe1_12
BackgroundProcesses.java 5KB
recipe1_13
AssociatingKeyboardSequences.java 4KB
recipe1_16
AddingTabsToUI.java 3KB
recipe1_14
Person.java 2KB
WorkingWithTables.java 6KB
recipe1_17
DevelopingADialog.java 8KB
recipe1_08
AddingControlsToLayout.java 2KB
recipe1_07
CreatingMenus.java 3KB
recipe1_06
AssigningColorsToObjects.java 4KB
recipe1_11
CreatingAndWorkingWithObservableLists.java 4KB
recipe1_02
HelloWorldMain.java 1KB
chapter3
recipe3_04
MarkPosInVideo.java 20KB
recipe3_01
PlayingAudio.java 11KB
recipe3_03
MediaActionsAndEvents.java 19KB
recipe3_05
SynchAnimationAndMedia.java 21KB
recipe3_02
PlayingVideo.java 16KB
共 43 条
- 1
资源评论
- zhj1492014-10-17正在学javafx,正是需要的东西
- jim8182014-02-08我想的是源码
pzg111111
- 粉丝: 0
- 资源: 11
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功