• 【无标题】


    目录

    1、选取 4 张卡牌

     2、旋转一个四边形

    3、移动小球

    4、创建一个簡单的计算器

    5、 两个消息交替出现

    6、使用鼠标改变颜色

     7、显示鼠标的位罝

    8、使用箭头键画线

    9、输入并显示字符串

    10、使用键移动圓

    11、几何问題:添加或删除点

     12、两个可移动的顶点以及它们间的距离

    13、几何问题:是否在圆内?

    14、几何问題:寻找边界矩形

     15、使用鼠标来移动一个矩形

    16、游戏:手眼协调

    ​17、动画:来回摆动

     18、动画:曲线上的球

     19、改变透明度

    20、控制一个移动的文本

    21、显示一个转动的风扇

     22、播放幻灯片

    23、几何问題:钟摆


    1、选取 4 张卡牌

    请写一个程序,可以让用户通过单击 Refiesh 按钮以显示从一副52 张卡牌选取的 4 张卡牌如图所示

    代码

    1. package EventDrivenAndAnimation_Practice;
    2. import java.util.ArrayList;
    3. import java.util.Arrays;
    4. import javafx.application.Application;
    5. import javafx.geometry.Pos;
    6. import javafx.scene.Scene;
    7. import javafx.scene.control.Button;
    8. import javafx.scene.image.Image;
    9. import javafx.scene.image.ImageView;
    10. import javafx.scene.layout.HBox;
    11. import javafx.scene.layout.GridPane;
    12. import javafx.scene.layout.HBox;
    13. import javafx.scene.layout.BorderPane;
    14. import javafx.scene.paint.Color;
    15. import javafx.scene.shape.Circle;
    16. import javafx.stage.Stage;
    17. import javafx.event.ActionEvent;
    18. import javafx.event.EventHandler;
    19. public class ShowFourCards extends Application {
    20. // 创建一个PokerPane
    21. private PorkerPane porkerPane = new PorkerPane();
    22. public void start(Stage primaryStage) {
    23. // 新建hbox用于存放button
    24. HBox hbox = new HBox();
    25. hbox.setSpacing(10);
    26. hbox.setAlignment(Pos.CENTER);
    27. //新建一个button
    28. Button btReflesh = new Button("Refresh");
    29. hbox.getChildren().add(btReflesh);
    30. //为reflesh按钮注册处理器
    31. btReflesh.setOnAction(new RefleshHandler());
    32. //把hbox porkerpane放入面板
    33. BorderPane borderPane = new BorderPane();
    34. borderPane.setCenter(porkerPane);
    35. borderPane.setBottom(hbox);
    36. BorderPane.setAlignment(hbox, Pos.CENTER);
    37. Scene scene = new Scene(borderPane, 450, 200);
    38. primaryStage.setTitle("ShowFourCards");
    39. primaryStage.setScene(scene);
    40. primaryStage.show();
    41. }
    42. //定义一个名为 RefleshHandler的处理器类
    43. class RefleshHandler implements EventHandler {
    44. public void handle(ActionEvent e) {
    45. porkerPane.reflesh();
    46. }
    47. }
    48. //定义一个新的类PorkerPane用于显示扑克牌
    49. class PorkerPane extends GridPane {
    50. private Image ig1;
    51. private Image ig2;
    52. private Image ig3;
    53. private Image ig4;
    54. public PorkerPane() {
    55. // ig1 = ig2 = ig3 = ig4 = new Image("/porkerImage/53.jpg");
    56. //
    57. // setHgap(5);
    58. // add(new ImageView(ig1), 0, 0);
    59. // add(new ImageView(ig2), 1, 0);
    60. // add(new ImageView(ig3), 2, 0);
    61. // add(new ImageView(ig4), 3, 0);
    62. }
    63. //提供reflesh方法用于显示随机的四张牌
    64. public void reflesh() {
    65. Integer array[] = new Integer[52];
    66. for (int i = 1; i <= 52; i++)
    67. array[i - 1] = i;
    68. ArrayList list = new ArrayList<>(Arrays.asList(array));
    69. java.util.Collections.shuffle(list);
    70. String url1 = "/porkerImage/" + list.get(0) + ".jpg";
    71. String url2 = "/porkerImage/" + list.get(1) + ".jpg";
    72. String url3 = "/porkerImage/" + list.get(2) + ".jpg";
    73. String url4 = "/porkerImage/" + list.get(3) + ".jpg";
    74. ig1 = new Image(url1);
    75. ig2 = new Image(url2);
    76. ig3 = new Image(url3);
    77. ig4 = new Image(url4);
    78. setHgap(5);
    79. add(new ImageView(ig1), 0, 0);
    80. add(new ImageView(ig2), 1, 0);
    81. add(new ImageView(ig3), 2, 0);
    82. add(new ImageView(ig4), 3, 0);
    83. }
    84. }
    85. public static void main(String[] args) {
    86. // TODO Auto-generated method stub
    87. Application.launch(args);
    88. }
    89. }

    运行结果

     2、旋转一个四边形

    请写一个程序,在 Rotate 按钮被单击时,将一个四边形向右旋转15 度如图所示

    代码

    1. package EventDrivenAndAnimation_Practice;
    2. import javafx.application.Application;
    3. import javafx.geometry.Pos;
    4. import javafx.scene.Scene;
    5. import javafx.scene.control.Button;
    6. import javafx.scene.layout.HBox;
    7. import javafx.scene.layout.StackPane;
    8. import javafx.scene.layout.BorderPane;
    9. import javafx.scene.paint.Color;
    10. import javafx.scene.shape.Rectangle;
    11. import javafx.stage.Stage;
    12. import javafx.event.ActionEvent;
    13. import javafx.event.EventHandler;
    14. public class RotateAQuadrilateral extends Application {
    15. private QuadrilateralPane quadrilateralPane = new QuadrilateralPane();
    16. public void start(Stage primaryStage) {
    17. HBox hbox = new HBox();
    18. hbox.setSpacing(10);
    19. hbox.setAlignment(Pos.CENTER);
    20. Button btRotate = new Button("Rotate");
    21. hbox.getChildren().add(btRotate);
    22. btRotate.setOnAction(new RotateHandler());
    23. BorderPane borderPane = new BorderPane();
    24. borderPane.setCenter(quadrilateralPane);
    25. borderPane.setBottom(hbox);
    26. BorderPane.setAlignment(hbox, Pos.CENTER);
    27. Scene scene = new Scene(borderPane, 200, 150);
    28. primaryStage.setTitle("RotateAQuadrilateral");
    29. primaryStage.setScene(scene);
    30. primaryStage.show();
    31. }
    32. class RotateHandler implements EventHandler {
    33. public void handle(ActionEvent e) {
    34. quadrilateralPane.rotate();
    35. }
    36. }
    37. class QuadrilateralPane extends StackPane {
    38. private Rectangle rectangle = new Rectangle(50, 80);
    39. public QuadrilateralPane() {
    40. getChildren().add(rectangle);
    41. rectangle.setStroke(Color.BLACK);
    42. rectangle.setFill(Color.GOLD);
    43. }
    44. public void rotate() {
    45. rectangle.setRotate(rectangle.getRotate() + 15);
    46. }
    47. }
    48. public static void main(String[] args) {
    49. // TODO Auto-generated method stub
    50. Application.launch(args);
    51. }
    52. }
    运行结果

    3、移动小球

    编写一个程序,在面板上移动小球。应该定义一个面板类来显示小球,并提供向左、 向右、向上和向下移动小球的方法如图所示请进行边界检査以防止球完全移到视线外。

    问题分析:一开始把BallPane类定义成了

    class BallPane extends StackPane

    发现小球无法移动。改为

    class BallPane extends Pane

    程序正常运行

    代码

    1. package EventDrivenAndAnimation_Practice;
    2. import javafx.application.Application;
    3. import javafx.geometry.Pos;
    4. import javafx.scene.Scene;
    5. import javafx.scene.control.Button;
    6. import javafx.scene.layout.HBox;
    7. import javafx.scene.layout.Pane;
    8. import javafx.scene.layout.BorderPane;
    9. import javafx.scene.paint.Color;
    10. import javafx.scene.shape.Circle;
    11. import javafx.stage.Stage;
    12. import javafx.event.ActionEvent;
    13. import javafx.event.EventHandler;
    14. public class MoveBall extends Application {
    15. private BallPane ballPane = new BallPane();
    16. public void start(Stage primaryStage) {
    17. HBox hbox = new HBox();
    18. hbox.setSpacing(10);
    19. hbox.setAlignment(Pos.CENTER);
    20. Button btLeft = new Button("Left");
    21. Button btRight = new Button("Right");
    22. Button btUp = new Button("Up");
    23. Button btDown = new Button("Down");
    24. hbox.getChildren().addAll(btLeft, btRight, btUp, btDown);
    25. btLeft.setOnAction((ActionEvent e) -> {
    26. ballPane.left();
    27. });
    28. btRight.setOnAction((ActionEvent e) -> {
    29. ballPane.right();
    30. });
    31. btUp.setOnAction((ActionEvent e) -> {
    32. ballPane.up();
    33. });
    34. btDown.setOnAction((ActionEvent e) -> {
    35. ballPane.down();
    36. });
    37. BorderPane borderPane = new BorderPane();
    38. borderPane.setCenter(ballPane);
    39. borderPane.setBottom(hbox);
    40. BorderPane.setAlignment(hbox, Pos.CENTER);
    41. Scene scene = new Scene(borderPane, 500, 150);
    42. primaryStage.setTitle("MoveBall");
    43. primaryStage.setScene(scene);
    44. primaryStage.show();
    45. }
    46. // 定义BallPane用于放置小球
    47. class BallPane extends Pane {
    48. private Circle circle = new Circle(50,50,30);
    49. public BallPane() {
    50. getChildren().add(circle);
    51. circle.setStroke(Color.BLACK);
    52. circle.setFill(Color.GREY);
    53. }
    54. // 提供四个方法用于移动小球
    55. public void left() {
    56. circle.setCenterX(circle.getCenterX() > 10 ? circle.getCenterX() - 10 : 0);
    57. }
    58. public void right() {
    59. circle.setCenterX(circle.getCenterX() < this.getWidth() - 10 ? circle.getCenterX() + 10 : 0);
    60. }
    61. public void up() {
    62. circle.setCenterY(circle.getCenterY() > 10 ? circle.getCenterY() - 10 : 0);
    63. }
    64. public void down() {
    65. circle.setCenterY(circle.getCenterY() < this.getHeight() - 10 ? circle.getCenterY() + 10 : 0);
    66. }
    67. }
    68. public static void main(String[] args) {
    69. // TODO Auto-generated method stub
    70. Application.launch(args);
    71. }
    72. }

     运行结果

    4、创建一个簡单的计算器

    编写一个程序完成加法减法乘法和除法操作

    代码

    1. package EventDrivenAndAnimation_Practice;
    2. import javafx.application.Application;
    3. import javafx.geometry.Pos;
    4. import javafx.geometry.HPos;
    5. import javafx.scene.Scene;
    6. import javafx.scene.control.Button;
    7. import javafx.scene.control.Label;
    8. import javafx.scene.control.TextField;
    9. import javafx.scene.layout.GridPane;
    10. import javafx.scene.layout.HBox;
    11. import javafx.scene.paint.Color;
    12. import javafx.stage.Stage;
    13. public class Calculator extends Application {
    14. private TextField tfNumber1 = new TextField();
    15. private TextField tfNumber2 = new TextField();
    16. private TextField tfResult = new TextField();
    17. private Button btAdd = new Button("Add");
    18. private Button btSubstract = new Button("Substract");
    19. private Button btMultiply = new Button("Multiply");
    20. private Button btDivide = new Button("Divide");
    21. public void start(Stage primaryStage) {
    22. GridPane pane = new GridPane();
    23. pane.setHgap(5);
    24. pane.setVgap(5);
    25. pane.add(new Label("Number1:"), 0, 0);
    26. pane.add(tfNumber1, 1, 0);
    27. pane.add(new Label("Number2:"), 0, 1);
    28. pane.add(tfNumber2, 1, 1);
    29. pane.add(new Label("Result"), 0, 2);
    30. pane.add(tfResult, 1, 2);
    31. pane.add(btAdd, 0, 3);
    32. pane.add(btSubstract, 1, 3);
    33. pane.add(btMultiply, 0, 4);
    34. pane.add(btDivide, 1, 4);
    35. pane.setAlignment(Pos.CENTER);
    36. tfNumber1.setAlignment(Pos.BOTTOM_RIGHT);
    37. tfNumber2.setAlignment(Pos.BOTTOM_RIGHT);
    38. tfResult.setAlignment(Pos.BOTTOM_RIGHT);
    39. btAdd.setOnAction(e -> add());
    40. btSubstract.setOnAction(e -> substract());
    41. btMultiply.setOnAction(e -> multiply());
    42. btDivide.setOnAction(e -> divide());
    43. Scene scene = new Scene(pane, 500, 500);
    44. primaryStage.setTitle("Calculator");
    45. primaryStage.setScene(scene);
    46. primaryStage.show();
    47. }
    48. private void add() {
    49. double number1 = Double.parseDouble(tfNumber1.getText());
    50. double number2 = Double.parseDouble(tfNumber1.getText());
    51. tfResult.setText(String.format("%.2f", number1 + number2));
    52. }
    53. private void substract() {
    54. double number1 = Double.parseDouble(tfNumber1.getText());
    55. double number2 = Double.parseDouble(tfNumber1.getText());
    56. tfResult.setText(String.format("%.2f", number1 - number2));
    57. }
    58. private void multiply() {
    59. double number1 = Double.parseDouble(tfNumber1.getText());
    60. double number2 = Double.parseDouble(tfNumber1.getText());
    61. tfResult.setText(String.format("%.2f", number1 * number2));
    62. }
    63. private void divide() {
    64. double number1 = Double.parseDouble(tfNumber1.getText());
    65. double number2 = Double.parseDouble(tfNumber1.getText());
    66. tfResult.setText(String.format("%.2f", number1 / number2));
    67. }
    68. public static void main(String[] args) {
    69. // TODO Auto-generated method stub
    70. Application.launch(args);
    71. }
    72. }

    运行结果

    5、 两个消息交替出现

    编写一个程序当单击鼠标时面板上交替显示两个文本 “ Java is

    fun Java i s powerful
    代码
    1. package EventDrivenAndAnimation_Practice;
    2. import javafx.animation.Animation;
    3. import javafx.animation.FadeTransition;
    4. import javafx.animation.Timeline;
    5. import javafx.animation.KeyFrame;
    6. import javafx.event.*;
    7. import javafx.application.Application;
    8. import javafx.scene.Scene;
    9. import javafx.scene.image.ImageView;
    10. import javafx.scene.layout.Pane;
    11. import javafx.scene.paint.Color;
    12. import javafx.scene.shape.Line;
    13. import javafx.scene.text.Text;
    14. import javafx.scene.shape.Circle;
    15. import javafx.stage.Stage;
    16. import javafx.util.Duration;
    17. public class AppearAlternately extends Application {
    18. public void start(Stage primaryStage) {
    19. Pane pane = new Pane();
    20. Text text = new Text(20, 50, "Programming is fun");
    21. text.setFill(Color.RED);
    22. pane.getChildren().add(text);
    23. EventHandler eventHandler = e -> {
    24. if (text.getText() == "Programming is fun")
    25. text.setText("Programming is powerful");
    26. else
    27. text.setText("Programming is fun");
    28. };
    29. Timeline animation = new Timeline(new KeyFrame(Duration.millis(500), eventHandler));
    30. animation.setCycleCount(Timeline.INDEFINITE);
    31. animation.play();
    32. text.setOnMouseClicked(e -> {
    33. if (animation.getStatus() == Animation.Status.PAUSED)
    34. animation.play();
    35. else
    36. animation.pause();
    37. });
    38. Scene scene = new Scene(pane, 250, 200);
    39. primaryStage.setTitle("TimelineDemo");
    40. primaryStage.setScene(scene);
    41. primaryStage.show();
    42. }
    43. public static void main(String[] args) {
    44. // TODO Auto-generated method stub
    45. Application.launch(args);
    46. }
    47. }

    6、使用鼠标改变颜色

    编写一个程序显示一个圆的颜色当按下鼠标键时颜色为黑色,释放鼠

    标时颜色为白色
    代码
    1. package EventDrivenAndAnimation_Practice;
    2. import javafx.animation.Animation;
    3. import javafx.animation.FadeTransition;
    4. import javafx.animation.Timeline;
    5. import javafx.animation.KeyFrame;
    6. import javafx.event.*;
    7. import javafx.application.Application;
    8. import javafx.scene.Scene;
    9. import javafx.scene.image.ImageView;
    10. import javafx.scene.layout.StackPane;
    11. import javafx.scene.paint.Color;
    12. import javafx.scene.shape.Circle;
    13. import javafx.scene.text.Text;
    14. import javafx.scene.shape.Circle;
    15. import javafx.stage.Stage;
    16. import javafx.util.Duration;
    17. public class MouseClickChangeCircleColor extends Application {
    18. public void start(Stage primaryStage) {
    19. StackPane pane = new StackPane();
    20. Circle circle = new Circle(50);
    21. circle.setFill(Color.GRAY);
    22. pane.getChildren().add(circle);
    23. circle.setOnMouseClicked(e -> {
    24. if (circle.getFill() == Color.GRAY)
    25. circle.setFill(Color.BLACK);
    26. else
    27. circle.setFill(Color.GRAY);
    28. });
    29. Scene scene = new Scene(pane, 250, 200);
    30. primaryStage.setTitle("TimelineDemo");
    31. primaryStage.setScene(scene);
    32. primaryStage.show();
    33. }
    34. public static void main(String[] args) {
    35. // TODO Auto-generated method stub
    36. Application.launch(args);
    37. }
    38. }

    运行结果

     7、显示鼠标的位罝

    编写两个程序,一个当单击鼠标时显示鼠标的位置.而另一个当按下鼠标时显示鼠标的位置,当释放鼠标时停止显示。

     代码

    1. package EventDrivenAndAnimation_Practice;
    2. import javafx.application.Application;
    3. import javafx.scene.Scene;
    4. import javafx.scene.layout.Pane;
    5. import javafx.scene.text.Text;
    6. import javafx.stage.Stage;
    7. public class DisplayMousePositionWhenClicked extends Application {
    8. public void start(Stage primaryStage) {
    9. Pane pane = new Pane();
    10. Text text = new Text();
    11. pane.getChildren().addAll(text);
    12. pane.setOnMouseClicked(e -> {
    13. text.setText("(" + e.getX() + "," + e.getY() + ")");
    14. text.setX(e.getX());
    15. text.setY(e.getY());
    16. });
    17. pane.setOnMouseReleased(event -> text.setText(""));
    18. Scene scene = new Scene(pane, 200, 200);
    19. primaryStage.setTitle("DisplayMousePositionWhenClicked");
    20. primaryStage.setScene(scene);
    21. primaryStage.show();
    22. }
    23. public static void main(String[] args) {
    24. // TODO Auto-generated method stub
    25. Application.launch(args);
    26. }
    27. }
    1. package EventDrivenAndAnimation_Practice;
    2. import javafx.application.Application;
    3. import javafx.scene.Scene;
    4. import javafx.scene.layout.Pane;
    5. import javafx.scene.text.Text;
    6. import javafx.stage.Stage;
    7. public class DisplayMousePositionWhenPressed extends Application {
    8. public void start(Stage primaryStage) {
    9. Pane pane = new Pane();
    10. Text text = new Text();
    11. pane.getChildren().addAll(text);
    12. pane.setOnMousePressed(e -> {
    13. text.setText("(" + e.getX() + "," + e.getY() + ")");
    14. text.setX(e.getX());
    15. text.setY(e.getY());
    16. });
    17. pane.setOnMouseReleased(event -> text.setText(""));
    18. Scene scene = new Scene(pane, 200, 200);
    19. primaryStage.setTitle("DisplayMousePositionWhenClicked");
    20. primaryStage.setScene(scene);
    21. primaryStage.show();
    22. }
    23. public static void main(String[] args) {
    24. // TODO Auto-generated method stub
    25. Application.launch(args);
    26. }
    27. }

    8、使用箭头键画线

    请编写一个程序使用箭头键绘制线段所画的线从面板的中心开始当敲

    击向右 向上 向左或向下的箭头键时 相应地向东 向北 向西或向南方向画线 如图所示

    代码
    1. package EventDrivenAndAnimation_Practice;
    2. import javafx.application.Application;
    3. import javafx.scene.Scene;
    4. import javafx.scene.layout.Pane;
    5. import javafx.scene.shape.Line;
    6. import javafx.scene.text.Text;
    7. import javafx.stage.Stage;
    8. public class PaintLines extends Application {
    9. private double width = 400 / 2, height = 400 / 2;
    10. public void start(Stage primaryStage) {
    11. Pane pane = new Pane();
    12. pane.setOnKeyPressed(e -> {
    13. switch (e.getCode()) {
    14. case DOWN:
    15. pane.getChildren().add(new Line(width, height, width, height + 50));
    16. height += 50;
    17. break;
    18. case UP:
    19. pane.getChildren().add(new Line(width, height, width, height - 50));
    20. height -= 50;
    21. break;
    22. case LEFT:
    23. pane.getChildren().add(new Line(width, height, width - 50, height));
    24. width -= 50;
    25. break;
    26. case RIGHT:
    27. pane.getChildren().add(new Line(width, height, width + 50, height));
    28. width += 50;
    29. break;
    30. }
    31. });
    32. Scene scene = new Scene(pane);
    33. primaryStage.setTitle("PaintLines");
    34. primaryStage.setScene(scene);
    35. primaryStage.show();
    36. pane.requestFocus();
    37. }
    38. public static void main(String[] args) {
    39. // TODO Auto-generated method stub
    40. Application.launch(args);
    41. }
    42. }

    运行结果

    9、输入并显示字符串

    请编写一个程序从键盘接收一个宇符串并把它显示在面板上回车键表明字符串结束。任何时候输人一个新宇符串时都会将它显示在面板上

    代码

    1. package EventDrivenAndAnimation_Practice;
    2. import javafx.application.Application;
    3. import javafx.scene.Scene;
    4. import javafx.scene.input.KeyCode;
    5. import javafx.scene.layout.StackPane;
    6. import javafx.scene.text.Text;
    7. import javafx.stage.Stage;
    8. public class ShowString extends Application {
    9. public void start(Stage primaryStage) {
    10. StackPane pane = new StackPane();
    11. Text text = new Text("");
    12. pane.getChildren().add(text);
    13. text.setOnKeyPressed(e -> {
    14. text.setText(e.getCode() == KeyCode.ENTER ? "" : text.getText() + e.getText());
    15. });
    16. Scene scene = new Scene(pane, 500, 500);
    17. primaryStage.setTitle("ShowString");
    18. primaryStage.setScene(scene);
    19. primaryStage.show();
    20. text.requestFocus();
    21. }
    22. public static void main(String[] args) {
    23. // TODO Auto-generated method stub
    24. Application.launch(args);
    25. }
    26. }

    10、使用键移动圓

    请编写程序可以使用箭头键向上向下向左向右移动一个圆

    代码

    1. package EventDrivenAndAnimation_Practice;
    2. import javafx.application.Application;
    3. import javafx.scene.Scene;
    4. import javafx.scene.layout.Pane;
    5. import javafx.scene.paint.Color;
    6. import javafx.scene.shape.Circle;
    7. import javafx.scene.text.Text;
    8. import javafx.stage.Stage;
    9. public class MoveCircle extends Application {
    10. public void start(Stage primaryStage) {
    11. Pane pane = new Pane();
    12. Circle circle = new Circle(100, 100, 50);
    13. circle.setFill(Color.GRAY);
    14. pane.getChildren().add(circle);
    15. circle.setOnKeyPressed(e -> {
    16. switch (e.getCode()) {
    17. case DOWN:
    18. circle.setCenterY(circle.getCenterY() + 20);
    19. break;
    20. case UP:
    21. circle.setCenterY(circle.getCenterY() - 20);
    22. break;
    23. case LEFT:
    24. circle.setCenterX(circle.getCenterX() - 20);
    25. break;
    26. case RIGHT:
    27. circle.setCenterX(circle.getCenterX() + 20);
    28. break;
    29. }
    30. });
    31. Scene scene = new Scene(pane, 400, 400);
    32. primaryStage.setTitle("MoveCircle");
    33. primaryStage.setScene(scene);
    34. primaryStage.show();
    35. circle.requestFocus();
    36. }
    37. public static void main(String[] args) {
    38. // TODO Auto-generated method stub
    39. Application.launch(args);
    40. }
    41. }

    11、几何问題:添加或删除点

    请编写一个程序,让用户在面板上单击以自动创建或移去点,当用户左击鼠标时(主按钮),就创建一个点并且显示在鼠标的位置,用户还可以将鼠标移到一个点上,然后右击鼠标(次按钮)以移去这个点.

     代码

    1. package EventDrivenAndAnimation_Practice;
    2. import javafx.application.Application;
    3. import javafx.scene.Scene;
    4. import javafx.scene.Node;
    5. import javafx.scene.input.MouseButton;
    6. import javafx.scene.layout.Pane;
    7. import javafx.scene.shape.Circle;
    8. import javafx.scene.paint.Color;
    9. import javafx.scene.text.Text;
    10. import javafx.stage.Stage;
    11. public class AddOrDeleteDots extends Application {
    12. public void start(Stage primaryStage) {
    13. Pane pane = new Pane();
    14. pane.setOnMouseClicked(e -> {
    15. Circle circle = new Circle(5);
    16. if (e.getButton() == MouseButton.PRIMARY) {
    17. circle.setFill(Color.WHITE);
    18. circle.setStroke(Color.BLACK);
    19. circle.setCenterX(e.getX());
    20. circle.setCenterY(e.getY());
    21. pane.getChildren().add(circle);
    22. } else
    23. for (Node node : pane.getChildren()) {
    24. Circle temp = (Circle) node;
    25. if (isInCircle(temp, e.getX(), e.getY())) {
    26. pane.getChildren().remove(temp);
    27. break;
    28. }
    29. }
    30. });
    31. Scene scene = new Scene(pane, 200, 200);
    32. primaryStage.setTitle("AddOrDeleteDots");
    33. primaryStage.setScene(scene);
    34. primaryStage.show();
    35. }
    36. private boolean isInCircle(Circle circle, double x, double y) {
    37. return Math.sqrt(Math.pow(x - circle.getCenterX(), 2) + Math.pow(y - circle.getCenterY(), 2)) <= circle
    38. .getRadius();
    39. }
    40. public static void main(String[] args) {
    41. // TODO Auto-generated method stub
    42. Application.launch(args);
    43. }
    44. }

    运行结果

     12、两个可移动的顶点以及它们间的距离

    请编写一个程序,显示两个分别位于(40,40)120,150) 的半径为10的圆并用一条直线连接两个圆如图所示圆之间的距离显示在直线上。 用户可以拖动圆,圆和它上面的直线会相应移动并且两个圆之间的距离会更新

    代码

    1. package EventDrivenAndAnimation_Practice;
    2. import javafx.application.Application;
    3. import javafx.scene.Scene;
    4. import javafx.scene.layout.Pane;
    5. import javafx.scene.paint.Color;
    6. import javafx.scene.shape.Circle;
    7. import javafx.scene.shape.Line;
    8. import javafx.scene.text.Text;
    9. import javafx.stage.Stage;
    10. public class TwoMovableCircles extends Application {
    11. public void start(Stage primaryStage) {
    12. Pane pane = new Pane();
    13. Circle circle1 = new Circle(40, 40, 10);
    14. Circle circle2 = new Circle(120, 150, 10);
    15. circle1.setFill(Color.GRAY);
    16. circle2.setFill(Color.GRAY);
    17. Line line = new Line(40, 40, 120, 150);
    18. double x1 = circle1.getCenterX(), x2 = circle2.getCenterX();
    19. double y1 = circle1.getCenterY(), y2 = circle2.getCenterY();
    20. Text text = new Text((line.getStartX() + line.getEndX()) / 2, (line.getStartY() + line.getEndY()) / 2,
    21. distance(x1, x2, y1, y2) + "");
    22. pane.getChildren().addAll(circle1, circle2, line, text);
    23. circle1.setOnMouseDragged(e -> {
    24. circle1.setCenterX(e.getX());
    25. circle1.setCenterY(e.getY());
    26. line.setStartX(e.getX());
    27. line.setStartY(e.getY());
    28. double distance1 = distance(circle1.getCenterX(), circle2.getCenterX(), circle1.getCenterY(),
    29. circle2.getCenterY());
    30. text.setX((line.getStartX() + line.getEndX()) / 2);
    31. text.setY((line.getStartY() + line.getEndY()) / 2);
    32. text.setText(String.valueOf(distance1));
    33. });
    34. circle2.setOnMouseDragged(e -> {
    35. circle2.setCenterX(e.getX());
    36. circle2.setCenterY(e.getY());
    37. line.setEndX(e.getX());
    38. line.setEndY(e.getY());
    39. double distance2 = distance(circle1.getCenterX(), circle2.getCenterX(), circle1.getCenterY(),
    40. circle2.getCenterY());
    41. text.setX((line.getStartX() + line.getEndX()) / 2);
    42. text.setY((line.getStartY() + line.getEndY()) / 2);
    43. text.setText(String.valueOf(distance2));
    44. });
    45. Scene scene = new Scene(pane, 400, 400);
    46. primaryStage.setTitle("TwoMovableCircles");
    47. primaryStage.setScene(scene);
    48. primaryStage.show();
    49. }
    50. public double distance(double x1, double x2, double y1, double y2) {
    51. double distance = Math.sqrt(Math.pow(x1 - x2, 2) + Math.pow(y1 - y2, 2));
    52. return distance;
    53. }
    54. public static void main(String[] args) {
    55. // TODO Auto-generated method stub
    56. Application.launch(args);
    57. }
    58. }

    运行结果

    13、几何问题是否在圆

    请编写一个程序绘制一个圆心在100, 60)而半径为 50的固定 的圆。当鼠标移动时显示一条消息表示鼠标点是在圆内还是在圆外如图所示

     

     代码

    1. package EventDrivenAndAnimation_Practice;
    2. import javafx.application.Application;
    3. import javafx.scene.Scene;
    4. import javafx.scene.layout.Pane;
    5. import javafx.scene.paint.Color;
    6. import javafx.scene.shape.Circle;
    7. import javafx.scene.shape.Line;
    8. import javafx.scene.text.Text;
    9. import javafx.stage.Stage;
    10. public class IsMouseInCircle extends Application {
    11. public void start(Stage primaryStage) {
    12. Pane pane = new Pane();
    13. Circle circle = new Circle(100, 60, 50);
    14. circle.setFill(Color.PURPLE);
    15. Text text = new Text();
    16. pane.getChildren().addAll(circle, text);
    17. pane.setOnMouseMoved(e -> {
    18. double distance = Math.sqrt(Math.pow(e.getX() - 100, 2) + Math.pow(e.getY() - 60, 2));
    19. text.setX(e.getX());
    20. text.setY(e.getY());
    21. if (distance <= 50) {
    22. text.setText("Mouse point is inside the circle");
    23. text.setFill(Color.BLACK);
    24. }
    25. else {
    26. text.setText("Mouse point is not inside the circle");
    27. text.setFill(Color.RED);
    28. }
    29. });
    30. Scene scene = new Scene(pane, 300, 300);
    31. primaryStage.setTitle("IsMouseInCircle");
    32. primaryStage.setScene(scene);
    33. primaryStage.show();
    34. }
    35. public static void main(String[] args) {
    36. // TODO Auto-generated method stub
    37. Application.launch(args);
    38. }
    39. }

    14、几何问題:寻找边界矩形

    请编写一个程序让用户可以在一个二维面板上动态地增加和移除 点,如图所示。当点加人和移除的时候,一个最小的边界矩形更新显示假设每个点的半径是10像素

     代码

    问题分析:添加点实现了,但是删除点还实现不了......

    1. package EventDrivenAndAnimation_Practice;
    2. import javafx.application.Application;
    3. import javafx.scene.Scene;
    4. import javafx.scene.Node;
    5. import javafx.scene.input.MouseButton;
    6. import javafx.scene.layout.Pane;
    7. import javafx.scene.shape.Circle;
    8. import javafx.scene.shape.Rectangle;
    9. import javafx.scene.paint.Color;
    10. import javafx.scene.text.Text;
    11. import javafx.stage.Stage;
    12. import java.util.ArrayList;
    13. import java.util.Arrays;
    14. public class FindingBoundary extends Application {
    15. public void start(Stage primaryStage) {
    16. Pane pane = new Pane();
    17. Rectangle rectangle = new Rectangle();
    18. rectangle.setStroke(Color.BLACK);
    19. rectangle.setFill(Color.WHITE);
    20. rectangle.setOpacity(1);
    21. pane.getChildren().add(rectangle);
    22. ArrayList listX = new ArrayList<>();
    23. ArrayList listY = new ArrayList<>();
    24. pane.setOnMouseClicked(e -> {
    25. Circle circle = new Circle(5);
    26. if (e.getButton() == MouseButton.PRIMARY) {
    27. circle.setFill(Color.WHITE);
    28. circle.setStroke(Color.BLACK);
    29. circle.setCenterX(e.getX());
    30. circle.setCenterY(e.getY());
    31. pane.getChildren().add(circle);
    32. listX.add(e.getX());
    33. listY.add(e.getY());
    34. java.util.Collections.sort(listX);
    35. java.util.Collections.sort(listY);
    36. rectangle.setX(listX.get(0) - 5);
    37. rectangle.setY(listY.get(0) - 5);
    38. rectangle.setWidth(listX.get(listX.size() - 1) - listX.get(0) + 10);
    39. rectangle.setHeight(listY.get(listY.size() - 1) - listY.get(0) + 10);
    40. }
    41. // else {
    42. //
    43. // for (Node node : pane.getChildren()) {
    44. //
    45. // pane.getChildren().remove(rectangle);
    46. // Circle temp = (Circle) node;
    47. //
    48. // if (isInCircle(temp, e.getX(), e.getY())) {
    49. // pane.getChildren().remove(temp);
    50. //
    51. // listX.remove(e.getX());
    52. // listY.remove(e.getY());
    53. // java.util.Collections.sort(listX);
    54. // java.util.Collections.sort(listY);
    55. //
    56. // rectangle.setStroke(Color.BLACK);
    57. // rectangle.setFill(Color.WHITE);
    58. //
    59. // rectangle.setX(listX.get(0) - 5);
    60. // rectangle.setY(listY.get(0) - 5);
    61. // rectangle.setWidth(listX.get(listX.size() - 1) - listX.get(0) + 10);
    62. // rectangle.setHeight(listY.get(listY.size() - 1) - listY.get(0) + 10);
    63. // pane.getChildren().add(rectangle);
    64. // break;
    65. // }
    66. // }
    67. //
    68. // }
    69. });
    70. Scene scene = new Scene(pane, 200, 200);
    71. primaryStage.setTitle("FindingBoundary");
    72. primaryStage.setScene(scene);
    73. primaryStage.show();
    74. }
    75. private boolean isInCircle(Circle circle, double x, double y) {
    76. return Math.sqrt(Math.pow(x - circle.getCenterX(), 2) + Math.pow(y - circle.getCenterY(), 2)) <= circle
    77. .getRadius();
    78. }
    79. public static void main(String[] args) {
    80. // TODO Auto-generated method stub
    81. Application.launch(args);
    82. }
    83. }

    运行结果

     15、使用鼠标来移动一个矩形

    请编写一个程序显示一个矩形。可以使用鼠标单击矩形内部并且拖 动(即按住鼠标移动矩形到鼠标的位置。鼠标点成为矩形的中央

    代码

    1. package EventDrivenAndAnimation_Practice;
    2. import javafx.application.Application;
    3. import javafx.scene.Scene;
    4. import javafx.scene.layout.Pane;
    5. import javafx.scene.text.Text;
    6. import javafx.scene.paint.Color;
    7. import javafx.scene.shape.Circle;
    8. import javafx.stage.Stage;
    9. public class MoveRectangleWithMouse extends Application {
    10. public void start(Stage primaryStage) {
    11. Pane pane = new Pane();
    12. Circle circle = new Circle(50,50,30);
    13. circle.setStroke(Color.BLACK);
    14. circle.setFill(Color.GRAY);
    15. pane.getChildren().addAll(circle);
    16. circle.setOnMouseDragged(e -> {
    17. circle.setCenterX(e.getX());
    18. circle.setCenterY(e.getY());
    19. });
    20. Scene scene = new Scene(pane, 300, 300);
    21. primaryStage.setTitle("MouseEventDemo");
    22. primaryStage.setScene(scene);
    23. primaryStage.show();
    24. }
    25. public static void main(String[] args) {
    26. // TODO Auto-generated method stub
    27. Application.launch(args);
    28. }
    29. }

    16、游戏手眼协调

    请编写一个程序显示一个半径为 10 像素的实心圆,该圆放置在面板上的

    随机位置 并填充随机的顔色 如图 所示 单击这个圆时 它会消失 然后在另一个 随机的位置显示新的随机颜色的圆。在单击了 20 个圆之后 在面板上显示所用的时间,如图所示

     代码

    1. package EventDrivenAndAnimation_Practice;
    2. import javafx.animation.Animation;
    3. import javafx.animation.FadeTransition;
    4. import javafx.animation.Timeline;
    5. import javafx.animation.KeyFrame;
    6. import javafx.event.*;
    7. import javafx.application.Application;
    8. import javafx.scene.Scene;
    9. import javafx.scene.image.ImageView;
    10. import javafx.scene.layout.Pane;
    11. import javafx.scene.paint.Color;
    12. import javafx.scene.shape.Circle;
    13. import javafx.scene.text.Text;
    14. import javafx.scene.shape.Circle;
    15. import javafx.stage.Stage;
    16. import javafx.util.Duration;
    17. import java.util.Date;
    18. public class HandAndEyeCoordination extends Application {
    19. int count = 0;
    20. public void start(Stage primaryStage) {
    21. Pane pane = new Pane();
    22. Circle circle = new Circle(Math.random() * 400, Math.random() * 400, 30);
    23. circle.setFill(new Color(Math.random(), Math.random(), Math.random(), Math.random()));
    24. pane.getChildren().add(circle);
    25. Date date1 = new Date(), date2;
    26. circle.setOnMouseClicked(e -> {
    27. count();
    28. circle.setCenterX(Math.random() * 400);
    29. circle.setCenterY(Math.random() * 400);
    30. circle.setFill(new Color(Math.random(), Math.random(), Math.random(), Math.random()));
    31. if (count == 5) {
    32. double time = new Date().getTime() - date1.getTime();
    33. Text text = new Text(50, 50, "Time spent is " + time + " milliseconds");
    34. text.setStroke(Color.BLACK);
    35. pane.getChildren().add(text);
    36. }
    37. });
    38. Scene scene = new Scene(pane, 400, 400);
    39. primaryStage.setTitle("HandAndEyeCoordination");
    40. primaryStage.setScene(scene);
    41. primaryStage.show();
    42. }
    43. public void count() {
    44. count++;
    45. }
    46. public static void main(String[] args) {
    47. // TODO Auto-generated method stub
    48. Application.launch(args);
    49. }
    50. }

    运行结果


    17、动画来回摆动

    编写一个程序用动画完成来回摆动如图所示单击 / 释放鼠标以暂停 / 恢复动画

     

     代码

    不足:实现的是长按暂停、释放停止。如果用setOnMouseClicked和setOnMouseReleased,只能实现暂停,暂未找到解决方法

    1. package EventDrivenAndAnimation_Practice;
    2. import javafx.animation.PathTransition;
    3. import javafx.animation.Timeline;
    4. import javafx.application.Application;
    5. import javafx.scene.Scene;
    6. import javafx.scene.layout.Pane;
    7. import javafx.scene.paint.Color;
    8. import javafx.scene.shape.Arc;
    9. import javafx.scene.shape.ArcType;
    10. import javafx.scene.shape.Circle;
    11. import javafx.stage.Stage;
    12. import javafx.util.Duration;
    13. public class SwingTheBall extends Application {
    14. public void start(Stage primaryStage) {
    15. Pane pane = new Pane();
    16. Arc arc = new Arc(100, 100, 80, 80, -40, -100);
    17. arc.setStroke(Color.BLACK);
    18. arc.setFill(new Color(0, 0, 0, 0));
    19. arc.setType(ArcType.OPEN);
    20. Circle circle = new Circle(15);
    21. // circle.setStroke(Color.BLACK);
    22. // circle.setFill(Color.BLACK);
    23. pane.getChildren().addAll(circle, arc);
    24. PathTransition pt = new PathTransition();
    25. pt.setDuration(Duration.millis(5000));
    26. pt.setPath(arc);
    27. pt.setNode(circle);
    28. pt.setOrientation(PathTransition.OrientationType.ORTHOGONAL_TO_TANGENT);
    29. pt.setCycleCount(Timeline.INDEFINITE);
    30. pt.setAutoReverse(true);
    31. pt.play();
    32. pane.setOnMousePressed(e -> pt.pause());
    33. pane.setOnMouseReleased(e -> pt.play());
    34. Scene scene = new Scene(pane, 300, 300);
    35. primaryStage.setTitle("SwingTheBallo");
    36. primaryStage.setScene(scene);
    37. primaryStage.show();
    38. }
    39. public static void main(String[] args) {
    40. // TODO Auto-generated method stub
    41. Application.launch(args);
    42. }
    43. }

    运行结果

     18、动画:曲线上的球

    请编写一个程序,用动画实现一个沿着正弦函数曲线移动的球,如图

    所示。当球到达右边界时,它从左边重新开始。用户可以单击鼠标左 / 右按钮来继续 / 暂停动画

     代码

    1. package EventDrivenAndAnimation_Practice;
    2. import javafx.application.Application;
    3. import javafx.collections.ObservableList;
    4. import javafx.geometry.Pos;
    5. import javafx.scene.Scene;
    6. import javafx.scene.input.MouseButton;
    7. import javafx.scene.layout.Pane;
    8. import javafx.scene.paint.Color;
    9. import javafx.scene.shape.Circle;
    10. import javafx.scene.shape.Line;
    11. import javafx.scene.text.*;
    12. import javafx.stage.Stage;
    13. import javafx.util.Duration;
    14. import javafx.scene.shape.Polyline;
    15. import javafx.animation.PathTransition;
    16. import javafx.animation.Timeline;
    17. public class BallSwingAlongSineFunction extends Application{
    18. public void start(Stage primaryStage) {
    19. Pane pane = new Pane();
    20. Polyline polyline = new Polyline();
    21. pane.getChildren().add(polyline);
    22. polyline.setFill(Color.WHITE);
    23. polyline.setStroke(Color.PURPLE);
    24. ObservableList list = polyline.getPoints();
    25. for (double y = 0; y <= 1400; y++) {
    26. list.add(y);
    27. list.add(200 - 50 * Math.sin(Math.toRadians(y - 700)));
    28. }
    29. Line x = new Line(20, 200, 1500, 200);
    30. Line y = new Line(700, 600, 700, 10);
    31. x.setStrokeWidth(3);
    32. x.setStroke(Color.BLACK);
    33. pane.getChildren().add(x);
    34. y.setStrokeWidth(3);
    35. y.setStroke(Color.BLACK);
    36. pane.getChildren().add(y);
    37. Line arrowX1 = new Line(1480, 190, 1500, 200);
    38. Line arrowX2 = new Line(1480, 210, 1500, 200);
    39. arrowX1.setStroke(Color.BLACK);
    40. arrowX1.setStrokeWidth(3);
    41. arrowX2.setStroke(Color.BLACK);
    42. arrowX2.setStrokeWidth(3);
    43. Line arrowY1 = new Line(690, 30, 700, 10);
    44. Line arrowY2 = new Line(710, 30, 700, 10);
    45. arrowY1.setStroke(Color.BLACK);
    46. arrowY1.setStrokeWidth(3);
    47. arrowY2.setStroke(Color.BLACK);
    48. arrowY2.setStrokeWidth(3);
    49. Text t1 = new Text(1480, 220, "x");
    50. t1.setFont((Font.font("Times Nre Roman", FontWeight.BOLD, FontPosture.ITALIC, 25)));
    51. Text t2 = new Text(710, 8, "y");
    52. t2.setFont((Font.font("Times Nre Roman", FontWeight.BOLD, FontPosture.ITALIC, 25)));
    53. pane.getChildren().addAll(arrowX1, arrowX2, arrowY1, arrowY2, t1, t2);
    54. Circle circle = new Circle(15);
    55. pane.getChildren().add(circle);
    56. PathTransition pt = new PathTransition();
    57. pt.setDuration(Duration.millis(5000));
    58. pt.setPath(polyline);
    59. pt.setNode(circle);
    60. pt.setOrientation(PathTransition.OrientationType.ORTHOGONAL_TO_TANGENT);
    61. pt.setCycleCount(Timeline.INDEFINITE);
    62. pt.setAutoReverse(true);
    63. pt.play();
    64. pane.setOnMousePressed(e -> {
    65. if(e.getButton() == MouseButton.PRIMARY)
    66. pt.pause();
    67. else
    68. pt.play();
    69. });
    70. Scene scene = new Scene(pane, 1600, 450);
    71. primaryStage.setTitle("BallSwingAlongSineFunction");
    72. primaryStage.setScene(scene);
    73. primaryStage.show();
    74. }
    75. public static void main(String[] args) {
    76. // TODO Auto-generated method stub
    77. Application.launch(args);
    78. }
    79. }

    运行结果

     19、改变透明度

    重写上一道, 当球摆动的时候改变球的透明度

    只需要在原代码基础上加

    1. FadeTransition ft = new FadeTransition(Duration.millis(5000),circle);
    2. ft.setFromValue(1.0);
    3. ft.setToValue(0.1);
    4. ft.setCycleCount(Timeline.INDEFINITE);
    5. ft.setAutoReverse(true);
    6. ft.play();
    7. circle.setOnMousePressed(e->ft.pause());
    8. circle.setOnMouseReleased(e->ft.play());

    20、控制一个移动的文本

    请编写一个程序显示一个移动的文本,如图所示。 文本从左到右循环的移动。当它消失在右侧的时候,又会从左侧再次出现。当鼠标按下的时 候,文本停滞不动当按钮释放的时候将继续移动

    代码

    1. package EventDrivenAndAnimation_Practice;
    2. import javafx.animation.PathTransition;
    3. import javafx.animation.Timeline;
    4. import javafx.application.Application;
    5. import javafx.scene.Scene;
    6. import javafx.scene.layout.Pane;
    7. import javafx.scene.paint.Color;
    8. import javafx.scene.text.Text;
    9. import javafx.scene.shape.Line;
    10. import javafx.scene.shape.Circle;
    11. import javafx.stage.Stage;
    12. import javafx.util.Duration;
    13. public class MoveTextAlongLine extends Application {
    14. public void start(Stage primaryStage) {
    15. Pane pane = new Pane();
    16. Text text = new Text("Programming is fun");
    17. Line line = new Line(-50, 50, 350, 50);
    18. line.setStroke(Color.WHITE);
    19. pane.getChildren().addAll(text, line);
    20. PathTransition pt = new PathTransition();
    21. pt.setDuration(Duration.millis(5000));
    22. pt.setPath(line);
    23. pt.setNode(text);
    24. pt.setOrientation(PathTransition.OrientationType.ORTHOGONAL_TO_TANGENT);
    25. pt.setCycleCount(Timeline.INDEFINITE);
    26. pt.setAutoReverse(true);
    27. pt.play();
    28. pane.setOnMousePressed(e -> pt.pause());
    29. pane.setOnMouseReleased(e -> pt.play());
    30. text.setOnMousePressed(e -> pt.pause());
    31. text.setOnMouseReleased(e -> pt.play());
    32. Scene scene = new Scene(pane, 300, 150);
    33. primaryStage.setTitle("SwingTheBallo");
    34. primaryStage.setScene(scene);
    35. primaryStage.show();
    36. }
    37. public static void main(String[] args) {
    38. // TODO Auto-generated method stub
    39. Application.launch(args);
    40. }
    41. }

    运行结果

    21、显示一个转动的风扇

    编写一个程序显示一个转动的风扇如图所示PauseResume

    Reverse 按钮用于暂停 继续和反转风扇的转动

     代码

    1. package EventDrivenAndAnimation_Practice;
    2. import javafx.animation.KeyFrame;
    3. import javafx.animation.PathTransition;
    4. import javafx.animation.Timeline;
    5. import javafx.application.Application;
    6. import javafx.geometry.Pos;
    7. import javafx.scene.Scene;
    8. import javafx.scene.layout.Pane;
    9. import javafx.scene.layout.BorderPane;
    10. import javafx.scene.layout.HBox;
    11. import javafx.scene.control.Button;
    12. import javafx.scene.paint.Color;
    13. import javafx.scene.text.Text;
    14. import javafx.scene.shape.Line;
    15. import javafx.scene.shape.Arc;
    16. import javafx.scene.shape.ArcType;
    17. import javafx.scene.shape.Circle;
    18. import javafx.stage.Stage;
    19. import javafx.util.Duration;
    20. public class ShowARatatingFan extends Application {
    21. private FanPane pane = new FanPane();
    22. public void start(Stage primaryStage) {
    23. HBox hbox = new HBox();
    24. hbox.setSpacing(10);
    25. hbox.setAlignment(Pos.CENTER);
    26. Button btPause = new Button("Pause");
    27. Button btResume = new Button("Resume");
    28. Button btReverse = new Button("Reverse");
    29. hbox.getChildren().addAll(btPause, btResume, btReverse);
    30. Timeline timeline = new Timeline(new KeyFrame(Duration.millis(20), e -> pane.resume()));
    31. timeline.setCycleCount(Timeline.INDEFINITE);
    32. timeline.play();
    33. btPause.setOnAction(e -> timeline.pause());
    34. btResume.setOnAction(e -> timeline.play());
    35. btReverse.setOnAction(e -> pane.reverse());
    36. BorderPane borderPane = new BorderPane();
    37. borderPane.setCenter(pane);
    38. borderPane.setBottom(hbox);
    39. BorderPane.setAlignment(hbox, Pos.CENTER);
    40. Scene scene = new Scene(borderPane);
    41. primaryStage.setTitle("ShowARatatingFan");
    42. primaryStage.setScene(scene);
    43. primaryStage.show();
    44. }
    45. class FanPane extends Pane {
    46. private Circle circle = new Circle(100, 70, 60);;
    47. private Arc[] arc = new Arc[4];
    48. private double startAngle = 30;
    49. private double increment = 5;
    50. public FanPane() {
    51. circle.setStroke(Color.BLACK);
    52. circle.setFill(Color.WHITE);
    53. getChildren().add(circle);
    54. for (int i = 0; i < 4; i++) {
    55. arc[i] = new Arc(100, 70, 50, 50, startAngle + 90 * i, 30);
    56. arc[i].setType(ArcType.ROUND);
    57. arc[i].setFill(Color.BLACK);
    58. getChildren().add(arc[i]);
    59. }
    60. }
    61. public void setAngle(double angle) {
    62. startAngle = angle;
    63. for (int i = 0; i < 4; i++)
    64. arc[i].setStartAngle(startAngle + 90 * i);
    65. }
    66. public void resume() {
    67. setAngle(startAngle + increment);
    68. }
    69. public void reverse() {
    70. increment *= -1;
    71. }
    72. }
    73. public static void main(String[] args) {
    74. // TODO Auto-generated method stub
    75. Application.launch(args);
    76. }
    77. }

    运行结果

     22、播放幻灯片

    25 张幻灯片都以图像文件(slide0.jpg, slide1.jpg, ....slide24.jpg)的形式存储

    在图像目录中. 可以在本书的源代码中下载。每个图像的大小都是 800 x 600 像素。编写一个
    Java 应用程序 自动重复显示这些幻灯片 每两秒显示一张幻灯片 幻灯片按顺序显示 当显
    示完最后一张幻灯片时 第一张幻灯片重复显示 依此类推 当动画正在播放的时候可以单击
    按钮暂停 如果动画当前是暂停的 单击恢复
    代码
    1. package EventDrivenAndAnimation_Practice;
    2. import javafx.animation.Animation;
    3. import javafx.animation.FadeTransition;
    4. import javafx.animation.Timeline;
    5. import javafx.animation.KeyFrame;
    6. import javafx.event.*;
    7. import javafx.application.Application;
    8. import javafx.scene.Scene;
    9. import javafx.scene.image.ImageView;
    10. import javafx.scene.layout.StackPane;
    11. import javafx.scene.paint.Color;
    12. import javafx.scene.shape.Line;
    13. import javafx.scene.image.Image;
    14. import javafx.scene.image.ImageView;
    15. import javafx.scene.text.Text;
    16. import javafx.scene.shape.Circle;
    17. import javafx.stage.Stage;
    18. import javafx.util.Duration;
    19. public class PlayTheSlideShow extends Application {
    20. public void start(Stage primaryStage) {
    21. StackPane pane = new StackPane();
    22. String[] url = new String[53];
    23. Image[] image = new Image[53];
    24. ImageView[] imageView = new ImageView[53];
    25. for (int i = 0; i < 53; i++) {
    26. url[i] = "/porkerImage/" + (i + 1) + ".jpg";
    27. image[i] = new Image(url[i]);
    28. imageView[i] = new ImageView(image[i]);
    29. }
    30. pane.getChildren().add(imageView[0]);
    31. EventHandler eventHandler = e -> {
    32. for (int i = 0; i < 53; i++)
    33. if (pane.getChildren().contains(imageView[i])) {
    34. pane.getChildren().remove(imageView[i]);
    35. pane.getChildren().add(imageView[i + 1]);
    36. break;
    37. }
    38. };
    39. Timeline animation = new Timeline(new KeyFrame(Duration.millis(1000), eventHandler));
    40. animation.setCycleCount(Timeline.INDEFINITE);
    41. animation.play();
    42. for (int i = 0; i < 53; i++)
    43. imageView[i].setOnMouseClicked(e -> {
    44. if (animation.getStatus() == Animation.Status.PAUSED)
    45. animation.play();
    46. else
    47. animation.pause();
    48. });
    49. Scene scene = new Scene(pane, 250, 200);
    50. primaryStage.setTitle("TimelineDemo");
    51. primaryStage.setScene(scene);
    52. primaryStage.show();
    53. }
    54. public static void main(String[] args) {
    55. // TODO Auto-generated method stub
    56. Application.launch(args);
    57. }
    58. }

    23、几何问題:钟摆

    编写一个程序用动画完成钟摆,如图所示。单击向上箭头 UP 键增 加速度,单击向下箭头键 DWON降低速度。单击 S 键停止动画,单击 R 键重新开始

     搞了一晚上,就是摆动不起来,算了,先睡了。

  • 相关阅读:
    深度学习之基于yolo的体育运动项目姿态估计识别计数系统
    Kubernetes弃用Docker?别误解!这份理论实战都具备的手册还得继续学习呀
    ARM交叉编译
    java面试-zookeeper
    ipv6地址概述——配置ipv6
    【简说八股】Nginx、GateWay、Ribbon有什么区别?
    Python配置与测试利器:Hydra + pytest的完美结合
    代码随想录算法训练营 单调栈part03
    网络原理---拿捏应用层:HTTP协议
    【机器学习笔记】【决策树】【泰坦尼克号幸存者的预测】
  • 原文地址:https://blog.csdn.net/TaoqiBao_/article/details/127087419