Css javafx2.X-动画背景和动画控件
几天前,我开始学习JavaFX,突然想到要做两个实验。首先,我想知道是否有可能在用户界面后面放置动画背景。我已经成功地创建了一个动画背景,现在我有很大的困难在我的界面中间放置一些控件。 我想给你们介绍我节目的两张照片。第一个演示了我得到的不理想结果: 我相信这是我的节点树: 这是我的申请代码:Css javafx2.X-动画背景和动画控件,css,animation,javafx,Css,Animation,Javafx,几天前,我开始学习JavaFX,突然想到要做两个实验。首先,我想知道是否有可能在用户界面后面放置动画背景。我已经成功地创建了一个动画背景,现在我有很大的困难在我的界面中间放置一些控件。 我想给你们介绍我节目的两张照片。第一个演示了我得到的不理想结果: 我相信这是我的节点树: 这是我的申请代码: public class AnimatedBackground extends Application { // ###########################################
public class AnimatedBackground extends Application
{
// #########################################################################################################
// MAIN
// #########################################################################################################
public static void main(String[] args)
{
Application.launch(args);
}
// #########################################################################################################
// INSTÂNCIAS
// #########################################################################################################
private Group root;
private Group grp_hexagons;
private Rectangle rect_background;
private Scene cenario;
// UI
private VBox lay_box_controls;
private Label lab_test;
private TextArea texA_test;
private Button bot_test;
// #########################################################################################################
// INÍCIO FX
// #########################################################################################################
@Override public void start(Stage stage) throws Exception
{
this.confFX();
cenario = new Scene(this.root , 640 , 480);
this.rect_background.widthProperty().bind(this.cenario.widthProperty());
this.rect_background.heightProperty().bind(this.cenario.heightProperty());
stage.setScene(cenario);
stage.setTitle("Meu programa JavaFX - R.D.S.");
stage.show();
}
protected void confFX()
{
this.root = new Group();
this.grp_hexagons = new Group();
// Initiate the circles and all animation stuff.
for(int cont = 0 ; cont < 15 ; cont++)
{
Circle circle = new Circle();
circle.setFill(Color.WHITE);
circle.setEffect(new GaussianBlur(Math.random() * 8 + 2));
circle.setOpacity(Math.random());
circle.setRadius(20);
this.grp_hexagons.getChildren().add(circle);
double randScale = (Math.random() * 4) + 1;
KeyValue kValueX = new KeyValue(circle.scaleXProperty() , randScale);
KeyValue kValueY = new KeyValue(circle.scaleYProperty() , randScale);
KeyFrame kFrame = new KeyFrame(Duration.millis(5000 + (Math.random() * 5000)) , kValueX , kValueY);
Timeline linhaT = new Timeline();
linhaT.getKeyFrames().add(kFrame);
linhaT.setAutoReverse(true);
linhaT.setCycleCount(Animation.INDEFINITE);
linhaT.play();
}
this.rect_background = new Rectangle();
this.root.getChildren().add(this.rect_background);
this.root.getChildren().add(this.grp_hexagons);
// UI
this.lay_box_controls = new VBox();
this.lay_box_controls.setSpacing(20);
this.lay_box_controls.setAlignment(Pos.CENTER);
this.bot_test = new Button("CHANGE POSITIONS");
this.bot_test.setAlignment(Pos.CENTER);
this.bot_test.setOnAction(new EventHandler<ActionEvent>()
{
@Override public void handle(ActionEvent e)
{
for(Node hexagono : grp_hexagons.getChildren())
{
hexagono.setTranslateX(Math.random() * cenario.getWidth());
hexagono.setTranslateY(Math.random() * cenario.getHeight());
}
}
});
this.texA_test = new TextArea();
this.texA_test.setText("This is just a test.");
this.lab_test = new Label("This is just a label.");
this.lab_test.setTextFill(Color.WHITE);
this.lab_test.setFont(new Font(32));
this.lay_box_controls.getChildren().add(this.lab_test);
this.lay_box_controls.getChildren().add(this.texA_test);
this.lay_box_controls.getChildren().add(this.bot_test);
this.root.getChildren().add(this.lay_box_controls);
}
}
public类AnimatedBackground扩展应用程序
{
// #########################################################################################################
//主要
// #########################################################################################################
公共静态void main(字符串[]args)
{
应用程序启动(args);
}
// #########################################################################################################
//印度科学院
// #########################################################################################################
私有组根;
私有组grp_六边形;
私有矩形矩形背景;
私人场景塞纳里奥;
//用户界面
专用VBox布局盒控件;
自有品牌实验室测试;
私人TextArea texA_测试;
专用按钮bot_测试;
// #########################################################################################################
//INÍCIO FX
// #########################################################################################################
@覆盖公共无效开始(阶段)引发异常
{
这个。confFX();
塞纳里奥=新场景(this.root,640480);
this.rect_background.widthProperty().bind(this.cenario.widthProperty());
this.rect_background.heightProperty().bind(this.cenario.heightProperty());
第二阶段(塞纳里奥);
stage.setTitle(“Meu programa JavaFX-R.D.S.”;
stage.show();
}
受保护的void confFX()
{
this.root=新组();
this.grp_hexagons=新组();
//启动圆圈和所有动画。
对于(int cont=0;cont<15;cont++)
{
圆圈=新圆圈();
圆形。设置填充(颜色。白色);
circle.setEffect(新的GaussianBlur(Math.random()*8+2));
circle.setOpacity(Math.random());
圆半径(20);
this.grp_hexagons.getChildren().add(圆);
双随机标度=(Math.random()*4)+1;
KeyValue kValueX=新的KeyValue(circle.scaleProperty(),randScale);
KeyValue kValue=新的KeyValue(circle.scaleYProperty(),randScale);
关键帧kFrame=新关键帧(Duration.millis(5000+(Math.random()*5000)),kValueX,kValueY);
时间线linhaT=新时间线();
linhaT.getKeyFrames().add(kFrame);
linhaT.setAutoReverse(真);
linhaT.setCycleCount(Animation.unfinite);
林哈特。play();
}
this.rect_background=新矩形();
this.root.getChildren().add(this.rect_背景);
this.root.getChildren().add(this.grp_hexagons);
//用户界面
this.lay_box_controls=new VBox();
该.铺箱控制.设置间距(20);
该.铺箱控制.设置对齐(位置中心);
此.bot_测试=新按钮(“改变位置”);
此.bot_测试设置对齐(位置中心);
this.bot_test.setOnAction(新的EventHandler()
{
@重写公共无效句柄(ActionEvent e)
{
对于(节点hexagono:grp_hexagons.getChildren())
{
setTranslateX(Math.random()*cenario.getWidth());
setTranslateY(Math.random()*cenario.getHeight());
}
}
});
this.texA_test=new TextArea();
this.texA_test.setText(“这只是一个测试”);
this.lab_test=新标签(“这只是一个标签”);
此.lab_test.setTextFill(颜色.白色);
这个.labu test.setFont(新字体(32));
this.lay\u box\u controls.getChildren().add(this.lab\u test);
this.lay_box_controls.getChildren().add(this.texA_test);
this.lay\u box\u controls.getChildren().add(this.bot\u test);
this.root.getChildren().add(this.lay\u box\u控件);
}
}
我试图使用StackPane作为场景图的根,但也发现了一个不想要的结果。尽管控件一直停留在窗口的中心,但随着圆圈的增长和收缩,圆圈开始向内移动,这使得一切看起来都很奇怪
我想知道的第二件事是,是否可以自定义控件,以便它们在某些事件发生时执行一些动画。尽管我们可以使用CSS更改控件的外观,但创建复杂的控件却比较困难。例如,当控件由于状态更改而更改其外观时,转换状态更改不是以动画方式进行的,而是以突然和静态的方式进行的。例如,有没有一种方法可以在按钮的状态之间设置动画?这将使用JavaFXAPI完成吗?或者这是使用CSS吗?或者无论如何都不可能
感谢您的关注。经过多次努力,我和Oracle社区的一些用户可以解决这个问题。我认为没有必要在这里重复所作的所有决议由我们提供,因此我将发布链接,以便您可以访问问题的解决方案。我希望这对我们大家都有利。无论如何,谢谢你的关注