Documentation



Send Feedback | Print | PDF | ePub | Mobi
JavaFX: Working with JavaFX UI Components

D Text Samples

This appendix lists code for the following JavaFX samples:

For the descriptions of the source files, see Applying Effects to Text.

TextEffects.java

/*
 * Copyright (c) 2011, 2014, Oracle and/or its affiliates.
 * All rights reserved. Use is subject to license terms.
 *
 * This file is available and licensed under the following license:
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * - Redistributions of source code must retain the above copyright
 * notice, this list of conditions and the following disclaimer.
 * - Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in
 * the documentation and/or other materials provided with the distribution.
 * - Neither the name of Oracle nor the names of its
 * contributors may be used to endorse or promote products derived
 * from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
 
 
 
 
package texteffects;
 
import javafx.application.Application;
import javafx.collections.ObservableList;
import javafx.geometry.VPos;
import javafx.scene.Group;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.effect.*;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;
import javafx.scene.text.Text;
import javafx.stage.Stage;
 
 
public class TextEffects extends Application {
 
 Stage stage;
 Scene scene;
 
 @Override 
 public void start(Stage stage) {
 stage.show();
 
 scene = new Scene(new Group(), 900, 600);
 ObservableList content = ((Group)scene.getRoot()).getChildren();
 
 /// Perspective
 content.add(perspective());
 /// DropShadow
 content.add(dropShadow());
 /// Bloom
 content.add(bloom());
 /// BoxBlur
 content.add(boxBlur());
 /// DisplacementMap
 content.add(displacementMap());
 /// InnerShadow
 content.add(innerShadow());
 /// Lighting
 content.add(lighting());
 /// MotionBlur
 content.add(motionBlur());
 /// Reflection
 content.add(reflection());
 /// GaussianBlur
 content.add(gaussianBlur());
 /// DistantLight
 content.add(distantLight());
 stage.setScene(scene);
 stage.setTitle("Text Effects");
 }
 
 static Node distantLight() {
 Light.Distant light = new Light.Distant();
 light.setAzimuth(-135.0f);
 light.setElevation(30.0f);
 
 Lighting l = new Lighting();
 l.setLight(light);
 l.setSurfaceScale(5.0f);
 
 final Text t = new Text();
 t.setText("DistantLight");
 t.setFill(Color.RED);
 t.setFont(Font.font(null, FontWeight.BOLD, 70));
 t.setX(10.0f);
 t.setY(10.0f);
 t.setTextOrigin(VPos.TOP);
 
 t.setEffect(l);
 
 final Rectangle r = new Rectangle();
 r.setFill(Color.BLACK);
 
 Group g = new Group();
 g.getChildren().add(r);
 g.getChildren().add(t);
 
 g.setTranslateY(470);
 
 return g;
 }
 
 static Node perspective() {
 Group g = new Group();
 PerspectiveTransform pt = new PerspectiveTransform();
 pt.setUlx(10.0f);
 pt.setUly(10.0f);
 pt.setUrx(310.0f);
 pt.setUry(40.0f);
 pt.setLrx(310.0f);
 pt.setLry(60.0f);
 pt.setLlx(10.0f);
 pt.setLly(90.0f);
 
 g.setEffect(pt);
 g.setCache(true);
 
 Rectangle r = new Rectangle();
 r.setX(10.0f);
 r.setY(10.0f);
 r.setWidth(280.0f);
 r.setHeight(80.0f);
 r.setFill(Color.BLUE);
 
 Text t = new Text();
 t.setX(20.0f);
 t.setY(65.0f);
 t.setText("Perspective");
 t.setFill(Color.YELLOW);
 t.setFont(Font.font(null, FontWeight.BOLD, 36));
 
 g.getChildren().add(r);
 g.getChildren().add(t);
 return g;
 }
 
 static Node gaussianBlur() {
 Text t2 = new Text();
 t2.setX(10.0f);
 t2.setY(140.0f);
 t2.setCache(true);
 t2.setText("Blurry Text");
 t2.setFill(Color.RED);
 t2.setFont(Font.font(null, FontWeight.BOLD, 36));
 t2.setEffect(new GaussianBlur());
 return t2;
 }
 
 static Node reflection() {
 Text t = new Text();
 t.setX(10.0f);
 t.setY(50.0f);
 t.setCache(true);
 t.setText("Reflections on JavaFX...");
 t.setFill(Color.RED);
 t.setFont(Font.font(null, FontWeight.BOLD, 30));
 
 Reflection r = new Reflection();
 r.setFraction(0.7f);
 
 t.setEffect(r);
 
 t.setTranslateY(400);
 return t;
 }
 
 static Node motionBlur() {
 Text t = new Text();
 t.setX(100.0f);
 t.setY(100.0f);
 t.setText("Motion");
 t.setFill(Color.RED);
 t.setFont(Font.font(null, FontWeight.BOLD, 60));
 
 MotionBlur mb = new MotionBlur();
 mb.setRadius(15.0f);
 mb.setAngle(-30.0f);
 
 t.setEffect(mb);
 
 t.setTranslateX(300);
 t.setTranslateY(150);
 
 return t;
 }
 
 static Node lighting() {
 Light.Distant light = new Light.Distant();
 light.setAzimuth(-135.0f);
 
 Lighting l = new Lighting();
 l.setLight(light);
 l.setSurfaceScale(5.0f);
 
 Text t = new Text();
 t.setText("Lighting!");
 t.setFill(Color.RED);
 t.setFont(Font.font(null, FontWeight.BOLD, 70));
 t.setX(10.0f);
 t.setY(10.0f);
 t.setTextOrigin(VPos.TOP);
 
 t.setEffect(l);
 
 t.setTranslateX(0);
 t.setTranslateY(320);
 
 return t;
 }
 
 static Node innerShadow() {
 InnerShadow is = new InnerShadow();
 is.setOffsetX(4.0f);
 is.setOffsetY(4.0f);
 
 Text t = new Text();
 t.setEffect(is);
 t.setX(20);
 t.setY(100);
 t.setText("InnerShadow");
 t.setFill(Color.YELLOW);
 t.setFont(Font.font(null, FontWeight.BOLD, 80));
 
 t.setTranslateX(350);
 t.setTranslateY(300);
 
 return t;
 }
 
 static Node displacementMap() {
 int w = 220;
 int h = 100;
 FloatMap map = new FloatMap();
 map.setWidth(w);
 map.setHeight(h);
 
 for (int i = 0; i < w; i++) {
 double v = (Math.sin(i / 50.0 * Math.PI) - 0.5) / 40.0;
 for (int j = 0; j < h; j++) {
 map.setSamples(i, j, 0.0f, (float) v);
 }
 }
 
 Group g = new Group();
 DisplacementMap dm = new DisplacementMap();
 dm.setMapData(map);
 
 Rectangle r = new Rectangle();
 r.setX(80.0f);
 r.setY(40.0f);
 r.setWidth(w);
 r.setHeight(h);
 r.setFill(Color.BLUE);
 
 g.getChildren().add(r);
 
 Text t = new Text();
 t.setX(100.0f);
 t.setY(80.0f);
 t.setText("Wavy Text");
 t.setFill(Color.YELLOW);
 t.setFont(Font.font(null, FontWeight.BOLD, 36));
 
 g.getChildren().add(t);
 
 g.setEffect(dm);
 g.setCache(true);
 
 g.setTranslateX(300);
 g.setTranslateY(180);
 
 return g;
 }
 
 static Node boxBlur() {
 Text t = new Text();
 t.setText("Blurry Text!");
 t.setFill(Color.RED);
 t.setFont(Font.font(null, FontWeight.BOLD, 36));
 t.setX(10);
 t.setY(40);
 
 BoxBlur bb = new BoxBlur();
 bb.setWidth(15);
 bb.setHeight(15);
 bb.setIterations(3);
 
 t.setEffect(bb);
 t.setTranslateX(350);
 t.setTranslateY(100);
 
 return t;
 }
 
 
 static Node dropShadow() {
 DropShadow ds = new DropShadow();
 ds.setOffsetY(3.0f);
 ds.setColor(Color.color(0.4f, 0.4f, 0.4f));
 
 Text t = new Text();
 t.setEffect(ds);
 t.setCache(true);
 t.setX(10.0f);
 t.setY(270.0f);
 t.setFill(Color.RED);
 t.setText("JavaFX drop shadow...");
 t.setFont(Font.font(null, FontWeight.BOLD, 32));
 
 return t;
 }
 
 static Node bloom() {
 Group g = new Group();
 
 Rectangle r = new Rectangle();
 r.setX(10);
 r.setY(10);
 r.setWidth(160);
 r.setHeight(80);
 r.setFill(Color.DARKBLUE);
 
 Text t = new Text();
 t.setText("Bloom!");
 t.setFill(Color.YELLOW);
 t.setFont(Font.font(null, FontWeight.BOLD, 36));
 t.setX(25);
 t.setY(65);
 
 g.setCache(true);
 g.setEffect(new Bloom());
 g.getChildren().add(r);
 g.getChildren().add(t);
 g.setTranslateX(350);
 return g;
 }
 
 /**
 * @param args the command line arguments
 */
 public static void main(String[] args) {
 Application.launch (args);
 }
}
 
 

