目录
请写一个程序,可以让用户通过单击 Refiesh 按钮以显示从一副52 张卡牌选取的 4 张卡牌,如图所示。
代码
- package EventDrivenAndAnimation_Practice;
-
- import java.util.ArrayList;
- import java.util.Arrays;
- import javafx.application.Application;
- import javafx.geometry.Pos;
- import javafx.scene.Scene;
- import javafx.scene.control.Button;
- import javafx.scene.image.Image;
- import javafx.scene.image.ImageView;
- import javafx.scene.layout.HBox;
- import javafx.scene.layout.GridPane;
- import javafx.scene.layout.HBox;
- import javafx.scene.layout.BorderPane;
- import javafx.scene.paint.Color;
- import javafx.scene.shape.Circle;
- import javafx.stage.Stage;
- import javafx.event.ActionEvent;
- import javafx.event.EventHandler;
-
- public class ShowFourCards extends Application {
- // 创建一个PokerPane
- private PorkerPane porkerPane = new PorkerPane();
-
- public void start(Stage primaryStage) {
- // 新建hbox用于存放button
- HBox hbox = new HBox();
- hbox.setSpacing(10);
- hbox.setAlignment(Pos.CENTER);
-
- //新建一个button
- Button btReflesh = new Button("Refresh");
- hbox.getChildren().add(btReflesh);
-
- //为reflesh按钮注册处理器
- btReflesh.setOnAction(new RefleshHandler());
-
- //把hbox porkerpane放入面板
- BorderPane borderPane = new BorderPane();
- borderPane.setCenter(porkerPane);
- borderPane.setBottom(hbox);
- BorderPane.setAlignment(hbox, Pos.CENTER);
-
- Scene scene = new Scene(borderPane, 450, 200);
- primaryStage.setTitle("ShowFourCards");
- primaryStage.setScene(scene);
- primaryStage.show();
- }
- //定义一个名为 RefleshHandler的处理器类
- class RefleshHandler implements EventHandler
{ - public void handle(ActionEvent e) {
- porkerPane.reflesh();
- }
- }
- //定义一个新的类PorkerPane用于显示扑克牌
- class PorkerPane extends GridPane {
- private Image ig1;
- private Image ig2;
- private Image ig3;
- private Image ig4;
-
- public PorkerPane() {
-
- // ig1 = ig2 = ig3 = ig4 = new Image("/porkerImage/53.jpg");
- //
- // setHgap(5);
- // add(new ImageView(ig1), 0, 0);
- // add(new ImageView(ig2), 1, 0);
- // add(new ImageView(ig3), 2, 0);
- // add(new ImageView(ig4), 3, 0);
- }
-
- //提供reflesh方法用于显示随机的四张牌
- public void reflesh() {
- Integer array[] = new Integer[52];
- for (int i = 1; i <= 52; i++)
- array[i - 1] = i;
- ArrayList
list = new ArrayList<>(Arrays.asList(array)); - java.util.Collections.shuffle(list);
-
- String url1 = "/porkerImage/" + list.get(0) + ".jpg";
- String url2 = "/porkerImage/" + list.get(1) + ".jpg";
- String url3 = "/porkerImage/" + list.get(2) + ".jpg";
- String url4 = "/porkerImage/" + list.get(3) + ".jpg";
-
- ig1 = new Image(url1);
- ig2 = new Image(url2);
- ig3 = new Image(url3);
- ig4 = new Image(url4);
-
- setHgap(5);
- add(new ImageView(ig1), 0, 0);
- add(new ImageView(ig2), 1, 0);
- add(new ImageView(ig3), 2, 0);
- add(new ImageView(ig4), 3, 0);
- }
- }
-
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- Application.launch(args);
- }
- }
运行结果

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

代码
- package EventDrivenAndAnimation_Practice;
-
- import javafx.application.Application;
- import javafx.geometry.Pos;
- import javafx.scene.Scene;
- import javafx.scene.control.Button;
- import javafx.scene.layout.HBox;
- import javafx.scene.layout.StackPane;
- import javafx.scene.layout.BorderPane;
- import javafx.scene.paint.Color;
- import javafx.scene.shape.Rectangle;
- import javafx.stage.Stage;
- import javafx.event.ActionEvent;
- import javafx.event.EventHandler;
-
- public class RotateAQuadrilateral extends Application {
-
- private QuadrilateralPane quadrilateralPane = new QuadrilateralPane();
-
- public void start(Stage primaryStage) {
- HBox hbox = new HBox();
- hbox.setSpacing(10);
- hbox.setAlignment(Pos.CENTER);
-
- Button btRotate = new Button("Rotate");
- hbox.getChildren().add(btRotate);
-
- btRotate.setOnAction(new RotateHandler());
-
- BorderPane borderPane = new BorderPane();
- borderPane.setCenter(quadrilateralPane);
- borderPane.setBottom(hbox);
- BorderPane.setAlignment(hbox, Pos.CENTER);
-
- Scene scene = new Scene(borderPane, 200, 150);
- primaryStage.setTitle("RotateAQuadrilateral");
- primaryStage.setScene(scene);
- primaryStage.show();
- }
-
- class RotateHandler implements EventHandler
{ - public void handle(ActionEvent e) {
- quadrilateralPane.rotate();
- }
- }
-
- class QuadrilateralPane extends StackPane {
- private Rectangle rectangle = new Rectangle(50, 80);
-
- public QuadrilateralPane() {
- getChildren().add(rectangle);
- rectangle.setStroke(Color.BLACK);
- rectangle.setFill(Color.GOLD);
- }
-
- public void rotate() {
- rectangle.setRotate(rectangle.getRotate() + 15);
- }
- }
-
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- Application.launch(args);
- }
-
- }
编写一个程序,在面板上移动小球。应该定义一个面板类来显示小球,并提供向左、 向右、向上和向下移动小球的方法,如图所示。请进行边界检査以防止球完全移到视线外。

