当前位置: 代码迷 >> Web前端 >> javafx的练习题五
  详细解决方案

javafx的练习题五

热度:263   发布时间:2012-11-23 00:03:43.0
javafx的练习五

package com.meyacom.first;

import static java.lang.Math.random;
import javafx.animation.KeyFrame;
import javafx.animation.KeyValue;
import javafx.animation.Timeline;
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.paint.Color;
import javafx.scene.paint.CycleMethod;
import javafx.scene.paint.LinearGradient;
import javafx.scene.paint.Stop;
import javafx.scene.shape.Circle;
import javafx.scene.shape.Rectangle;
import javafx.scene.shape.StrokeType;
import javafx.stage.Stage;
import javafx.util.Duration;
/***
?*
?* 类名:FivthDemo
?*
?* @author 朱湘鄂
?* @Created Date:2011-7-14 @Created Time:下午03:36:33
?* @Copyright? 2011-2023 MYC Corporation, All Rights Reserved.
?* 这个实例是在第
?*/
public class FivthDemo extends Application{


??? /**
??? ?* 方法用途:
??? ?* 方法名称: main
??? ?* @param args
??? ?*???? 返回类型:void
??? ?*???? 返回值说明:
??? ?*/
??? public static void main(String[] args) {
??? ??? ??? Application.launch(args);//这个方法是唯一在main方法里面调用的
??? }

??? @Override//重写start方法
??? public void start(Stage arg0) throws Exception {
??? ??? Group root = new Group();//创建一个根节点
??? ???
??? ??? Scene scene = new Scene(root,800,600,Color.BLUE);//背景的颜色
??? ??? arg0.setScene(scene);
??? ???
??? ??? Group cycle = new Group();
??? ??? for(int i=0;i<30;i++){
??? ??? ??? Circle circle = new Circle(150,Color.web("red",0.05));
??? ??? ??? circle.setStrokeType(StrokeType.OUTSIDE);
??? ??? ??? circle.setStroke(Color.web("white",0.16));
??? ??? ??? circle.setStrokeWidth(4);
??? ??? ??? cycle.getChildren().add(circle);
??? ??? }
??? ??? //cycle.setEffect(new BoxBlur(10,10,3));//添加模糊效果
??? ???
??? ??? Rectangle colors = new Rectangle(scene.getWidth(),scene.getHeight(),//创建一个矩形的效果图
??? ??? ??? ??? new LinearGradient(0f,1f,1f,0f,true,CycleMethod.NO_CYCLE,new //从左下角0,0开始,在右下角1,1结束的一个线性填充
??? ??? ??? ??? ??? ??? Stop[]{//stop序列代表一个渐变的点
??? ??? ??? ??? ??? ??? ??? new Stop(0,Color.web("#f8bd55")),
??? ??? ??? ??? ??? ??? ??? new Stop(0.14,Color.web("#c0fe56")),
??? ??? ??? ??? ??? ??? ??? new Stop(0.28,Color.web("#5dfbc1")),
??? ??? ??? ??? ??? ??? ??? new Stop(0.43,Color.web("#64c2f8")),
??? ??? ??? ??? ??? ??? ??? new Stop(0.57,Color.web("#be4af7")),
??? ??? ??? ??? ??? ??? ??? new Stop(0.71,Color.web("#ed5fc2")),
??? ??? ??? ??? ??? ??? ??? new Stop(0.85,Color.web("#ef504c")),
??? ??? ??? ??? ??? ??? ??? new Stop(1,Color.web("#f2660f")),
??? ??? ??? ??? }));
??? ??? //root.getChildren().add(colors);//不添加渐变到容器root中
??? ??? //root.getChildren().add(cycle);//不将圆添加到背景中
??? ???
??? ??? //应用混合模式
??? ??? //Group blendModeGroup = new Group(//创建混合模式
??? ??? //??? ??? ??? ??? new Group(new Rectangle(scene.getWidth(),scene.getHeight(),Color.BLACK),cycle),colors);//该混合模式包含两个子元素
??? ??? //第一个是一个匿名的Group,第二个是上面创建的那个cycle
??? ??? //blendModeGroup.setBlendMode(BlendMode.OVERLAY);//
??? ??? //root.getChildren().add(blendModeGroup);//添加blendModeGroup的场景图
??? ???
??? ??? //添加动画,随机的移动
??? ??? Timeline timeline = new Timeline();
??? ??? for(Node circle:cycle.getChildren()){
??? ??? ??? timeline.getKeyFrames().addAll(
??? ??? ??? ??? ??? new KeyFrame(Duration.ZERO,
??? ??? ??? ??? ??? ??? ??? new KeyValue(circle.translateXProperty(),random()*800),
??? ??? ??? ??? ??? ??? ??? new KeyValue(circle.translateYProperty(),random()*600)
??? ??? ??? ??? ??? ),
??? ??? ??? ??? ??? new KeyFrame(new Duration(40000),
??? ??? ??? ??? ??? ??? ??? new KeyValue(circle.translateXProperty(),random()*800),
??? ??? ??? ??? ??? ??? ??? new KeyValue(circle.translateYProperty(),random()*600)
??? ??? ??? ??? ??? )
??? ??? ??? );
??? ??? }
??? ??? timeline.play();
??? ??? root.getChildren().add(colors);//不添加渐变到容器root中
??? ??? root.getChildren().add(cycle);//不将圆添加到背景中
??? ??? arg0.setVisible(true);//让stage可见
??? ??? arg0.setTitle("练习五--动画特效");
??? }


}

  相关解决方案