- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
因此,我受到 Daniel Shiffman 的编码挑战视频 ( https://www.youtube.com/watch?v=17WoOqgXsRM&t=328s ) 和新星球大战之一的启发,用 Java 制作了某种超空间模拟动画。我是一名初学者到中级程序员,我确实研究了我遇到的问题,但似乎找不到答案,但如果其他人发现类似的问题和答案,请随时链接到它。无论如何,我已经建立了一个已知的类作为这里的明星。
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
import javafx.scene.paint.Color;
import javafx.scene.Group;
import javafx.scene.shape.Circle;
import java.util.Timer;
import java.util.TimerTask;
import javafx.application.Platform;
import javafx.scene.canvas.Canvas;
import javafx.animation.AnimationTimer;
/**
* @author (Richard Zins)
* @version (1)
*/
public class Star extends Circle
{
double x = 0;
double y = 0;
double z = 0;
public Star()
{
x = Math.random()*800;
y = Math.random()*800;
z = Math.random()*800;
}
public void update(){
z = z - 10 ;
}
我使用它为 800X800 空间中的星星创建随机起始位置,并稍后更改这些值。另外,是的,我确实知道我没有将实例字段设为私有(private),因此当我在主程序中将星星创建为圆圈时,我可以轻松地引用它们,而无需使用我知道这不是惯例但我可以稍后更改的方法。无论如何,我的问题是在我创建星星并创建相应的圆圈之后,当我尝试通过使用我编写的更新方法更改我的 z 值来更新它们的位置,然后获取圆圈 x 值并减去新的 z 我认为它们都应该移动但他们没有。我也知道,目前如果这可行的话,它不会在视频中产生相同的效果,但我只是想让他们现在全部移动。我认为应该执行此操作的代码位于位于我的主类波纹管中的匿名内部类动画计时器中。
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
import javafx.scene.paint.Color;
import javafx.scene.Group;
import javafx.scene.shape.Circle;
import java.util.Timer;
import java.util.TimerTask;
import javafx.application.Platform;
import javafx.scene.canvas.Canvas;
import javafx.animation.AnimationTimer;
/**
* @author (Richard Zins)
* @version (1)
*/
public class Starfield extends Application
{
public static void main(String[]args)
{
launch(args);
}
@Override
public void start(Stage primaryStage) throws Exception{
primaryStage.setTitle("Starfield Simulation");
Group layout = new Group();
Canvas canvas = new Canvas(800,800);
layout.getChildren().add(canvas);
//puts all the stars onto the scene
Star[] stars = new Star[100];
for(int i = 0;i < stars.length;i++){
stars[i] = new Star();
}
Circle[] circles = new Circle[100];
for(int i = 0;i < stars.length;i++){
for(int r = 0;r < circles.length;r++){
layout.getChildren().add(circles[r] = new Circle(stars[i].x,stars[i].y,5,Color.WHITE));
}
}
//going to handle moving the stars with animation timer
final long startNanoTime = System.nanoTime();
new AnimationTimer(){
public void handle(long currentNanoTime){
for(int i = 0;i < stars.length;i++){
for(int r = 0;r < circles.length;r++){
stars[i].update();
circles[r].setCenterX(stars[i].x - stars[i].z);
circles[r].setCenterY(stars[i].y - stars[i].z);
}
}
}
} .start();
Scene sim = new Scene(layout,800,800,Color.BLACK);
primaryStage.setScene(sim);
primaryStage.show();
}
}
我将不胜感激任何有关此问题的帮助或其他方法的建议或只是对我的代码的总体建议。
最佳答案
JavaFX 使用的默认相机是 ParallelCamera 。如果在使用平行相机时更新 z 坐标,用户将无法感知和移动。
对于一般代码审查,http://codereview.stackexchange.com更好(请确保您在发布之前阅读 asking)。
import javafx.animation.AnimationTimer;
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.PerspectiveCamera;
import javafx.scene.Scene;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.stage.Stage;
public class Starfield extends Application {
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage stage) throws Exception {
Group layout = new Group();
Star[] stars = createStars(layout);
animateStars(stars);
Scene sim = new Scene(layout, 800, 800, true);
sim.setFill(Color.BLACK);
sim.setCamera(new PerspectiveCamera());
stage.setScene(sim);
stage.show();
}
private void animateStars(final Star[] stars) {
new AnimationTimer() {
public void handle(long currentNanoTime) {
for (Star star : stars) {
star.update();
}
}
}.start();
}
private Star[] createStars(Group layout) {
Star[] stars = new Star[100];
for (int i = 0; i < stars.length; i++) {
stars[i] = new Star();
layout.getChildren().add(
stars[i]
);
}
return stars;
}
public class Star extends Circle {
public Star() {
super(5, Color.WHITE);
setTranslateX(Math.random() * 800);
setTranslateY(Math.random() * 800);
setTranslateZ(Math.random() * 800);
}
public void update() {
double newTranslateZ = getTranslateZ() - 10;
if (newTranslateZ < -2000) {
newTranslateZ = Math.random() * 800;
}
setTranslateZ(newTranslateZ);
}
}
}
我添加了 -2000 z 的模糊因子,以便在星星靠近相机时回收它们。这不是很精确,可以使用一些数学来 determine if the point remains within the field of view在它被回收之前。一些math for such calculations记录在 Camera 类中。 node.computeAreaInScreen()
应该能够帮助进行这样的计算,但它没有像我预期的那样工作。
其他问题的答案
follow up question this might be a silly question but why did you make the animateStars method and createStars method private?
只是习惯,在这种情况下,方法是否私有(private)并不重要。对于较大的程序,它可以使代码更容易推理和分析,因为在检查公共(public)接口(interface)时可以忽略私有(private)细节。这是information hiding的OOP原则。其他原则如immutable objects还可以使事情更容易推理(特别是在编写并发代码时)。
注意Java的默认设置很差,项目默认应该是私有(private)的和最终的,这将使Java程序不那么冗长,也鼓励人们编写更容易推理的程序。较新的语言,例如 Ceylon使用更合理的默认值。
关于java - 我的 javafx 动画计时器似乎没有改变我正在更新的圆圈的位置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41203121/
我需要移动一个对象,在我的例子中是给定路径上的一个字符串。实际上路径可以是半圆。如附图所示,字符串应该出现在另一个物体的后面,沿着路径移动并消失在第二个物体(两个图像)后面。我不知道如何开始...希望
我需要构建一个圆形(在 css 中),它有 2 行文本,可以根据选择的翻译改变长度并始终居中。 到目前为止我有这个: h3 { background-color: #fcd141; borde
是否可以在 CSS3 中使用 -webkit-border-radius 绘制一个圆,同时将宽度和高度限制为特定变量(例如 height:100px 和 width:100px) 所以当在圆圈内添加文
我正在尝试在方形图像上叠加一个圆圈。文本需要在圆圈中水平和垂直居中。 我几乎用正方形 div 做对了,但是一旦我将图像放入组合中,圆圈就会移动到图像下方。 我的代码。 .Container { w
使用 CSS,我有一个将图标放置在圆圈/圆盘中的显示。 这是我的例子: 但我很难将图标放在圆圈/圆盘的中心。 我已经搜索过 SO(找到 this post 但更改行高只会扩展圆盘/圆圈)和 Googl
我正在尝试在一行中制作几个带有文本的 css 圆圈。当我使用 circle 类来 img 时,圆圈是内联的,但我无法添加任何文本。当我使用 circle class 到 div 时,我可以添加文本,但
我尝试在单击“提交”按钮时显示 ProgessBar。它会在数据加载完成时隐藏。但是,progressBar 没有覆盖整个屏幕。相反,它被按钮覆盖。请引用屏幕截图,它应该更容易理解我的意思。 我要实现
这个问题在这里已经有了答案: Circle with two borders (4 个答案) 关闭 7 年前。 我有一个只有一个边框的圆圈,但我想知道是否有办法实现一个有两个不同颜色边框的圆圈。我有
我正尝试按照以下示例在 CSS 中创建一个带有镶嵌边框的圆圈: 我有以下 HTML 和 CSS,但它没有产生我需要的效果: .inlay-circle { width: 15rem; heig
我找到的每个指南都有相同的线条和填充颜色。我想要的只是一个带有红线和白色填充的圆圈。 我试过: .circle { border: red; background-color: #FF
我正在寻找一种用纯色和图像填充 SVG 圆圈的方法。 我现在尝试的是使用这段代码: 它用我的背景图片绘制
我目前正在组建一个将托管用户的网站。每个用户都会有一个个人资料页面,该页面将显示 SVG 圆数组,每个用户在数据库的用户表中自己的行中也有一个相应的 SVG_number。 例如,如果 User1 在
我正在尝试在 SVG 中创建三组圆圈。我给他们打电话circleA circleB和circleC我打算给它们涂上不同的颜色。 var circleA = [ [50,48],[106,35]
使用 snapsvg.io,我想知道是否可以添加可点击的链接,例如 My Link标记到 SVG 文本、圆圈或线条。 我这里的一个例子是文本: var s = Snap("#svg"); var te
所以这是我的一个小项目,只是为了好玩。我尝试使用 libgdx 在 Java 中重新创建随机 Walker。 现在我认为我的代码非常成功,因为它工作正常(也许)。 但是有一个问题,圆比其他轴更倾向于向
我想在 CSS 中创建一个在右边和底部有线条的圆。类似下图。我找到了一个 css code水平连接圆圈。我不知道如何垂直添加线条或类似于我附加的图像?
以下圆形标签位于标签内: 现在我必须将生成的圆圈附加到标签内,例如 更新: function createCircle(a) { var circle = document.
我希望圆圈类似于饼图,具有相同的不同颜色切片。出于某种原因,我画了一个圆,在带有颜色的一侧有弧形,中间有一个白色八边形。 for(var i=0;i<8;i++){ ctx.beginPath
我有一个场景,我必须在样条图中创建标记/圆圈。我使用 highcharts 创建了样条图表,图表的代码如下。 我的输出应该如下所示。我已经在图像中标记了预期的圆圈: $(function ()
给定以下示例: 是否有可能检测到网络中的环路 (I1, I2,I3, C6, C7, I5)? 我试过:simple_cycles → 它适用于 3 个节点,但不能超过 3 个。 我需要检测包含所有节
我是一名优秀的程序员,十分优秀!