问题分析:一开始把BallPane类定义成了
class BallPane extends StackPane
发现小球无法移动。改为
class BallPane extends Pane
程序正常运行
代码
- package EventDrivenAndAnimation_Practice;
-
- import javafx.application.Application;
- import javafx.geometry.Pos;
- import javafx.scene.Scene;
- import javafx.scene.control.Button;
- import javafx.scene.layout.HBox;
- import javafx.scene.layout.Pane;
- import javafx.scene.layout.BorderPane;
- import javafx.scene.paint.Color;
- import javafx.scene.shape.Circle;
- import javafx.stage.Stage;
- import javafx.event.ActionEvent;
- import javafx.event.EventHandler;
-
- public class MoveBall extends Application {
-
- private BallPane ballPane = new BallPane();
-
- public void start(Stage primaryStage) {
- HBox hbox = new HBox();
- hbox.setSpacing(10);
- hbox.setAlignment(Pos.CENTER);
-
- Button btLeft = new Button("Left");
- Button btRight = new Button("Right");
- Button btUp = new Button("Up");
- Button btDown = new Button("Down");
-
- hbox.getChildren().addAll(btLeft, btRight, btUp, btDown);
-
- btLeft.setOnAction((ActionEvent e) -> {
- ballPane.left();
- });
- btRight.setOnAction((ActionEvent e) -> {
- ballPane.right();
- });
- btUp.setOnAction((ActionEvent e) -> {
- ballPane.up();
- });
- btDown.setOnAction((ActionEvent e) -> {
- ballPane.down();
- });
-
- BorderPane borderPane = new BorderPane();
- borderPane.setCenter(ballPane);
- borderPane.setBottom(hbox);
- BorderPane.setAlignment(hbox, Pos.CENTER);
-
- Scene scene = new Scene(borderPane, 500, 150);
- primaryStage.setTitle("MoveBall");
- primaryStage.setScene(scene);
- primaryStage.show();
- }
-
- // 定义BallPane用于放置小球
- class BallPane extends Pane {
- private Circle circle = new Circle(50,50,30);
-
- public BallPane() {
- getChildren().add(circle);
- circle.setStroke(Color.BLACK);
- circle.setFill(Color.GREY);
- }
-
- // 提供四个方法用于移动小球
- public void left() {
- circle.setCenterX(circle.getCenterX() > 10 ? circle.getCenterX() - 10 : 0);
- }
-
- public void right() {
- circle.setCenterX(circle.getCenterX() < this.getWidth() - 10 ? circle.getCenterX() + 10 : 0);
- }
-
- public void up() {
- circle.setCenterY(circle.getCenterY() > 10 ? circle.getCenterY() - 10 : 0);
- }
-
- public void down() {
- circle.setCenterY(circle.getCenterY() < this.getHeight() - 10 ? circle.getCenterY() + 10 : 0);
- }
- }
-
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- Application.launch(args);
- }
-
- }
运行结果

编写一个程序完成加法、减法、乘法和除法操作
代码
- package EventDrivenAndAnimation_Practice;
-
- import javafx.application.Application;
- import javafx.geometry.Pos;
- import javafx.geometry.HPos;
- import javafx.scene.Scene;
- import javafx.scene.control.Button;
- import javafx.scene.control.Label;
- import javafx.scene.control.TextField;
- import javafx.scene.layout.GridPane;
- import javafx.scene.layout.HBox;
- import javafx.scene.paint.Color;
- import javafx.stage.Stage;
-
- public class Calculator extends Application {
- private TextField tfNumber1 = new TextField();
- private TextField tfNumber2 = new TextField();
- private TextField tfResult = new TextField();
-
- private Button btAdd = new Button("Add");
- private Button btSubstract = new Button("Substract");
- private Button btMultiply = new Button("Multiply");
- private Button btDivide = new Button("Divide");
-
- public void start(Stage primaryStage) {
- GridPane pane = new GridPane();
- pane.setHgap(5);
- pane.setVgap(5);
-
- pane.add(new Label("Number1:"), 0, 0);
- pane.add(tfNumber1, 1, 0);
- pane.add(new Label("Number2:"), 0, 1);
- pane.add(tfNumber2, 1, 1);
- pane.add(new Label("Result"), 0, 2);
- pane.add(tfResult, 1, 2);
-
- pane.add(btAdd, 0, 3);
- pane.add(btSubstract, 1, 3);
- pane.add(btMultiply, 0, 4);
- pane.add(btDivide, 1, 4);
-
- pane.setAlignment(Pos.CENTER);
- tfNumber1.setAlignment(Pos.BOTTOM_RIGHT);
- tfNumber2.setAlignment(Pos.BOTTOM_RIGHT);
- tfResult.setAlignment(Pos.BOTTOM_RIGHT);
-
- btAdd.setOnAction(e -> add());
- btSubstract.setOnAction(e -> substract());
- btMultiply.setOnAction(e -> multiply());
- btDivide.setOnAction(e -> divide());
-
- Scene scene = new Scene(pane, 500, 500);
- primaryStage.setTitle("Calculator");
- primaryStage.setScene(scene);
- primaryStage.show();
- }
-
- private void add() {
- double number1 = Double.parseDouble(tfNumber1.getText());
- double number2 = Double.parseDouble(tfNumber1.getText());
-
- tfResult.setText(String.format("%.2f", number1 + number2));
- }
-
- private void substract() {
- double number1 = Double.parseDouble(tfNumber1.getText());
- double number2 = Double.parseDouble(tfNumber1.getText());
-
- tfResult.setText(String.format("%.2f", number1 - number2));
- }
-
- private void multiply() {
- double number1 = Double.parseDouble(tfNumber1.getText());
- double number2 = Double.parseDouble(tfNumber1.getText());
-
- tfResult.setText(String.format("%.2f", number1 * number2));
- }
-
- private void divide() {
- double number1 = Double.parseDouble(tfNumber1.getText());
- double number2 = Double.parseDouble(tfNumber1.getText());
-
- tfResult.setText(String.format("%.2f", number1 / number2));
- }
-
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- Application.launch(args);
- }
-
- }
运行结果

