程序小白,希望和大家多交流,共同学习 16.22
//控制一段音频播放的开始,循环和停止 import javafx.application.Application; import javafx.stage.Stage; import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.scene.layout.HBox; import javafx.scene.media.Media; import javafx.scene.media.MediaPlayer; import javafx.geometry.Pos; public class ControlMusic extends Application { @Override public void start(Stage primaryStage) { String str = getClass().getResource("sound/china.mp3").toString(); Media media = new Media(str); MediaPlayer mediaPlayer = new MediaPlayer(media); Button btPlay = new Button("Play"); Button btLoop = new Button("Loop"); Button btStop = new Button("Stop"); HBox pane = new HBox(10); pane.setAlignment(Pos.CENTER); pane.getChildren().addAll(btPlay, btLoop, btStop); btPlay.setOnAction(e -> { mediaPlayer.play(); mediaPlayer.setAutoPlay(false); }); btLoop.setOnAction(e -> { mediaPlayer.setAutoPlay(true); }); btStop.setOnAction(e -> { mediaPlayer.stop(); }); Scene scene = new Scene(pane); primaryStage.setTitle("ControlMusic"); primaryStage.setScene(scene); primaryStage.show(); } }16.23
//有声的图像动画 import javafx.application.Application; import javafx.stage.Stage; import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.scene.control.TextField; import javafx.scene.control.Label; import javafx.scene.image.ImageView; import javafx.scene.image.Image; import javafx.animation.Timeline; import javafx.animation.KeyFrame; import javafx.util.Duration; import javafx.event.EventHandler; import javafx.event.ActionEvent; import javafx.scene.layout.StackPane; import javafx.scene.layout.BorderPane; import javafx.scene.layout.HBox; import javafx.scene.layout.BorderPane; import javafx.scene.layout.GridPane; import javafx.geometry.Pos; import javafx.scene.media.Media; import javafx.scene.media.MediaPlayer; public class AnimationWithMusic extends Application { private int currentNum = 1; private int totalNum = 1; private ImageView image = new ImageView(); private Timeline animation = new Timeline(); private TextField tfSpeed = new TextField(); private TextField tfPath = new TextField(); private TextField tfNumber = new TextField(); private TextField tfURL = new TextField(); private String str = getClass().getResource("sound/china.mp3").toString(); private Media media = new Media(str); private MediaPlayer mediaPlayer = new MediaPlayer(media); @Override public void start(Stage primaryStage) { //添加文本框 tfSpeed.setAlignment(Pos.BOTTOM_LEFT); tfSpeed.setPrefColumnCount(25); tfPath.setAlignment(Pos.BOTTOM_LEFT); tfPath.setPrefColumnCount(25); tfNumber.setAlignment(Pos.BOTTOM_LEFT); tfNumber.setPrefColumnCount(25); tfURL.setAlignment(Pos.BOTTOM_LEFT); tfURL.setPrefColumnCount(25); //控制面板 GridPane gp = new GridPane(); gp.add(new Label("Enter information for animation"), 0, 0); gp.add(new Label("Animation speed in millisecond"), 0, 1); gp.add(tfSpeed, 1, 1); gp.add(new Label("Image file prefix"), 0, 2); gp.add(tfPath, 1, 2); gp.add(new Label("Number of images"), 0, 3); gp.add(tfNumber, 1, 3); gp.add(new Label("Audio file URL"), 0, 4); gp.add(tfURL, 1, 4); //添加图像面板 StackPane sp = new StackPane(); sp.setPrefSize(400, 300); sp.getChildren().add(image); //添加按钮 Button btStart = new Button("Start Animation"); HBox hb = new HBox(); hb.setAlignment(Pos.TOP_RIGHT); hb.getChildren().add(btStart); EventHandler<ActionEvent> eventHandler = e -> { changeImage(); }; animation.setCycleCount(Timeline.INDEFINITE); btStart.setOnAction(e -> { mediaPlayer.stop(); animation.stop(); currentNum = 1; boolean speed = judgeNum(tfSpeed.getText()); boolean number = judgeNum(tfNumber.getText()); boolean path = judgeLetter(tfPath.getText()); if (number) { totalNum = Integer.parseInt(tfNumber.getText()); } if (speed) { animation.getKeyFrames().clear(); animation.getKeyFrames().add(new KeyFrame( Duration.millis(Double.parseDouble(tfSpeed.getText())), eventHandler)); } if (speed && number && path) { animation.play(); changeMusic(); } }); BorderPane pane = new BorderPane(); pane.setTop(hb); pane.setCenter(sp); pane.setBottom(gp); Scene scene = new Scene(pane); primaryStage.setTitle("AnimationWithMusic"); primaryStage.setScene(scene); primaryStage.show(); } //显示不同的图片 public void changeImage() { if (currentNum <= totalNum) { image.setImage(new Image("image/" + tfPath.getText() + currentNum + ".gif")); currentNum++; } else currentNum = 1; } //判断最大数字的文本框是否正确输入 public boolean judgeNum(String str) { if (str.equals("")) { return false; } int len = str.length(); for (int i = 0; i < len; i++) { if (!Character.isDigit(str.charAt(i))) { return false; } } return true; } //判断路径文本框是否正确输入 public boolean judgeLetter(String str) { if (str.equals("")) { return false; } int len = str.length(); for (int i = 0; i < len; i++) { if (!Character.isLetter(str.charAt(i))) { return false; } } return true; } //重新设置背景音乐时 public void changeMusic() { mediaPlayer.stop(); String str = getClass().getResource("sound/" + tfURL.getText() + ".mp3").toString(); media = new Media(str); mediaPlayer = new MediaPlayer(media); mediaPlayer.play(); } }16.24
//修改后的视频播放器 import javafx.application.Application; import javafx.stage.Stage; import javafx.scene.Scene; import javafx.scene.control.Label; import javafx.scene.control.Slider; import javafx.scene.control.Button; import javafx.scene.layout.BorderPane; import javafx.scene.layout.HBox; import javafx.scene.media.Media; import javafx.scene.media.MediaPlayer; import javafx.scene.media.MediaView; import javafx.scene.media.MediaPlayer.Status; import javafx.geometry.Pos; import javafx.animation.Timeline; import javafx.animation.KeyFrame; import javafx.util.Duration; import javafx.event.EventHandler; import javafx.event.ActionEvent; import javafx.beans.InvalidationListener; import javafx.beans.Observable; import javafx.beans.property.DoubleProperty; import javafx.beans.property.SimpleDoubleProperty; public class FinalRadioPlay extends Application { private double totalTime = 0; private String all = ""; @Override public void start(Stage primaryStage) { String radio = getClass().getResource("out/longzhou.mp4").toString(); Media media = new Media(radio); MediaPlayer mediaPlayer = new MediaPlayer(media); MediaView mediaView = new MediaView(mediaPlayer); mediaView.setFitWidth(400); mediaView.setFitHeight(400); Button btPlay = new Button(">"); Slider slTime = new Slider(); slTime.setMax(200); Slider slVolume = new Slider(); slVolume.setMax(200); //播放时间和总时间的标签 long time = (long)media.getDuration().toSeconds(); //System.out.println(time); //System.out.println(getTime(time)); Label timeInfo = new Label(getTime((long)mediaPlayer.getCurrentTime().toSeconds()) + "/" + getTime(time)); //各个使用标签 HBox hb = new HBox(10); hb.setAlignment(Pos.CENTER); hb.getChildren().addAll(btPlay, new Label("Time"), slTime, timeInfo, new Label("Volume"), slVolume); //功能实现 slVolume.setValue(50); mediaPlayer.volumeProperty().bind(slVolume.valueProperty().divide(200)); //显示时间的标签(时间线动画) EventHandler<ActionEvent> eventHandler = e -> { double nowTime = (mediaPlayer.getCurrentTime().toSeconds()); String now = getTime((long)nowTime); timeInfo.setText(now + "/" + all); if (totalTime != 0) { slTime.setValue((nowTime / totalTime) * 200.0); } }; Timeline animationFotTime = new Timeline(new KeyFrame(Duration.millis(500), eventHandler)); animationFotTime.setCycleCount(Timeline.INDEFINITE); animationFotTime.play(); //时间滑动条功能实现 // slTime.valueProperty().addListener(new InvalidationListener(){ // public void invalidated(Observable o) // { // double totalTime = media.getDuration().toMillis(); // double newTime = (slTime.valueProperty().getValue() / 200 * totalTime); // mediaPlayer.seek(Duration.millis(newTime)); // } // }); //按钮功能实现 btPlay.setOnAction(e -> { if (btPlay.getText().equals(">")) { mediaPlayer.play(); btPlay.setText("||"); totalTime = media.getDuration().toSeconds(); all = getTime((long)totalTime); } else if (btPlay.getText().equals("||")) { mediaPlayer.pause(); btPlay.setText(">"); } }); BorderPane pane = new BorderPane(); pane.setCenter(mediaView); pane.setBottom(hb); Scene scene = new Scene(pane); primaryStage.setTitle("RadioPlay"); primaryStage.setScene(scene); primaryStage.show(); } //给定的秒数返回一个小时:分钟:秒的时间格式 public String getTime(long seconds) { long currentSecond = seconds % 60; long totalMinutes = (long)(seconds / 60); long currentMinute = totalMinutes % 60; long totalHours = (long)(totalMinutes / 24); long currentHour = totalHours % 24; //System.out.println("" + currentHour + " " + currentMinute + " " + currentSecond); String str = String.format("d:d:d", currentHour, currentMinute, currentSecond); return str; } }16.25
//小车继承Pane //进行了扩充 //CarPane,使小车可以按照指定的x,y绘制,并且可以加减速,开始暂停,还有定时器 import javafx.scene.shape.Circle; import javafx.scene.shape.Polygon; import javafx.collections.ObservableList; import javafx.scene.shape.Rectangle; import javafx.scene.paint.Color; import javafx.scene.shape.Line; import javafx.animation.PathTransition; import javafx.animation.Timeline; import javafx.util.Duration; import javafx.scene.layout.Pane; public class CarPane extends Pane { private double x;//起始点x坐标 private double y;//起始点y坐标 private double w;//窗体宽度 private double time;//动画时间 private PathTransition aRoof; private PathTransition aBody; private PathTransition aWheel1; private PathTransition aWheel2; private Polygon roof; private Rectangle body; private Circle wheel1; private Circle wheel2; private Duration duration; public CarPane() { this(0, 100, 400, 50000); } public CarPane(double w) { this(0, 50, w, 50000); } public CarPane(double x, double y) { this(x, y, 400, 50000); } public CarPane(double x, double y, double w, double time) { this.x = x; this.y = y; this.time = time; this.w = w; aRoof = new PathTransition(); aBody = new PathTransition(); aWheel1 = new PathTransition(); aWheel2 = new PathTransition(); duration = new Duration(time); roof = new Polygon(); body = new Rectangle(); wheel1 = new Circle(); wheel2 = new Circle(); paintCar(); } //设置速度 public void setRate(double rate) { aRoof.setRate(rate < 0.0 ? 0.0 : rate); aBody.setRate(rate < 0.0 ? 0.0 : rate); aWheel1.setRate(rate < 0.0 ? 0.0 : rate); aWheel2.setRate(rate < 0.0 ? 0.0 : rate); } //动画开始 public void play() { aRoof.play(); aBody.play(); aWheel1.play(); aWheel2.play(); } //画小车 public void paintCar() { //车顶 roof.setFill(Color.RED); roof.setStroke(Color.BLACK); ObservableList<Double> list = roof.getPoints(); list.add(x + 10); list.add(y - 20); list.add(x + 20); list.add(y - 30); list.add(x + 30); list.add(y - 30); list.add(x + 40); list.add(y - 20); Line lR = new Line(x + 25, y - 25, w - 25, y - 25); //车体 body.setX(x); body.setY(y - 20); body.setWidth(50); body.setHeight(10); body.setFill(Color.RED); body.setStroke(Color.BLACK); Line lB = new Line(x + 25, y - 15, w - 25, y - 15); //车轮1 wheel1.setCenterX(x + 15); wheel1.setCenterY(y - 5); wheel1.setRadius(5); wheel1.setFill(Color.BLACK); Line lW1 = new Line(x + 15, y - 5, w - 35, y - 5); //车轮2 wheel2.setCenterX(x + 35); wheel2.setCenterY(y - 5); wheel2.setRadius(5); wheel2.setFill(Color.BLACK); Line lW2 = new Line(x + 35, y - 5, w - 15, y - 5); super.getChildren().addAll(roof, body, wheel1, wheel2); aRoof.setDuration(duration); aRoof.setPath(lR); aRoof.setNode(roof); aRoof.setCycleCount(Timeline.INDEFINITE); aRoof.play(); aBody.setDuration(duration); aBody.setPath(lB); aBody.setNode(body); aBody.setCycleCount(Timeline.INDEFINITE); aBody.play(); aWheel1.setDuration(duration); aWheel1.setPath(lW1); aWheel1.setNode(wheel1); aWheel1.setCycleCount(Timeline.INDEFINITE); aWheel1.play(); aWheel2.setDuration(duration); aWheel2.setPath(lW2); aWheel2.setNode(wheel2); aWheel2.setCycleCount(Timeline.INDEFINITE); aWheel2.play(); } } //控制小车的面板,同时控制四辆车 import javafx.application.Application; import javafx.stage.Stage; import javafx.scene.Scene; import javafx.scene.control.Label; import javafx.scene.paint.Color; import javafx.scene.control.TextField; import javafx.scene.layout.VBox; import javafx.scene.layout.HBox; import javafx.geometry.Pos; public class ControlCarPane extends Application { private CarPane[] cars = new CarPane[4]; private TextField[] tfCars = new TextField[4]; @Override public void start(Stage primaryStage) { //初始化文本框和汽车面板 double w = 500; for (int i = 0; i < 4; i++) { cars[i] = new CarPane(w); cars[i].setPrefSize(w, 50); cars[i].setStyle("-fx-border-color: black"); } for (int i = 0; i < 4; i++) { tfCars[i] = new TextField("12"); tfCars[i].setPrefColumnCount(5); } //面板功能实现 tfCars[0].setOnAction(e -> { changeRate(0); }); tfCars[1].setOnAction(e -> { changeRate(1); }); tfCars[2].setOnAction(e -> { changeRate(2); }); tfCars[3].setOnAction(e -> { changeRate(3); }); //排版文本框 HBox hbForTf = new HBox(10); hbForTf.setAlignment(Pos.CENTER); hbForTf.getChildren().addAll(new Label("Car1:"), tfCars[0], new Label("Car2:"), tfCars[1], new Label("Car3:"), tfCars[2], new Label("Car4"), tfCars[3]); VBox pane = new VBox(10); pane.getChildren().addAll(hbForTf, cars[0], cars[1], cars[2], cars[3]); Scene scene = new Scene(pane); primaryStage.setTitle("ControlCars"); primaryStage.setScene(scene); primaryStage.show(); } //按照文本框的值设置速度 public void changeRate(int i) { cars[i].setRate(Double.parseDouble(tfCars[i].getText())); } }16.26
//升国旗奏国歌 import javafx.application.Application; import javafx.stage.Stage; import javafx.scene.Scene; import javafx.scene.shape.Line; import javafx.scene.paint.Color; import javafx.scene.image.ImageView; import javafx.scene.media.Media;; import javafx.scene.media.MediaPlayer; import javafx.animation.PathTransition; import javafx.animation.Timeline; import javafx.scene.layout.Pane; import javafx.util.Duration; public class RaiseNationalFlag extends Application { @Override public void start(Stage primaryStage) { //设置面板初始值 Pane pane = new Pane(); pane.setPrefSize(400, 400); //国旗路径 Line path = new Line(100, 320, 100, 50); ImageView flag = new ImageView("image/china.gif"); //背景音乐 String str = getClass().getResource("sound/china.mp3").toString(); Media music = new Media(str); MediaPlayer player = new MediaPlayer(music); player.play(); //升旗动画 PathTransition animation = new PathTransition(Duration.millis(10000), path, flag); animation.play(); //旗杆 Line line = new Line(30, 380, 30, 10); line.setStroke(Color.BLACK); line.setFill(Color.BLACK); pane.getChildren().addAll(line, flag); Scene scene = new Scene(pane); primaryStage.setTitle("RaiseNationalFlag"); primaryStage.setScene(scene); primaryStage.show(); } }