NeonSign.java

/*
 * Copyright (c) 2011, 2014, Oracle and/or its affiliates.
 * All rights reserved. Use is subject to license terms.
 *
 * This file is available and licensed under the following license:
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * - Redistributions of source code must retain the above copyright
 * notice, this list of conditions and the following disclaimer.
 * - Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in
 * the documentation and/or other materials provided with the distribution.
 * - Neither the name of Oracle nor the names of its
 * contributors may be used to endorse or promote products derived
 * from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
 
package neonsign;
 
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.TextField;
import javafx.scene.effect.Blend;
import javafx.scene.effect.BlendMode;
import javafx.scene.effect.DropShadow;
import javafx.scene.effect.InnerShadow;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.scene.text.Text;
import javafx.stage.Stage;
 
 
public class NeonSign extends Application {
 
 /**
 * @param args the command line arguments
 */
 public static void main(String[] args) {
 Application.launch(args);
 }
 
 @Override
 public void start(Stage primaryStage) {
 primaryStage.setTitle("Text Effects: Neon Sign");
 double width = 600;
 double height = 300;
 Pane root = new Pane();
 Scene scene = new Scene(root, width, height);
 
 Rectangle rect = new Rectangle (width, height);
 rect.getStyleClass().add("myrect");
 
 Text text = new Text();
 text.setId("fancytext");
 
 text.setX(20);
 text.setY(150);
 Blend blend = new Blend();
 blend.setMode(BlendMode.MULTIPLY);
 
 DropShadow ds = new DropShadow();
 ds.setColor(Color.rgb(254, 235, 66, 0.3));
 ds.setOffsetX(5);
 ds.setOffsetY(5);
 ds.setRadius(5);
 ds.setSpread(0.2);
 
 blend.setBottomInput(ds);
 
 DropShadow ds1 = new DropShadow();
 ds1.setColor(Color.web("#f13a00"));
 ds1.setRadius(20);
 ds1.setSpread(0.2);
 
 Blend blend2 = new Blend();
 blend2.setMode(BlendMode.MULTIPLY);
 
 InnerShadow is = new InnerShadow();
 is.setColor(Color.web("#feeb42"));
 is.setRadius(9);
 is.setChoke(0.8);
 blend2.setBottomInput(is);
 
 InnerShadow is1 = new InnerShadow();
 is1.setColor(Color.web("#f13a00"));
 is1.setRadius(5);
 is1.setChoke(0.4);
 blend2.setTopInput(is1);
 
 Blend blend1 = new Blend();
 blend1.setMode(BlendMode.MULTIPLY);
 blend1.setBottomInput(ds1);
 blend1.setTopInput(blend2);
 
 blend.setTopInput(blend1);
 
 text.setEffect(blend);
 
 TextField textField = new TextField();
 textField.setText("Neon Sign");
 text.textProperty().bind(textField.textProperty());
 textField.setPrefColumnCount(40);
 textField.setLayoutX(50);
 textField.setLayoutY(260);
 
 root.getChildren().addAll(rect,text,textField);
 primaryStage.setScene(scene);
 scene.getStylesheets().add(NeonSign.class.getResource("brickStyle.css").toExternalForm());
 primaryStage.show();
 }
}

Table of Contents

JavaFX: Working with JavaFX UI Components

|

Facebook Twitter RSS Feeds
Copyright © 2011, 2014, Oracle and/or its affiliates. All rights reserved. Legal Notices

AltStyle によって変換されたページ (->オリジナル) /