编写一个程序,当单击鼠标时,面板上交替显示两个文本 “ Java is
- package EventDrivenAndAnimation_Practice;
-
- import javafx.animation.Animation;
- import javafx.animation.FadeTransition;
- import javafx.animation.Timeline;
- import javafx.animation.KeyFrame;
- import javafx.event.*;
- import javafx.application.Application;
- import javafx.scene.Scene;
- import javafx.scene.image.ImageView;
- import javafx.scene.layout.Pane;
- import javafx.scene.paint.Color;
- import javafx.scene.shape.Line;
- import javafx.scene.text.Text;
- import javafx.scene.shape.Circle;
- import javafx.stage.Stage;
- import javafx.util.Duration;
-
- public class AppearAlternately extends Application {
- public void start(Stage primaryStage) {
- Pane pane = new Pane();
-
- Text text = new Text(20, 50, "Programming is fun");
- text.setFill(Color.RED);
- pane.getChildren().add(text);
-
- EventHandler
eventHandler = e -> { - if (text.getText() == "Programming is fun")
- text.setText("Programming is powerful");
- else
- text.setText("Programming is fun");
- };
-
- Timeline animation = new Timeline(new KeyFrame(Duration.millis(500), eventHandler));
- animation.setCycleCount(Timeline.INDEFINITE);
- animation.play();
-
- text.setOnMouseClicked(e -> {
- if (animation.getStatus() == Animation.Status.PAUSED)
- animation.play();
- else
- animation.pause();
- });
- Scene scene = new Scene(pane, 250, 200);
- primaryStage.setTitle("TimelineDemo");
- primaryStage.setScene(scene);
- primaryStage.show();
- }
-
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- Application.launch(args);
- }
-
- }
编写一个程序,显示一个圆的颜色,当按下鼠标键时颜色为黑色,释放鼠
- package EventDrivenAndAnimation_Practice;
-
- import javafx.animation.Animation;
- import javafx.animation.FadeTransition;
- import javafx.animation.Timeline;
- import javafx.animation.KeyFrame;
- import javafx.event.*;
- import javafx.application.Application;
- import javafx.scene.Scene;
- import javafx.scene.image.ImageView;
- import javafx.scene.layout.StackPane;
- import javafx.scene.paint.Color;
- import javafx.scene.shape.Circle;
- import javafx.scene.text.Text;
- import javafx.scene.shape.Circle;
- import javafx.stage.Stage;
- import javafx.util.Duration;
-
- public class MouseClickChangeCircleColor extends Application {
- public void start(Stage primaryStage) {
- StackPane pane = new StackPane();
-
- Circle circle = new Circle(50);
- circle.setFill(Color.GRAY);
- pane.getChildren().add(circle);
-
-
- circle.setOnMouseClicked(e -> {
- if (circle.getFill() == Color.GRAY)
- circle.setFill(Color.BLACK);
- else
- circle.setFill(Color.GRAY);
- });
- Scene scene = new Scene(pane, 250, 200);
- primaryStage.setTitle("TimelineDemo");
- primaryStage.setScene(scene);
- primaryStage.show();
- }
-
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- Application.launch(args);
- }
-
- }
运行结果

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

代码
- package EventDrivenAndAnimation_Practice;
-
- import javafx.application.Application;
- import javafx.scene.Scene;
- import javafx.scene.layout.Pane;
- import javafx.scene.text.Text;
- import javafx.stage.Stage;
-
- public class DisplayMousePositionWhenClicked extends Application {
- public void start(Stage primaryStage) {
- Pane pane = new Pane();
- Text text = new Text();
- pane.getChildren().addAll(text);
-
- pane.setOnMouseClicked(e -> {
- text.setText("(" + e.getX() + "," + e.getY() + ")");
- text.setX(e.getX());
- text.setY(e.getY());
- });
- pane.setOnMouseReleased(event -> text.setText(""));
-
- Scene scene = new Scene(pane, 200, 200);
- primaryStage.setTitle("DisplayMousePositionWhenClicked");
- primaryStage.setScene(scene);
- primaryStage.show();
- }
-
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- Application.launch(args);
- }
-
- }
- package EventDrivenAndAnimation_Practice;
-
- import javafx.application.Application;
- import javafx.scene.Scene;
- import javafx.scene.layout.Pane;
- import javafx.scene.text.Text;
- import javafx.stage.Stage;
-
- public class DisplayMousePositionWhenPressed extends Application {
- public void start(Stage primaryStage) {
- Pane pane = new Pane();
- Text text = new Text();
- pane.getChildren().addAll(text);
-
- pane.setOnMousePressed(e -> {
- text.setText("(" + e.getX() + "," + e.getY() + ")");
- text.setX(e.getX());
- text.setY(e.getY());
- });
- pane.setOnMouseReleased(event -> text.setText(""));
-
- Scene scene = new Scene(pane, 200, 200);
- primaryStage.setTitle("DisplayMousePositionWhenClicked");
- primaryStage.setScene(scene);
- primaryStage.show();
- }
-
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- Application.launch(args);
- }
-
- }
请编写一个程序,使用箭头键绘制线段。所画的线从面板的中心开始,当敲
- package EventDrivenAndAnimation_Practice;
-
- import javafx.application.Application;
- import javafx.scene.Scene;
- import javafx.scene.layout.Pane;
- import javafx.scene.shape.Line;
- import javafx.scene.text.Text;
- import javafx.stage.Stage;
-
- public class PaintLines extends Application {
- private double width = 400 / 2, height = 400 / 2;
-
- public void start(Stage primaryStage) {
- Pane pane = new Pane();
-
- pane.setOnKeyPressed(e -> {
- switch (e.getCode()) {
- case DOWN:
- pane.getChildren().add(new Line(width, height, width, height + 50));
- height += 50;
- break;
- case UP:
- pane.getChildren().add(new Line(width, height, width, height - 50));
- height -= 50;
- break;
- case LEFT:
- pane.getChildren().add(new Line(width, height, width - 50, height));
- width -= 50;
- break;
- case RIGHT:
- pane.getChildren().add(new Line(width, height, width + 50, height));
- width += 50;
- break;
- }
-
- });
-
- Scene scene = new Scene(pane);
- primaryStage.setTitle("PaintLines");
- primaryStage.setScene(scene);
- primaryStage.show();
-
- pane.requestFocus();
- }
-
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- Application.launch(args);
- }
-
- }
运行结果

