Java语言程序设计 第十五章 (15.25、15.26、15.27、15.28)
2018-02-06 21:20
405 查看
程序小白,希望和大家多交流,共同学习
![](https://oscdn.geek-share.com/Uploads/Images/Content/201802/5b97e3bdfa9b96b658931a0845308e8f)
![](https://oscdn.geek-share.com/Uploads/Images/Content/201802/977d9b091478d55eb69e3f2575364a1b)
15.25
15.26
15.27
15.28
15.25
//小球沿着正弦图像移动 import javafx.application.Application; import javafx.stage.Stage; import javafx.scene.Scene; import javafx.scene.paint.Color; import javafx.scene.shape.Polyline; import javafx.collections.ObservableList; import javafx.scene.shape.Line; import javafx.scene.text.Text; import javafx.scene.layout.Pane; import javafx.scene.shape.Circle; import javafx.animation.PathTransition; import javafx.animation.Timeline; import javafx.util.Duration; import javafx.scene.input.MouseButton; public class SinBall extends Application { @Override public void start(Stage primaryStage) { double width = 1000; double height = 500; final double PILEN = 100; final double A = height / 4; final double GAP = 20; Pane pane = new Pane(); //画坐标 Line x = new Line(GAP, height / 2, width - GAP, height / 2); Text textX = new Text(width - GAP, height / 2 - 5, "x"); Polyline directX = new Polyline(); ObservableList<Double> listX = directX.getPoints(); listX.add(width - GAP - 10); listX.add(height / 2 - 5); listX.add(width - GAP); listX.add(height / 2); listX.add(width - G 11b2a AP - 10); listX.add(height / 2 + 5); Line y = new Line(width / 2, height - GAP, width / 2, GAP); Text textY = new Text(width / 2 + 10, GAP, "y"); Polyline directY = new Polyline(); ObservableList<Double> listY = directY.getPoints(); listY.add(width / 2 - 5); listY.add(GAP + 10); listY.add(width / 2); listY.add(GAP); listY.add(width / 2 + 5); listY.add(GAP + 10); pane.getChildren().addAll(x, textX, directX, y, textY, directY); //画正弦图形 Polyline sinShape = new Polyline(); sinShape.setStroke(Color.RED); ObservableList<Double> sinList = sinShape.getPoints(); int rightX = (int)((width - GAP) / 2 - GAP); for (int i = -rightX; i < rightX; i++) { sinList.add(i + width / 2); sinList.add((height / 2) - A * Math.sin((i / PILEN) * Math.PI)); } pane.getChildren().add(sinShape); //显示PI("\u03c0") int num = (int)((width / 2 - GAP - 10) / PILEN); Text coordinate = new Text(); for (int i = -num; i < num; i++) { if (i == 1) { coordinate = new Text((width / 2 + i * PILEN), (height / 2 + 10), "\u03c0"); } else if (i == -1) { coordinate = new Text((width / 2 + i * PILEN), (height / 2 + 10), "-\u03c0"); } else if (i == 0) { coordinate = new Text((width / 2 + i * PILEN), (height / 2 + 10), "0"); } else coordinate = new Text((width / 2 + i * PILEN), (height / 2 + 10), i + "\u03c0"); pane.getChildren().add(coordinate); } Circle circle = new Circle(10); circle.setFill(Color.GOLD); pane.getChildren().add(circle); PathTransition animation = new PathTransition(); animation.setDuration(Duration.millis(10000)); animation.setPath(sinShape); animation.setNode(circle); animation.setCycleCount(Timeline.INDEFINITE); animation.play(); pane.setOnMouseClicked(e -> { if (e.getButton() == MouseButton.PRIMARY) { animation.pause(); } else if (e.getButton() == MouseButton.SECONDARY) { animation.play(); } }); Scene scene = new Scene(pane); primaryStage.setTitle("ShowSinCos"); primaryStage.setScene(scene); primaryStage.show(); } }
15.26
//来回摆动的圆,高处颜色变浅,低处颜色变深 import javafx.application.Application; import javafx.stage.Stage; import javafx.scene.Scene; import javafx.scene.layout.Pane; import javafx.scene.shape.Line; import javafx.scene.shape.Arc; import javafx.scene.shape.ArcType; import javafx.scene.shape.Circle; import javafx.scene.paint.Color; import javafx.animation.Timeline; import javafx.animation.PathTransition; import javafx.util.Duration; import javafx.scene.input.MouseButton; import javafx.animation.FadeTransition; public class Swing_FadeBall extends Application { @Override public void start(Stage primaryStage) { Pane pane = new Pane(); Circle ball = new Circle(10); ball.setFill(Color.RED); ball.setStroke(Color.BLACK); //小球颜色变化的动画 FadeTransition fadeBall = new FadeTransition(Duration.millis(500), ball); fadeBall.setFromValue(0.2); fadeBall.setToValue(1.0); fadeBall.setAutoReverse(true); fadeBall.setCycleCount(Timeline.INDEFINITE); //小球的路径 Arc path = new Arc(300, 200, 80, 60, 0, -180); path.setType(ArcType.OPEN); path.setFill(Color.WHITE); path.setStroke(Color.BLACK); pane.getChildren().addAll(path, ball); //小球移动的动画 PathTransition animation = new PathTransition(); animation.setDuration(Duration.millis(1000)); animation.setCycleCount(Timeline.INDEFINITE); animation.setPath(path); animation.setNode(ball); animation.setAutoReverse(true); animation.play(); fadeBall.play(); //鼠标事件 pane.setOnMouseClicked(e -> { if (e.getButton() == MouseButton.PRIMARY) { animation.pause(); fadeBall.pause(); } else if (e.getButton() == MouseButton.SECONDARY) { animation.play(); fadeBall.play(); } }); Scene scene = new Scene(pane, 600, 300); primaryStage.setTitle("SwingBall"); primaryStage.setScene(scene); primaryStage.show(); } }
15.27
//移动的文本 import javafx.application.Application; import javafx.stage.Stage; import javafx.scene.Scene; import javafx.scene.layout.Pane; import javafx.scene.text.Text; import javafx.scene.text.Font; import javafx.scene.paint.Color; import javafx.scene.shape.Line; import javafx.util.Duration; import javafx.animation.Timeline; import javafx.animation.PathTransition; public class MoveText extends Application { @Override public void start(Stage primaryStage) { double height = 100; double width = 600; Pane pane = new Pane(); //文本路径 Line line = new Line(); line.setStartX(0); line.setStartY(height / 2); line.setEndX(width); line.setEndY(height / 2); //文本 Text text = new Text("Programming is fun"); text.setStroke(Color.RED); text.setFont(Font.font("MyFont", 20)); pane.getChildren().add(text); //动画 PathTransition animation = new PathTransition(); animation.setDuration(Duration.millis(5000)); animation.setPath(line); animation.setNode(text); animation.setCycleCount(Timeline.INDEFINITE); animation.play(); //鼠标事件 text.setOnMousePressed(e -> { animation.pause(); }); text.setOnMouseReleased(e -> { animation.play(); }); primaryStage.setResizable(false); Scene scene = new Scene(pane, width, height); primaryStage.setTitle("MoveText"); primaryStage.setScene(scene); primaryStage.show(); } }
15.28
//三个按钮控制风扇 import javafx.application.Application; import javafx.stage.Stage; import javafx.scene.Scene; import javafx.scene.layout.Pane; import javafx.scene.layout.BorderPane; import javafx.scene.layout.HBox; import javafx.geometry.Pos; import javafx.scene.shape.Circle; import javafx.scene.shape.Arc; import javafx.scene.shape.ArcType; import javafx.scene.paint.Color; import javafx.scene.control.Button; import javafx.animation.PathTransition; import javafx.animation.Timeline; import javafx.util.Duration; import javafx.animation.KeyFrame; import javafx.event.ActionEvent; import javafx.event.EventHandler; public class RotateFang extends Application { private double rotate = 10; private boolean direction = true; @Override public void start(Stage primaryStage) { double rate = 20; //绘制面板内图形 //1.画三个按钮 HBox buttonPane = new HBox(10); buttonPane.setAlignment(Pos.CENTER); Button btPause = new Button("Pause"); Button btResume = new Button("Resume"); Button btReverse = new Button("Reverse"); buttonPane.getChildren().addAll(btPause, btResume, btReverse); //2.画风扇 Pane fanPane = new Pane(); Circle fan = new Circle(200, 200, 110); fan.setFill(Color.WHITE); fan.setStroke(Color.BLACK); Arc a1 = new Arc(200, 200, 100, 100, 0, 30); a1.setType(ArcType.ROUND); a1.setFill(Color.BLACK); Arc a2 = new Arc(200, 200, 100, 100, 90, 30); a2.setType(ArcType.ROUND); a2.setFill(Color.BLACK); Arc a3 = new Arc(200, 200, 100, 100, 180, 30); a3.setType(ArcType.ROUND); a3.setFill(Color.BLACK); Arc a4 = new Arc(200, 200, 100, 100, 270, 30); a4.setType(ArcType.ROUND); a4.setFill(Color.BLACK); fanPane.getChildren().addAll(fan, a1, a2, a3, a4); //绘制动画 EventHandler<ActionEvent> eventHandler = e -> { fanPane.setRotate(newRotate()); }; Timeline rotateFan = new Timeline(new KeyFrame(Duration.millis(100), eventHandler)); rotateFan.setCycleCount(Timeline.INDEFINITE); rotateFan.play(); btPause.setOnAction(e -> { rotateFan.pause(); }); btResume.setOnAction(e -> { rotateFan.play(); }); btReverse.setOnAction(e -> { direction = !direction;//改变当前的旋转方向 }); //画入主面板 BorderPane pane = new BorderPane(); pane.setCenter(fanPane); pane.setBottom(buttonPane); //System.out.println(pane.widthProperty().getValue() + " " + pane.heightProperty().getValue()); primaryStage.setResizable(false); Scene scene = new Scene(pane, 390, 415); primaryStage.setTitle("RotateFan"); primaryStage.setScene(scene); primaryStage.show(); } //确定新的旋转方向 public double newRotate() { if (direction) { System.out.println("true"); return rotate = (rotate + 10) % 360; } else { System.out.println("false"); return rotate = (rotate - 10) % 360; } } }
相关文章推荐
- Java语言程序设计 第十五章(15.33 )分豆机
- java语言程序设计 第十五章 (15.21、15.22、15.23、15.24)
- Java语言程序设计 第十五章 (15.8、15.9、15.10、15.11、15.12、15.13、15.14)
- Java语言程序设计 第十五章 (15.15、15.16、15.17、15.18、15.19、15.20)
- Java语言程序设计 第十五章(15.29、15.30、15.31、15.32)
- java语言程序设计 第十五章 (15.1、15.2、15.3、15.4、15.5、15.6、15.7)
- Java语言程序设计 第十五章(15.34、15.35、15.36)自回避自由漫步
- java语言程序设计 第十五章 (样例代码)
- Java语言程序设计 第十六章 (16.17、16.18、16.19、16.20、16.21)
- Java 语言程序设计基础篇原书第八版_第十二章_第八题_程序分享
- java语言程序设计 第十二章 (12.28、12.30、12.33)
- Java语言程序设计 第十六章 (样例代码)
- C++程序设计语言--第十五章:类层次结构
- 二级Java语言程序设计考试大纲(2013年版)
- java语言程序设计-基础篇-4.22课后题
- 《程序设计入门—Java语言.翁恺》第五周编程作业(2)-井字棋
- Java语言程序设计案例教程--源代码
- Java程序语言(基础篇)第2章 基本程序设计 编程练习题解答
- java语言程序设计第十版(Introduce to java) 课后习题 chapter6-10
- java语言程序设计第十版(Introduce to java) 课后习题 chapter6-25