javafx如何绘制一条动态的正弦波线?
在一个给定宽度和高度的AnchorPane如何绘制一条动态的正弦波线?像波浪一样一直在流动?
回答:
(Java)正弦曲线的绘制:https://blog.csdn.net/qq_43492547/article/details/107719979
Main.java:
import javafx.application.Application;import javafx.scene.Scene;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;
public class Main extends Application {
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) {
AnchorPane anchorPane = new AnchorPane();
anchorPane.setPrefSize(800, 300);
Scene scene = new Scene(anchorPane);
primaryStage.setScene(scene);
primaryStage.setTitle("Dynamic Sine Wave");
primaryStage.show();
DynamicSineWave dynamicSineWave = new DynamicSineWave(anchorPane.getPrefWidth(), anchorPane.getPrefHeight(), 50, 0.05);
anchorPane.getChildren().add(dynamicSineWave.getSineWave());
dynamicSineWave.startAnimation();
}
}
DynamicSineWave.java:
import javafx.animation.PathTransition;import javafx.scene.paint.Color;
import javafx.scene.shape.*;
import javafx.util.Duration;
public class DynamicSineWave {
private Path sineWave;
private PathTransition pathTransition;
public DynamicSineWave(double width, double height, double amplitude, double frequency) {
sineWave = createSineWave(width, height, amplitude, frequency);
sineWave.setStroke(Color.BLUE);
sineWave.setStrokeWidth(2);
setupAnimation();
}
private Path createSineWave(double width, double height, double amplitude, double frequency) {
Path path = new Path();
path.getElements().add(new MoveTo(0, height / 2));
for (double x = 0; x <= width; x++) {
double y = height / 2 + amplitude * Math.sin(frequency * x);
path.getElements().add(new LineTo(x, y));
}
return path;
}
private void setupAnimation() {
pathTransition = new PathTransition();
pathTransition.setPath(sineWave);
pathTransition.setNode(sineWave);
pathTransition.setDuration(Duration.seconds(2));
pathTransition.setCycleCount(PathTransition.INDEFINITE);
pathTransition.setAutoReverse(false);
}
public Path getSineWave() {
return sineWave;
}
public void startAnimation() {
pathTransition.play();
}
}
以上是 javafx如何绘制一条动态的正弦波线? 的全部内容, 来源链接: utcz.com/p/945168.html