请编写一个程序,从键盘接收一个宇符串并把它显示在面板上。回车键表明字符串结束。任何时候输人一个新宇符串时都会将它显示在面板上
代码
- package EventDrivenAndAnimation_Practice;
-
- import javafx.application.Application;
- import javafx.scene.Scene;
- import javafx.scene.input.KeyCode;
- import javafx.scene.layout.StackPane;
- import javafx.scene.text.Text;
- import javafx.stage.Stage;
-
- public class ShowString extends Application {
- public void start(Stage primaryStage) {
- StackPane pane = new StackPane();
- Text text = new Text("");
-
- pane.getChildren().add(text);
-
- text.setOnKeyPressed(e -> {
- text.setText(e.getCode() == KeyCode.ENTER ? "" : text.getText() + e.getText());
- });
-
- Scene scene = new Scene(pane, 500, 500);
- primaryStage.setTitle("ShowString");
- primaryStage.setScene(scene);
- primaryStage.show();
-
- text.requestFocus();
- }
-
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- Application.launch(args);
- }
-
- }
请编写程序,可以使用箭头键向上、向下、向左、向右移动一个圆
代码
- package EventDrivenAndAnimation_Practice;
-
- import javafx.application.Application;
- import javafx.scene.Scene;
- import javafx.scene.layout.Pane;
- import javafx.scene.paint.Color;
- import javafx.scene.shape.Circle;
- import javafx.scene.text.Text;
- import javafx.stage.Stage;
-
- public class MoveCircle extends Application {
-
- public void start(Stage primaryStage) {
- Pane pane = new Pane();
-
- Circle circle = new Circle(100, 100, 50);
- circle.setFill(Color.GRAY);
- pane.getChildren().add(circle);
-
- circle.setOnKeyPressed(e -> {
- switch (e.getCode()) {
- case DOWN:
- circle.setCenterY(circle.getCenterY() + 20);
- break;
- case UP:
- circle.setCenterY(circle.getCenterY() - 20);
- break;
- case LEFT:
- circle.setCenterX(circle.getCenterX() - 20);
- break;
- case RIGHT:
- circle.setCenterX(circle.getCenterX() + 20);
- break;
- }
- });
-
- Scene scene = new Scene(pane, 400, 400);
- primaryStage.setTitle("MoveCircle");
- primaryStage.setScene(scene);
- primaryStage.show();
-
- circle.requestFocus();
- }
-
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- Application.launch(args);
- }
-
- }
请编写一个程序,让用户在面板上单击以自动创建或移去点,当用户左击鼠标时(主按钮),就创建一个点并且显示在鼠标的位置,用户还可以将鼠标移到一个点上,然后右击鼠标(次按钮)以移去这个点.

代码
- package EventDrivenAndAnimation_Practice;
-
- import javafx.application.Application;
- import javafx.scene.Scene;
- import javafx.scene.Node;
- import javafx.scene.input.MouseButton;
- import javafx.scene.layout.Pane;
- import javafx.scene.shape.Circle;
- import javafx.scene.paint.Color;
- import javafx.scene.text.Text;
- import javafx.stage.Stage;
-
- public class AddOrDeleteDots extends Application {
- public void start(Stage primaryStage) {
- Pane pane = new Pane();
-
- pane.setOnMouseClicked(e -> {
- Circle circle = new Circle(5);
-
- if (e.getButton() == MouseButton.PRIMARY) {
- circle.setFill(Color.WHITE);
- circle.setStroke(Color.BLACK);
- circle.setCenterX(e.getX());
- circle.setCenterY(e.getY());
- pane.getChildren().add(circle);
- } else
- for (Node node : pane.getChildren()) {
- Circle temp = (Circle) node;
- if (isInCircle(temp, e.getX(), e.getY())) {
- pane.getChildren().remove(temp);
- break;
- }
- }
-
- });
- Scene scene = new Scene(pane, 200, 200);
- primaryStage.setTitle("AddOrDeleteDots");
- primaryStage.setScene(scene);
- primaryStage.show();
- }
-
- private boolean isInCircle(Circle circle, double x, double y) {
- return Math.sqrt(Math.pow(x - circle.getCenterX(), 2) + Math.pow(y - circle.getCenterY(), 2)) <= circle
- .getRadius();
- }
-
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- Application.launch(args);
- }
-
- }
运行结果

