您的位置:首页 > 编程语言 > Java开发

Java语言程序设计 第十五章 (15.25、15.26、15.27、15.28)

2018-02-06 21:20 405 查看
程序小白,希望和大家多交流,共同学习





15.25

//小球沿着正弦图像移动

import javafx.application.Application;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.paint.Color;
import javafx.scene.shape.Polyline;
import javafx.collections.ObservableList;
import javafx.scene.shape.Line;
import javafx.scene.text.Text;
import javafx.scene.layout.Pane;
import javafx.scene.shape.Circle;
import javafx.animation.PathTransition;
import javafx.animation.Timeline;
import javafx.util.Duration;
import javafx.scene.input.MouseButton;

public class SinBall extends Application
{
@Override
public void start(Stage primaryStage)
{
double width = 1000;
double height = 500;

final double PILEN = 100;
final double A = height / 4;
final double GAP = 20;

Pane pane = new Pane();
//画坐标
Line x = new Line(GAP, height / 2, width - GAP, height / 2);
Text textX  = new Text(width - GAP, height / 2 - 5, "x");
Polyline directX = new Polyline();
ObservableList<Double> listX = directX.getPoints();
listX.add(width - GAP - 10);
listX.add(height / 2 - 5);
listX.add(width - GAP);
listX.add(height / 2);
listX.add(width - G
11b2a
AP - 10);
listX.add(height / 2 + 5);

Line y = new Line(width / 2, height - GAP, width / 2, GAP);
Text textY = new Text(width / 2 + 10, GAP, "y");
Polyline directY = new Polyline();
ObservableList<Double> listY = directY.getPoints();
listY.add(width / 2 - 5);
listY.add(GAP + 10);
listY.add(width / 2);
listY.add(GAP);
listY.add(width / 2 + 5);
listY.add(GAP + 10);
pane.getChildren().addAll(x, textX, directX, y, textY, directY);

//画正弦图形
Polyline sinShape = new Polyline();
sinShape.setStroke(Color.RED);
ObservableList<Double> sinList = sinShape.getPoints();
int rightX = (int)((width - GAP) / 2 - GAP);
for (int i = -rightX; i < rightX; i++)
{
sinList.add(i + width / 2);
sinList.add((height / 2) - A * Math.sin((i / PILEN) * Math.PI));
}
pane.getChildren().add(sinShape);

//显示PI("\u03c0")
int num = (int)((width / 2 - GAP - 10) / PILEN);
Text coordinate = new Text();
for (int i = -num; i < num; i++)
{
if (i == 1)
{
coordinate = new Text((width / 2 + i * PILEN), (height / 2 + 10), "\u03c0");
}
else if (i == -1)
{
coordinate = new Text((width / 2 + i * PILEN), (height / 2 + 10), "-\u03c0");
}
else if (i == 0)
{
coordinate = new Text((width / 2 + i * PILEN), (height / 2 + 10), "0");
}
else
coordinate = new Text((width / 2 + i * PILEN), (height / 2 + 10), i + "\u03c0");
pane.getChildren().add(coordinate);
}

Circle circle = new Circle(10);
circle.setFill(Color.GOLD);
pane.getChildren().add(circle);
PathTransition animation = new PathTransition();
animation.setDuration(Duration.millis(10000));
animation.setPath(sinShape);
animation.setNode(circle);
animation.setCycleCount(Timeline.INDEFINITE);
animation.play();

pane.setOnMouseClicked(e -> {
if (e.getButton() == MouseButton.PRIMARY)
{
animation.pause();
}
else if (e.getButton() == MouseButton.SECONDARY)
{
animation.play();
}
});

Scene scene = new Scene(pane);
primaryStage.setTitle("ShowSinCos");
primaryStage.setScene(scene);
primaryStage.show();
}
}


15.26

//来回摆动的圆,高处颜色变浅,低处颜色变深
import javafx.application.Application;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.layout.Pane;
import javafx.scene.shape.Line;
import javafx.scene.shape.Arc;
import javafx.scene.shape.ArcType;
import javafx.scene.shape.Circle;
import javafx.scene.paint.Color;
import javafx.animation.Timeline;
import javafx.animation.PathTransition;
import javafx.util.Duration;
import javafx.scene.input.MouseButton;
import javafx.animation.FadeTransition;

public class Swing_FadeBall extends Application
{
@Override
public void start(Stage primaryStage)
{
Pane pane = new Pane();
Circle ball  = new Circle(10);
ball.setFill(Color.RED);
ball.setStroke(Color.BLACK);
//小球颜色变化的动画
FadeTransition fadeBall = new FadeTransition(Duration.millis(500), ball);
fadeBall.setFromValue(0.2);
fadeBall.setToValue(1.0);
fadeBall.setAutoReverse(true);
fadeBall.setCycleCount(Timeline.INDEFINITE);
//小球的路径
Arc path = new Arc(300, 200, 80, 60, 0, -180);
path.setType(ArcType.OPEN);
path.setFill(Color.WHITE);
path.setStroke(Color.BLACK);
pane.getChildren().addAll(path, ball);
//小球移动的动画
PathTransition animation = new PathTransition();
animation.setDuration(Duration.millis(1000));
animation.setCycleCount(Timeline.INDEFINITE);
animation.setPath(path);
animation.setNode(ball);
animation.setAutoReverse(true);
animation.play();
fadeBall.play();
//鼠标事件
pane.setOnMouseClicked(e -> {
if (e.getButton() == MouseButton.PRIMARY)
{
animation.pause();
fadeBall.pause();
}
else if (e.getButton() == MouseButton.SECONDARY)
{
animation.play();
fadeBall.play();
}
});

Scene scene = new Scene(pane, 600, 300);
primaryStage.setTitle("SwingBall");
primaryStage.setScene(scene);
primaryStage.show();
}
}


