1. 程式人生 > >Java語言程式設計 第十五章 (15.25、15.26、15.27、15.28)

Java語言程式設計 第十五章 (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 - GAP - 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;
        }
    }
}