请编写一个程序,显示两个分别位于(40,40)和(120,150) 的半径为10的圆,并用一条直线连接两个圆,如图所示。圆之间的距离显示在直线上。 用户可以拖动圆,圆和它上面的直线会相应移动,并且两个圆之间的距离会更新
代码
- package EventDrivenAndAnimation_Practice;
-
- import javafx.application.Application;
- import javafx.scene.Scene;
- import javafx.scene.layout.Pane;
- import javafx.scene.paint.Color;
- import javafx.scene.shape.Circle;
- import javafx.scene.shape.Line;
- import javafx.scene.text.Text;
- import javafx.stage.Stage;
-
- public class TwoMovableCircles extends Application {
- public void start(Stage primaryStage) {
- Pane pane = new Pane();
- Circle circle1 = new Circle(40, 40, 10);
- Circle circle2 = new Circle(120, 150, 10);
-
- circle1.setFill(Color.GRAY);
- circle2.setFill(Color.GRAY);
-
- Line line = new Line(40, 40, 120, 150);
-
- double x1 = circle1.getCenterX(), x2 = circle2.getCenterX();
- double y1 = circle1.getCenterY(), y2 = circle2.getCenterY();
-
- Text text = new Text((line.getStartX() + line.getEndX()) / 2, (line.getStartY() + line.getEndY()) / 2,
- distance(x1, x2, y1, y2) + "");
-
- pane.getChildren().addAll(circle1, circle2, line, text);
-
- circle1.setOnMouseDragged(e -> {
- circle1.setCenterX(e.getX());
- circle1.setCenterY(e.getY());
- line.setStartX(e.getX());
- line.setStartY(e.getY());
-
- double distance1 = distance(circle1.getCenterX(), circle2.getCenterX(), circle1.getCenterY(),
- circle2.getCenterY());
-
- text.setX((line.getStartX() + line.getEndX()) / 2);
- text.setY((line.getStartY() + line.getEndY()) / 2);
- text.setText(String.valueOf(distance1));
-
- });
- circle2.setOnMouseDragged(e -> {
- circle2.setCenterX(e.getX());
- circle2.setCenterY(e.getY());
- line.setEndX(e.getX());
- line.setEndY(e.getY());
-
- double distance2 = distance(circle1.getCenterX(), circle2.getCenterX(), circle1.getCenterY(),
- circle2.getCenterY());
-
- text.setX((line.getStartX() + line.getEndX()) / 2);
- text.setY((line.getStartY() + line.getEndY()) / 2);
- text.setText(String.valueOf(distance2));
- });
- Scene scene = new Scene(pane, 400, 400);
- primaryStage.setTitle("TwoMovableCircles");
- primaryStage.setScene(scene);
- primaryStage.show();
- }
-
- public double distance(double x1, double x2, double y1, double y2) {
- double distance = Math.sqrt(Math.pow(x1 - x2, 2) + Math.pow(y1 - y2, 2));
- return distance;
- }
-
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- Application.launch(args);
- }
-
- }
运行结果

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

代码
- package EventDrivenAndAnimation_Practice;
-
- import javafx.application.Application;
- import javafx.scene.Scene;
- import javafx.scene.layout.Pane;
- import javafx.scene.paint.Color;
- import javafx.scene.shape.Circle;
- import javafx.scene.shape.Line;
- import javafx.scene.text.Text;
- import javafx.stage.Stage;
-
- public class IsMouseInCircle extends Application {
- public void start(Stage primaryStage) {
- Pane pane = new Pane();
- Circle circle = new Circle(100, 60, 50);
- circle.setFill(Color.PURPLE);
-
- Text text = new Text();
- pane.getChildren().addAll(circle, text);
-
- pane.setOnMouseMoved(e -> {
- double distance = Math.sqrt(Math.pow(e.getX() - 100, 2) + Math.pow(e.getY() - 60, 2));
- text.setX(e.getX());
- text.setY(e.getY());
-
- if (distance <= 50) {
- text.setText("Mouse point is inside the circle");
- text.setFill(Color.BLACK);
- }
-
- else {
- text.setText("Mouse point is not inside the circle");
- text.setFill(Color.RED);
- }
-
- });
- Scene scene = new Scene(pane, 300, 300);
- primaryStage.setTitle("IsMouseInCircle");
- primaryStage.setScene(scene);
- primaryStage.show();
- }
-
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- Application.launch(args);
- }
-
- }
请编写一个程序,让用户可以在一个二维面板上动态地增加和移除 点,如图所示。当点加人和移除的时候,一个最小的边界矩形更新显示。假设每个点的半径是10像素

代码
问题分析:添加点实现了,但是删除点还实现不了......
- package EventDrivenAndAnimation_Practice;
-
- import javafx.application.Application;
- import javafx.scene.Scene;
- import javafx.scene.Node;
- import javafx.scene.input.MouseButton;
- import javafx.scene.layout.Pane;
- import javafx.scene.shape.Circle;
- import javafx.scene.shape.Rectangle;
- import javafx.scene.paint.Color;
- import javafx.scene.text.Text;
- import javafx.stage.Stage;
- import java.util.ArrayList;
- import java.util.Arrays;
-
- public class FindingBoundary extends Application {
- public void start(Stage primaryStage) {
- Pane pane = new Pane();
-
- Rectangle rectangle = new Rectangle();
- rectangle.setStroke(Color.BLACK);
- rectangle.setFill(Color.WHITE);
- rectangle.setOpacity(1);
- pane.getChildren().add(rectangle);
-
- ArrayList
listX = new ArrayList<>(); - ArrayList
listY = new ArrayList<>(); -
- pane.setOnMouseClicked(e -> {
- Circle circle = new Circle(5);
-
- if (e.getButton() == MouseButton.PRIMARY) {
- circle.setFill(Color.WHITE);
- circle.setStroke(Color.BLACK);
- circle.setCenterX(e.getX());
- circle.setCenterY(e.getY());
- pane.getChildren().add(circle);
-
- listX.add(e.getX());
- listY.add(e.getY());
- java.util.Collections.sort(listX);
- java.util.Collections.sort(listY);
-
- rectangle.setX(listX.get(0) - 5);
- rectangle.setY(listY.get(0) - 5);
- rectangle.setWidth(listX.get(listX.size() - 1) - listX.get(0) + 10);
- rectangle.setHeight(listY.get(listY.size() - 1) - listY.get(0) + 10);
-
- }
- // else {
- //
- // for (Node node : pane.getChildren()) {
- //
- // pane.getChildren().remove(rectangle);
- // Circle temp = (Circle) node;
- //
- // if (isInCircle(temp, e.getX(), e.getY())) {
- // pane.getChildren().remove(temp);
- //
- // listX.remove(e.getX());
- // listY.remove(e.getY());
- // java.util.Collections.sort(listX);
- // java.util.Collections.sort(listY);
- //
- // rectangle.setStroke(Color.BLACK);
- // rectangle.setFill(Color.WHITE);
- //
- // rectangle.setX(listX.get(0) - 5);
- // rectangle.setY(listY.get(0) - 5);
- // rectangle.setWidth(listX.get(listX.size() - 1) - listX.get(0) + 10);
- // rectangle.setHeight(listY.get(listY.size() - 1) - listY.get(0) + 10);
- // pane.getChildren().add(rectangle);
- // break;
- // }
- // }
- //
- // }
-
- });
- Scene scene = new Scene(pane, 200, 200);
- primaryStage.setTitle("FindingBoundary");
- primaryStage.setScene(scene);
- primaryStage.show();
- }
-
- private boolean isInCircle(Circle circle, double x, double y) {
- return Math.sqrt(Math.pow(x - circle.getCenterX(), 2) + Math.pow(y - circle.getCenterY(), 2)) <= circle
- .getRadius();
- }
-
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- Application.launch(args);
- }
-
- }
运行结果