15.27

//移动的文本
import javafx.application.Application;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.layout.Pane;
import javafx.scene.text.Text;
import javafx.scene.text.Font;
import javafx.scene.paint.Color;
import javafx.scene.shape.Line;
import javafx.util.Duration;
import javafx.animation.Timeline;
import javafx.animation.PathTransition;

public class MoveText extends Application
{
@Override
public void start(Stage primaryStage)
{
double height = 100;
double width = 600;
Pane pane = new Pane();
//文本路径
Line line = new Line();
line.setStartX(0);
line.setStartY(height / 2);
line.setEndX(width);
line.setEndY(height / 2);
//文本
Text text = new Text("Programming is fun");
text.setStroke(Color.RED);
text.setFont(Font.font("MyFont", 20));
pane.getChildren().add(text);
//动画
PathTransition animation = new PathTransition();
animation.setDuration(Duration.millis(5000));
animation.setPath(line);
animation.setNode(text);
animation.setCycleCount(Timeline.INDEFINITE);
animation.play();
//鼠标事件
text.setOnMousePressed(e -> {
animation.pause();
});
text.setOnMouseReleased(e -> {
animation.play();
});

primaryStage.setResizable(false);
Scene scene = new Scene(pane, width, height);
primaryStage.setTitle("MoveText");
primaryStage.setScene(scene);
primaryStage.show();
}
}


15.28

//三个按钮控制风扇

import javafx.application.Application;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.layout.Pane;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.geometry.Pos;
import javafx.scene.shape.Circle;
import javafx.scene.shape.Arc;
import javafx.scene.shape.ArcType;
import javafx.scene.paint.Color;
import javafx.scene.control.Button;
import javafx.animation.PathTransition;
import javafx.animation.Timeline;
import javafx.util.Duration;
import javafx.animation.KeyFrame;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;

public class RotateFang extends Application
{
private double rotate = 10;
private boolean direction = true;
@Override
public void start(Stage primaryStage)
{
double rate = 20;
//绘制面板内图形
//1.画三个按钮
HBox buttonPane = new HBox(10);
buttonPane.setAlignment(Pos.CENTER);
Button btPause = new Button("Pause");
Button btResume = new Button("Resume");
Button btReverse = new Button("Reverse");
buttonPane.getChildren().addAll(btPause, btResume, btReverse);
//2.画风扇
Pane fanPane = new Pane();
Circle fan = new Circle(200, 200, 110);
fan.setFill(Color.WHITE);
fan.setStroke(Color.BLACK);
Arc a1 = new Arc(200, 200, 100, 100, 0, 30);
a1.setType(ArcType.ROUND);
a1.setFill(Color.BLACK);
Arc a2 = new Arc(200, 200, 100, 100, 90, 30);
a2.setType(ArcType.ROUND);
a2.setFill(Color.BLACK);
Arc a3 = new Arc(200, 200, 100, 100, 180, 30);
a3.setType(ArcType.ROUND);
a3.setFill(Color.BLACK);
Arc a4 = new Arc(200, 200, 100, 100, 270, 30);
a4.setType(ArcType.ROUND);
a4.setFill(Color.BLACK);
fanPane.getChildren().addAll(fan, a1, a2, a3, a4);

//绘制动画
EventHandler<ActionEvent> eventHandler = e -> {
fanPane.setRotate(newRotate());
};
Timeline rotateFan = new Timeline(new KeyFrame(Duration.millis(100), eventHandler));
rotateFan.setCycleCount(Timeline.INDEFINITE);
rotateFan.play();

btPause.setOnAction(e -> {
rotateFan.pause();
});

btResume.setOnAction(e -> {
rotateFan.play();
});

btReverse.setOnAction(e -> {
direction = !direction;//改变当前的旋转方向
});
//画入主面板
BorderPane pane = new BorderPane();
pane.setCenter(fanPane);
pane.setBottom(buttonPane);

//System.out.println(pane.widthProperty().getValue() + " " + pane.heightProperty().getValue());
primaryStage.setResizable(false);
Scene scene = new Scene(pane, 390, 415);
primaryStage.setTitle("RotateFan");
primaryStage.setScene(scene);
primaryStage.show();
}
//确定新的旋转方向
public double newRotate()
{
if (direction)
{
System.out.println("true");
return rotate = (rotate + 10) % 360;
}
else
{
System.out.println("false");
return rotate = (rotate - 10) % 360;
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: