JavaFX在Eclipse 中画图应用效果

1.在安装了e(fx)clipse插件后,把JavaFx JDK目录设定下,重启Eclipse。

点new-other会出现JavaFX project界面

然后新建class文件即可。


[java]  view plain copy
  1. package HelloJavaFX;  
  2.   
  3. import javafx.application.Application;  
  4. import javafx.collections.ObservableList;  
  5. import javafx.geometry.VPos;  
  6. import javafx.scene.Group;  
  7. import javafx.scene.Node;  
  8. import javafx.scene.Scene;  
  9. import javafx.scene.effect.BlendMode;  
  10. import javafx.scene.effect.Bloom;  
  11. import javafx.scene.effect.BoxBlur;  
  12. import javafx.scene.effect.DropShadow;  
  13. import javafx.scene.effect.GaussianBlur;  
  14. import javafx.scene.effect.Light.Distant;  
  15. import javafx.scene.effect.Lighting;  
  16. import javafx.scene.effect.MotionBlur;  
  17. import javafx.scene.effect.PerspectiveTransform;  
  18. import javafx.scene.effect.Reflection;  
  19. import javafx.scene.paint.Color;  
  20. import javafx.scene.shape.Circle;  
  21. import javafx.scene.shape.Rectangle;  
  22. import javafx.scene.text.Font;  
  23. import javafx.scene.text.FontWeight;  
  24. import javafx.scene.text.Text;  
  25. import javafx.stage.Stage;  
  26.   
  27. public class HelloEffects extends Application{  
  28.   
  29.     Stage stage;  
  30.     Scene scene;  
  31.       
  32.     public static void main(String[] args) {  
  33.         launch(args);  
  34.   
  35.     }  
  36.   
  37.     @Override  
  38.     public void start(Stage stage) throws Exception {  
  39.         stage.show();  
  40.         scene = new Scene(new Group(), 650300);  
  41.         ObservableList<Node> content = ((Group)scene.getRoot()).getChildren();  
  42.           
  43.         content.add(blendMode());  
  44.         content.add(bloom());  
  45.         content.add(boxBlur());  
  46.         content.add(motionBlur());  
  47.         content.add(gaussianBlur());  
  48.         content.add(dropShadow());  
  49.         content.add(reflection());  
  50.         content.add(lighting());  
  51.         content.add(perspective());  
  52.         content.add(chainEffects());  
  53.           
  54.         stage.setScene(scene);  
  55.     }  
  56.   
  57.     static Node blendMode(){  
  58.         Rectangle rect = new Rectangle();  
  59.           
  60.         rect.setX(590);  
  61.         rect.setY(50);  
  62.         rect.setWidth(50);  
  63.         rect.setHeight(50);  
  64.         rect.setFill(Color.BLUE);  
  65.           
  66.         Circle c = new Circle();  
  67.         c.setFill(Color.rgb(255000.5f));  
  68.         c.setCenterX(590);  
  69.         c.setCenterY(50);  
  70.         c.setRadius(25);  
  71.           
  72.         Group g = new Group();  
  73.         g.setBlendMode(BlendMode.MULTIPLY);//blendMode混合模式//multiply:按比例增大  
  74.         g.getChildren().add(rect);  
  75.         g.getChildren().add(c);  
  76.           
  77.         return g;  
  78.     }  
  79.       
  80.     static Node bloom(){//bloom:开花、盛开  
  81.         Group g = new Group();  
  82.           
  83.         Rectangle r = new Rectangle();  
  84.         r.setX(10);  
  85.         r.setY(10);  
  86.         r.setWidth(160);  
  87.         r.setHeight(80);  
  88.         r.setFill(Color.DARKBLUE);  
  89.           
  90.         Text t = new Text();  
  91.         t.setText("Bloom!");  
  92.         t.setFill(Color.YELLOW);  
  93.         t.setFont(Font.font("null", FontWeight.BOLD,36));  
  94.         t.setX(25);  
  95.         t.setY(65);  
  96.           
  97.         g.setCache(true);//Cache:快取、缓存  
  98.           
  99.         Bloom bloom = new Bloom();  
  100.         bloom.setThreshold(1.0);//threshold:界限,限度   
  101.           
  102.         g.setEffect(bloom);  
  103.         g.setTranslateX(50);  
  104.           
  105.         g.getChildren().add(r);  
  106.         g.getChildren().add(t);  
  107.         return g;  
  108.     }  
  109.       
  110.     static Node boxBlur(){  
  111.         Text t = new Text();  
  112.         t.setText("Blurry Text!");  
  113.         t.setFill(Color.RED);  
  114.         t.setFont(Font.font("null", FontWeight.BOLD, 36));  
  115.         t.setX(10);  
  116.         t.setY(40);  
  117.           
  118.         BoxBlur bb = new BoxBlur();//blur变模糊  
  119.         bb.setWidth(5);  
  120.         bb.setHeight(5);  
  121.         bb.setIterations(3);//Iterations:反复、迭代  
  122.           
  123.         t.setTranslateX(300);  
  124.         t.setTranslateY(100);  
  125.         t.setEffect(bb);  
  126.           
  127.         return t;  
  128.     }  
  129.       
  130.     static Node motionBlur() {  
  131.         Text t = new Text();  
  132.         t.setX(20.0f);  
  133.         t.setY(80.0f);  
  134.         t.setText("Motion Blur");  
  135.         t.setFill(Color.RED);  
  136.         t.setFont(Font.font("null", FontWeight.BOLD, 60));  
  137.    
  138.         MotionBlur mb = new MotionBlur();//motion移动   模糊  
  139.         mb.setRadius(15.0f);  
  140.         mb.setAngle(45.0f);  
  141.    
  142.         t.setEffect(mb);  
  143.    
  144.         t.setTranslateX(300);  
  145.         t.setTranslateY(150);  
  146.    
  147.         return t;  
  148.     }  
  149.       
  150.     static Node gaussianBlur() {//gaussian:高斯  模糊  
  151.         Text t2 = new Text();  
  152.         t2.setX(10.0f);  
  153.         t2.setY(140.0f);  
  154.         t2.setCache(true);  
  155.         t2.setText("Gaussian Blur");  
  156.         t2.setFill(Color.RED);  
  157.         t2.setFont(Font.font("null", FontWeight.BOLD, 36));  
  158.         t2.setEffect(new GaussianBlur());  
  159.         return t2;  
  160.     }  
  161.       
  162.     static Node dropShadow(){  
  163.         Group g = new Group();  
  164.           
  165.         DropShadow ds1 = new DropShadow();  
  166.         ds1.setOffsetY(4.0f);  
  167.         ds1.setOffsetX(4.0f);  
  168.         ds1.setColor(Color.CORAL);  
  169.    
  170.         Circle c = new Circle();  
  171.         c.setEffect(ds1);  
  172.         c.setCenterX(50.0f);  
  173.         c.setCenterY(225.0f);  
  174.         c.setRadius(30.0f);  
  175.         c.setFill(Color.RED);  
  176.         c.setCache(true);  
  177.    
  178.         g.getChildren().add(c);  
  179.           
  180.         return g;  
  181.     }  
  182.       
  183.     static Node reflection() {  
  184.         Text t = new Text();  
  185.         t.setX(10.0f);  
  186.         t.setY(50.0f);  
  187.         t.setCache(true);  
  188.         t.setText("Reflection in JavaFX...");  
  189.         t.setFill(Color.RED);  
  190.         t.setFont(Font.font("null", FontWeight.BOLD, 30));  
  191.    
  192.         Reflection r = new Reflection();  
  193.         r.setFraction(0.9);  
  194.    
  195.         t.setEffect(r);  
  196.    
  197.         t.setTranslateY(400);  
  198.         return t;  
  199.     }  
  200.       
  201.     static Node lighting() {  
  202.         Distant light = new Distant();  
  203.         light.setAzimuth(-135.0f);  
  204.    
  205.         Lighting l = new Lighting();  
  206.         l.setLight(light);  
  207.         l.setSurfaceScale(5.0f);  
  208.    
  209.         Text t = new Text();  
  210.         t.setText("JavaFX"+"\n"+"Lighting!");  
  211.         t.setFill(Color.RED);  
  212.         t.setFont(Font.font("null", FontWeight.BOLD, 70));  
  213.         t.setX(10.0f);  
  214.         t.setY(10.0f);  
  215.         t.setTextOrigin(VPos.TOP);  
  216.    
  217.         t.setEffect(l);  
  218.    
  219.         t.setTranslateX(350);  
  220.         t.setTranslateY(320);  
  221.    
  222.         return t;  
  223.     }  
  224.       
  225.     static Node perspective() {  
  226.         Group g = new Group();  
  227.         PerspectiveTransform pt = new PerspectiveTransform();  
  228.         pt.setUlx(10.0f);  
  229.         pt.setUly(10.0f);  
  230.         pt.setUrx(210.0f);  
  231.         pt.setUry(40.0f);  
  232.         pt.setLrx(210.0f);  
  233.         pt.setLry(60.0f);  
  234.         pt.setLlx(10.0f);  
  235.         pt.setLly(90.0f);  
  236.    
  237.         g.setEffect(pt);  
  238.         g.setCache(true);  
  239.    
  240.         Rectangle r = new Rectangle();  
  241.         r.setX(10.0f);  
  242.         r.setY(10.0f);  
  243.         r.setWidth(280.0f);  
  244.         r.setHeight(80.0f);  
  245.         r.setFill(Color.DARKBLUE);  
  246.    
  247.         Text t = new Text();  
  248.         t.setX(400.0f);  
  249.         t.setY(465.0f);  
  250.         t.setText("Perspective");  
  251.         t.setFill(Color.RED);  
  252.         t.setFont(Font.font("null", FontWeight.BOLD, 36));  
  253.    
  254.         g.getChildren().add(r);  
  255.         g.getChildren().add(t);  
  256.         return g;  
  257.     }  
  258.       
  259. static Node chainEffects() {  
  260.           
  261.         Rectangle rect = new Rectangle();  
  262.         rect.setFill(Color.RED);  
  263.         rect.setWidth(200);  
  264.         rect.setHeight(100);  
  265.         rect.setX(60.0f);  
  266.         rect.setY(550.0f);  
  267.    
  268.         DropShadow ds = new DropShadow();  
  269.         ds.setOffsetY(5.0);  
  270.         ds.setOffsetX(5.0);  
  271.         ds.setColor(Color.GRAY);  
  272.           
  273.           
  274.         Reflection reflection = new Reflection();  
  275.    
  276.         ds.setInput(reflection);      
  277.         rect.setEffect(ds);  
  278.    
  279.         return rect;  
  280.     }  
  281. }  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值