请编写一个程序显示一个矩形。可以使用鼠标单击矩形内部并且拖 动(即按住鼠标移动)矩形到鼠标的位置。鼠标点成为矩形的中央
代码
- package EventDrivenAndAnimation_Practice;
-
- import javafx.application.Application;
- import javafx.scene.Scene;
- import javafx.scene.layout.Pane;
- import javafx.scene.text.Text;
- import javafx.scene.paint.Color;
- import javafx.scene.shape.Circle;
- import javafx.stage.Stage;
-
- public class MoveRectangleWithMouse extends Application {
- public void start(Stage primaryStage) {
- Pane pane = new Pane();
- Circle circle = new Circle(50,50,30);
- circle.setStroke(Color.BLACK);
- circle.setFill(Color.GRAY);
- pane.getChildren().addAll(circle);
-
- circle.setOnMouseDragged(e -> {
- circle.setCenterX(e.getX());
- circle.setCenterY(e.getY());
- });
- Scene scene = new Scene(pane, 300, 300);
- primaryStage.setTitle("MouseEventDemo");
- primaryStage.setScene(scene);
- primaryStage.show();
- }
-
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- Application.launch(args);
- }
-
- }
请编写一个程序,显示一个半径为 10 像素的实心圆,该圆放置在面板上的
代码
- package EventDrivenAndAnimation_Practice;
-
- import javafx.animation.Animation;
- import javafx.animation.FadeTransition;
- import javafx.animation.Timeline;
- import javafx.animation.KeyFrame;
- import javafx.event.*;
- import javafx.application.Application;
- import javafx.scene.Scene;
- import javafx.scene.image.ImageView;
- import javafx.scene.layout.Pane;
- import javafx.scene.paint.Color;
- import javafx.scene.shape.Circle;
- import javafx.scene.text.Text;
- import javafx.scene.shape.Circle;
- import javafx.stage.Stage;
- import javafx.util.Duration;
- import java.util.Date;
-
- public class HandAndEyeCoordination extends Application {
- int count = 0;
-
- public void start(Stage primaryStage) {
- Pane pane = new Pane();
-
- Circle circle = new Circle(Math.random() * 400, Math.random() * 400, 30);
- circle.setFill(new Color(Math.random(), Math.random(), Math.random(), Math.random()));
- pane.getChildren().add(circle);
-
- Date date1 = new Date(), date2;
- circle.setOnMouseClicked(e -> {
- count();
- circle.setCenterX(Math.random() * 400);
- circle.setCenterY(Math.random() * 400);
- circle.setFill(new Color(Math.random(), Math.random(), Math.random(), Math.random()));
- if (count == 5) {
-
- double time = new Date().getTime() - date1.getTime();
- Text text = new Text(50, 50, "Time spent is " + time + " milliseconds");
- text.setStroke(Color.BLACK);
- pane.getChildren().add(text);
- }
-
- });
-
- Scene scene = new Scene(pane, 400, 400);
- primaryStage.setTitle("HandAndEyeCoordination");
- primaryStage.setScene(scene);
- primaryStage.show();
- }
-
- public void count() {
- count++;
- }
-
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- Application.launch(args);
- }
-
- }
运行结果

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

代码
不足:实现的是长按暂停、释放停止。如果用setOnMouseClicked和setOnMouseReleased,只能实现暂停,暂未找到解决方法
- package EventDrivenAndAnimation_Practice;
-
- import javafx.animation.PathTransition;
- import javafx.animation.Timeline;
- import javafx.application.Application;
- import javafx.scene.Scene;
- import javafx.scene.layout.Pane;
- import javafx.scene.paint.Color;
- import javafx.scene.shape.Arc;
- import javafx.scene.shape.ArcType;
- import javafx.scene.shape.Circle;
- import javafx.stage.Stage;
- import javafx.util.Duration;
-
- public class SwingTheBall extends Application {
- public void start(Stage primaryStage) {
- Pane pane = new Pane();
-
- Arc arc = new Arc(100, 100, 80, 80, -40, -100);
- arc.setStroke(Color.BLACK);
- arc.setFill(new Color(0, 0, 0, 0));
- arc.setType(ArcType.OPEN);
-
- Circle circle = new Circle(15);
- // circle.setStroke(Color.BLACK);
- // circle.setFill(Color.BLACK);
-
- pane.getChildren().addAll(circle, arc);
-
- PathTransition pt = new PathTransition();
- pt.setDuration(Duration.millis(5000));
- pt.setPath(arc);
- pt.setNode(circle);
- pt.setOrientation(PathTransition.OrientationType.ORTHOGONAL_TO_TANGENT);
- pt.setCycleCount(Timeline.INDEFINITE);
- pt.setAutoReverse(true);
- pt.play();
-
- pane.setOnMousePressed(e -> pt.pause());
- pane.setOnMouseReleased(e -> pt.play());
-
- Scene scene = new Scene(pane, 300, 300);
- primaryStage.setTitle("SwingTheBallo");
- primaryStage.setScene(scene);
- primaryStage.show();
- }
-
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- Application.launch(args);
- }
-
- }
运行结果

请编写一个程序,用动画实现一个沿着正弦函数曲线移动的球,如图
代码
- package EventDrivenAndAnimation_Practice;
- import javafx.application.Application;
- import javafx.collections.ObservableList;
- import javafx.geometry.Pos;
- import javafx.scene.Scene;
- import javafx.scene.input.MouseButton;
- import javafx.scene.layout.Pane;
- import javafx.scene.paint.Color;
- import javafx.scene.shape.Circle;
- import javafx.scene.shape.Line;
- import javafx.scene.text.*;
- import javafx.stage.Stage;
- import javafx.util.Duration;
- import javafx.scene.shape.Polyline;
- import javafx.animation.PathTransition;
- import javafx.animation.Timeline;
-
- public class BallSwingAlongSineFunction extends Application{
- public void start(Stage primaryStage) {
- Pane pane = new Pane();
- Polyline polyline = new Polyline();
- pane.getChildren().add(polyline);
-
- polyline.setFill(Color.WHITE);
- polyline.setStroke(Color.PURPLE);
- ObservableList
list = polyline.getPoints(); -
- for (double y = 0; y <= 1400; y++) {
- list.add(y);
- list.add(200 - 50 * Math.sin(Math.toRadians(y - 700)));
- }
- Line x = new Line(20, 200, 1500, 200);
- Line y = new Line(700, 600, 700, 10);
- x.setStrokeWidth(3);
- x.setStroke(Color.BLACK);
- pane.getChildren().add(x);
-
- y.setStrokeWidth(3);
- y.setStroke(Color.BLACK);
- pane.getChildren().add(y);
-
- Line arrowX1 = new Line(1480, 190, 1500, 200);
- Line arrowX2 = new Line(1480, 210, 1500, 200);
- arrowX1.setStroke(Color.BLACK);
- arrowX1.setStrokeWidth(3);
- arrowX2.setStroke(Color.BLACK);
- arrowX2.setStrokeWidth(3);
-
- Line arrowY1 = new Line(690, 30, 700, 10);
- Line arrowY2 = new Line(710, 30, 700, 10);
- arrowY1.setStroke(Color.BLACK);
- arrowY1.setStrokeWidth(3);
- arrowY2.setStroke(Color.BLACK);
- arrowY2.setStrokeWidth(3);
-
- Text t1 = new Text(1480, 220, "x");
- t1.setFont((Font.font("Times Nre Roman", FontWeight.BOLD, FontPosture.ITALIC, 25)));
- Text t2 = new Text(710, 8, "y");
- t2.setFont((Font.font("Times Nre Roman", FontWeight.BOLD, FontPosture.ITALIC, 25)));
- pane.getChildren().addAll(arrowX1, arrowX2, arrowY1, arrowY2, t1, t2);
-
-
- Circle circle = new Circle(15);
- pane.getChildren().add(circle);
-
- PathTransition pt = new PathTransition();
- pt.setDuration(Duration.millis(5000));
- pt.setPath(polyline);
- pt.setNode(circle);
- pt.setOrientation(PathTransition.OrientationType.ORTHOGONAL_TO_TANGENT);
- pt.setCycleCount(Timeline.INDEFINITE);
- pt.setAutoReverse(true);
- pt.play();
-
- pane.setOnMousePressed(e -> {
- if(e.getButton() == MouseButton.PRIMARY)
- pt.pause();
- else
- pt.play();
- });
-
- Scene scene = new Scene(pane, 1600, 450);
- primaryStage.setTitle("BallSwingAlongSineFunction");
- primaryStage.setScene(scene);
- primaryStage.show();
- }
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- Application.launch(args);
- }
-
- }
运行结果

重写上一道, 当球摆动的时候改变球的透明度
只需要在原代码基础上加
- FadeTransition ft = new FadeTransition(Duration.millis(5000),circle);
- ft.setFromValue(1.0);
- ft.setToValue(0.1);
- ft.setCycleCount(Timeline.INDEFINITE);
- ft.setAutoReverse(true);
- ft.play();
-
- circle.setOnMousePressed(e->ft.pause());
- circle.setOnMouseReleased(e->ft.play());
请编写一个程序,显示一个移动的文本,如图所示。 文本从左到右循环的移动。当它消失在右侧的时候,又会从左侧再次出现。当鼠标按下的时 候,文本停滞不动,当按钮释放的时候,将继续移动
代码
- package EventDrivenAndAnimation_Practice;
-
- import javafx.animation.PathTransition;
- import javafx.animation.Timeline;
- import javafx.application.Application;
- import javafx.scene.Scene;
- import javafx.scene.layout.Pane;
- import javafx.scene.paint.Color;
- import javafx.scene.text.Text;
- import javafx.scene.shape.Line;
- import javafx.scene.shape.Circle;
- import javafx.stage.Stage;
- import javafx.util.Duration;
-
- public class MoveTextAlongLine extends Application {
- public void start(Stage primaryStage) {
- Pane pane = new Pane();
-
- Text text = new Text("Programming is fun");
- Line line = new Line(-50, 50, 350, 50);
- line.setStroke(Color.WHITE);
-
- pane.getChildren().addAll(text, line);
-
- PathTransition pt = new PathTransition();
- pt.setDuration(Duration.millis(5000));
- pt.setPath(line);
- pt.setNode(text);
- pt.setOrientation(PathTransition.OrientationType.ORTHOGONAL_TO_TANGENT);
- pt.setCycleCount(Timeline.INDEFINITE);
- pt.setAutoReverse(true);
- pt.play();
-
- pane.setOnMousePressed(e -> pt.pause());
- pane.setOnMouseReleased(e -> pt.play());
-
- text.setOnMousePressed(e -> pt.pause());
- text.setOnMouseReleased(e -> pt.play());
-
- Scene scene = new Scene(pane, 300, 150);
- primaryStage.setTitle("SwingTheBallo");
- primaryStage.setScene(scene);
- primaryStage.show();
-
- }
-
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- Application.launch(args);
- }
-
- }
运行结果

编写一个程序显示一个转动的风扇,如图所示。Pause、Resume
代码
- package EventDrivenAndAnimation_Practice;
-
- import javafx.animation.KeyFrame;
- import javafx.animation.PathTransition;
- import javafx.animation.Timeline;
- import javafx.application.Application;
- import javafx.geometry.Pos;
- import javafx.scene.Scene;
- import javafx.scene.layout.Pane;
- import javafx.scene.layout.BorderPane;
- import javafx.scene.layout.HBox;
- import javafx.scene.control.Button;
- import javafx.scene.paint.Color;
- import javafx.scene.text.Text;
- import javafx.scene.shape.Line;
- import javafx.scene.shape.Arc;
- import javafx.scene.shape.ArcType;
- import javafx.scene.shape.Circle;
- import javafx.stage.Stage;
- import javafx.util.Duration;
-
- public class ShowARatatingFan extends Application {
- private FanPane pane = new FanPane();
-
- public void start(Stage primaryStage) {
- HBox hbox = new HBox();
- hbox.setSpacing(10);
- hbox.setAlignment(Pos.CENTER);
-
- Button btPause = new Button("Pause");
- Button btResume = new Button("Resume");
- Button btReverse = new Button("Reverse");
-
- hbox.getChildren().addAll(btPause, btResume, btReverse);
-
- Timeline timeline = new Timeline(new KeyFrame(Duration.millis(20), e -> pane.resume()));
- timeline.setCycleCount(Timeline.INDEFINITE);
- timeline.play();
-
- btPause.setOnAction(e -> timeline.pause());
- btResume.setOnAction(e -> timeline.play());
- btReverse.setOnAction(e -> pane.reverse());
-
- BorderPane borderPane = new BorderPane();
- borderPane.setCenter(pane);
- borderPane.setBottom(hbox);
- BorderPane.setAlignment(hbox, Pos.CENTER);
-
- Scene scene = new Scene(borderPane);
- primaryStage.setTitle("ShowARatatingFan");
- primaryStage.setScene(scene);
- primaryStage.show();
-
- }
-
- class FanPane extends Pane {
-
- private Circle circle = new Circle(100, 70, 60);;
- private Arc[] arc = new Arc[4];
- private double startAngle = 30;
- private double increment = 5;
-
- public FanPane() {
- circle.setStroke(Color.BLACK);
- circle.setFill(Color.WHITE);
-
- getChildren().add(circle);
-
- for (int i = 0; i < 4; i++) {
- arc[i] = new Arc(100, 70, 50, 50, startAngle + 90 * i, 30);
- arc[i].setType(ArcType.ROUND);
- arc[i].setFill(Color.BLACK);
- getChildren().add(arc[i]);
- }
-
- }
-
- public void setAngle(double angle) {
- startAngle = angle;
- for (int i = 0; i < 4; i++)
- arc[i].setStartAngle(startAngle + 90 * i);
- }
-
- public void resume() {
- setAngle(startAngle + increment);
- }
-
- public void reverse() {
- increment *= -1;
- }
- }
-
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- Application.launch(args);
- }
-
- }
运行结果

25 张幻灯片都以图像文件(slide0.jpg, slide1.jpg, ....slide24.jpg)的形式存储
- package EventDrivenAndAnimation_Practice;
-
- import javafx.animation.Animation;
- import javafx.animation.FadeTransition;
- import javafx.animation.Timeline;
- import javafx.animation.KeyFrame;
- import javafx.event.*;
- import javafx.application.Application;
- import javafx.scene.Scene;
- import javafx.scene.image.ImageView;
- import javafx.scene.layout.StackPane;
- import javafx.scene.paint.Color;
- import javafx.scene.shape.Line;
- import javafx.scene.image.Image;
- import javafx.scene.image.ImageView;
- import javafx.scene.text.Text;
- import javafx.scene.shape.Circle;
- import javafx.stage.Stage;
- import javafx.util.Duration;
-
- public class PlayTheSlideShow extends Application {
- public void start(Stage primaryStage) {
- StackPane pane = new StackPane();
-
- String[] url = new String[53];
- Image[] image = new Image[53];
- ImageView[] imageView = new ImageView[53];
-
- for (int i = 0; i < 53; i++) {
- url[i] = "/porkerImage/" + (i + 1) + ".jpg";
- image[i] = new Image(url[i]);
- imageView[i] = new ImageView(image[i]);
- }
-
- pane.getChildren().add(imageView[0]);
-
- EventHandler
eventHandler = e -> { - for (int i = 0; i < 53; i++)
- if (pane.getChildren().contains(imageView[i])) {
- pane.getChildren().remove(imageView[i]);
- pane.getChildren().add(imageView[i + 1]);
- break;
- }
- };
-
- Timeline animation = new Timeline(new KeyFrame(Duration.millis(1000), eventHandler));
- animation.setCycleCount(Timeline.INDEFINITE);
- animation.play();
-
- for (int i = 0; i < 53; i++)
- imageView[i].setOnMouseClicked(e -> {
- if (animation.getStatus() == Animation.Status.PAUSED)
- animation.play();
- else
- animation.pause();
- });
- Scene scene = new Scene(pane, 250, 200);
- primaryStage.setTitle("TimelineDemo");
- primaryStage.setScene(scene);
- primaryStage.show();
- }
-
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- Application.launch(args);
- }
-
- }
编写一个程序,用动画完成钟摆,如图所示。单击向上箭头 UP 键增 加速度,单击向下箭头键 DWON降低速度。单击 S 键停止动画,单击 R 键重新开始

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